NPOI像excel一样插入行

Rod*_*rez 5 npoi

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

谢谢!

小智 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;
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

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


小智 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);
}
Run Code Online (Sandbox Code Playgroud)

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