NPOI insert row like excel

Rodrigo Juarez picture Rodrigo Juarez · Jul 13, 2011 · Viewed 9.2k times · Source

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

Thanks!

Answer

user831170 picture user831170 · Jul 11, 2013
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