5

如何使用 NPOI 插入像 excel 这样的行?excel插入命令复制上面一行的格式

谢谢!

4

2 回答 2

6
static void InsertRows(ref HSSFSheet sheet1, int fromRowIndex, int rowCount)
    {
        sheet1.ShiftRows(fromRowIndex, sheet1.LastRowNum, rowCount, true, false, true);

        for (int rowIndex = fromRowIndex; rowIndex < fromRowIndex + rowCount; rowIndex++)
        {
            HSSFRow rowSource = sheet1.GetRow(rowIndex + rowCount);
            HSSFRow rowInsert = sheet1.CreateRow(rowIndex);
            rowInsert.Height = rowSource.Height;
            for (int colIndex = 0; colIndex < rowSource.LastCellNum; colIndex++)
            {
                HSSFCell cellSource = rowSource.GetCell(colIndex);
                HSSFCell cellInsert = rowInsert.CreateCell(colIndex);
                if (cellSource != null)
                {
                    cellInsert.CellStyle = cellSource.CellStyle;
                }
            }
        }
    }

也许你可以在这里寻找一些灵感

于 2013-07-11T08:53:07.307 回答
5
 HSSFRow newRow = worksheet.GetRow(destinationRowNum);
HSSFRow sourceRow = worksheet.GetRow(sourceRowNum);

// If the row exist in destination, push down all rows by 1 else create a new row
if (newRow != null)
{
    worksheet.ShiftRows(destinationRowNum, worksheet.LastRowNum, 1);
}
else
{
    newRow = worksheet.CreateRow(destinationRowNum);
}

我正在使用此代码创建一个新行。这可能对你很方便。

于 2015-07-16T08:43:38.493 回答