NPOI insert row like excel

泄露秘密 提交于 2019-12-23 19:30:40

问题


How can I use NPOI to insert a row like excel? The excel insert command copy the format for the upper row

Thanks!


回答1:


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




回答2:


 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.



来源:https://stackoverflow.com/questions/6673623/npoi-insert-row-like-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!