开发者

NPOI insert row like excel

开发者 https://www.devze.com 2023-03-19 16:24 出处:网络
How can I use NPOI to insert a row like excel? The excel insert c开发者_运维问答ommand copy the format for the upper row

How can I use NPOI to insert a row like excel? The excel insert c开发者_运维问答ommand copy the format for the upper row

Thanks!


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;
                }
            }
        }
    }

maybe you can look here for some inspiration


 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);
}

I am using this code to create a new row. This might come handy to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消