2012-04-01 回答
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++;
}
}