问题
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