第1个回答 推荐于2016-10-23
private void ReportExportMergedCells(NPOI.SS.UserModel.ISheet sheet, string strFields)
{
int iCol = 0, iR = 0, iR1 = 0;
string strValue1 = string.Empty;
if (strFields != "" && !strFields.StartsWith(",")) strFields = "," + strFields;
if (strFields != "" && !strFields.EndsWith(",")) strFields += ",";
foreach (NPOI.SS.UserModel.ICell cell in sheet.GetRow(0).Cells)
{
if (strFields != "" && !strFields.Contains("," + cell.ToString() + ","))
{
iCol++;
continue;
}
iR1 = iR = 0;
strValue1 = string.Empty;
foreach (NPOI.SS.UserModel.IRow irow in sheet)
{
if (iR1 == 0)
{
strValue1 = irow.Cells[iCol].ToString();
iR1 = iR;
}
else if (strValue1 != irow.Cells[iCol].ToString())
{
if ((iR - 1) >= iR1)
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(iR1, iR - 1, iCol, iCol));
strValue1 = irow.Cells[iCol].ToString();
iR1 = iR;
}
else if (iR == (sheet.LastRowNum))//
{
if (strValue1 == irow.Cells[iCol].ToString())
{
if ((iR - 1) >= iR1)
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(iR1, iR, iCol, iCol));
}
}
iR++;
}
iCol++;
}
}本回答被网友采纳
第2个回答 2012-11-08
可以试一下 单元格里面有个两端对齐 适合将单元格里的数据合并到一格(前提是要将所在的列拉开的足够宽)
第3个回答 推荐于2016-10-23
private void ReportExportMergedCells(NPOI.SS.UserModel.ISheet sheet, string strFields)
{
int iCol = 0, iR = 0, iR1 = 0;
string strValue1 = string.Empty;
if (strFields != "" && !strFields.StartsWith(",")) strFields = "," + strFields;
if (strFields != "" && !strFields.EndsWith(",")) strFields += ",";
foreach (NPOI.SS.UserModel.ICell cell in sheet.GetRow(0).Cells)
{
if (strFields != "" && !strFields.Contains("," + cell.ToString() + ","))
{
iCol++;
continue;
}
iR1 = iR = 0;
strValue1 = string.Empty;
foreach (NPOI.SS.UserModel.IRow irow in sheet)
{
if (iR1 == 0)
{
strValue1 = irow.Cells[iCol].ToString();
iR1 = iR;
}
else if (strValue1 != irow.Cells[iCol].ToString())
{
if ((iR - 1) >= iR1)
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(iR1, iR - 1, iCol, iCol));
strValue1 = irow.Cells[iCol].ToString();
iR1 = iR;
}
else if (iR == (sheet.LastRowNum))//
{
if (strValue1 == irow.Cells[iCol].ToString())
{
if ((iR - 1) >= iR1)
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(iR1, iR, iCol, iCol));
}
}
iR++;
}
iCol++;
}
}本回答被网友采纳
第4个回答 2012-11-08
可以试一下 单元格里面有个两端对齐 适合将单元格里的数据合并到一格(前提是要将所在的列拉开的足够宽)