NPOI的插入行与删除行,从感觉上来说,没有直接使用COM组件里的添加删除好用,在做的过程中,发现图表并不会随着插入的行而位置向下移。也就是说,图中饼图的位置是12行到27行,当我插入40行数据时,饼图的位置,还是在12行到27行。
不过,也能满足我们的大多数需求了,这里简单的写几个方法好,其实这几个方法都是网上其他人的,我也是汇总测试了一下,还是有用的。
第一种插入方法:


1 private void MyInsertRow(XSSFSheet sheet, int RowIndex, int RowCount, IRow RowStyle) 2 { 3 4 #region 批量移动行 5 sheet.ShiftRows( 6 RowIndex, //--开始行 7 sheet.LastRowNum, //--结束行 8 RowCount, //--移动大小(行数)--往下移动 9 true, //是否复制行高 10 false//, //是否重置行高 11 //true //是否移动批注 12 ); 13 #endregion 14 15 #region //对批量移动后空出的空行插,创建相应的行,并以插入行的上一行为格式源(即:插入行-1的那一行) 16 for (int i = RowIndex; i < RowIndex + RowCount - 1; i++) 17 { 18 IRow targetRow = null; 19 ICell sourceCell = null; 20 ICell targetCell = null; 21 22 targetRow = sheet.CreateRow(i + 1); 23 24 for (int m = RowStyle.FirstCellNum; m < RowStyle.LastCellNum; m++) 25 { 26 sourceCell = RowStyle.GetCell(m); 27 if (sourceCell == null) 28 continue; 29 targetCell = targetRow.CreateCell(m); 30 //targetCell.Encoding = sourceCell.Encoding; 31 targetCell.CellStyle = sourceCell.CellStyle; 32 targetCell.SetCellType(sourceCell.CellType);