在workbook的sheet中插入行的方法

        private void MyInsertRowN(ISheet sheet, int aa, int count, IRow bb, DataTable dtName,int flag)
        {
            #region 批量移动行
            sheet.ShiftRows(
                aa,                                 //--开始行
                sheet.LastRowNum,                      //--结束行
                count,                             //--移动大小(行数)--往下移动
                true,                                  //是否复制行高
                false//,                               //是否重置行高
            );
            #endregion
            #region        
            if (flag == 1)
            {
            for (int i = aa; i < aa + count; i++)           /*对批量移动后空出的空行插,创建相应的行,并以插入行的上一行为格式源(即:插入行-1的那一行)*/
            {
                IRow targetRow = null;
                ICell sourceCell = null;
                ICell targetCell = null;
                targetRow = sheet.CreateRow(i);
                for (int m = bb.FirstCellNum; m < bb.LastCellNum; m++)
                {
                    sourceCell = bb.GetCell(m);
                    if (sourceCell == null)
                        continue;
                    targetCell = targetRow.CreateCell(m);
                    targetCell.CellStyle = sourceCell.CellStyle;
                    targetCell.SetCellType(sourceCell.CellType);
                }
                sheet.AddMergedRegion(new CellRangeAddress(i, i, 1, 2));          //CellRangeAddress四个参数为:起始行,结束行,起始列,结束列
                sheet.AddMergedRegion(new CellRangeAddress(i, i, 3, 4));
                sheet.AddMergedRegion(new CellRangeAddress(i, i, 5, 6));
                sheet.AddMergedRegion(new CellRangeAddress(i, i, 7, 10));
                DataTable dt = dtName;
                string str = dt.Rows[nnn][0].ToString();
                string str1 = dt.Rows[nnn][1].ToString();
                string str2 = dt.Rows[nnn][2].ToString();
                string str3 = dt.Rows[nnn][3].ToString();
                sheet.GetRow(i - 1).GetCell(1).SetCellValue(str);
                double result;
                double.TryParse(str1, out result);
                sheet.GetRow(i - 1).GetCell(3).SetCellValue(result);
                sheet.GetRow(i - 1).GetCell(3).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00");//设置数据类型为两位小数的number型;
                double result2;
                double.TryParse(str2, out result2);
                sheet.GetRow(i - 1).GetCell(5).SetCellValue(result2);
                sheet.GetRow(i - 1).GetCell(5).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00");
                double result3;
                double.TryParse(str3, out result3);
                sheet.GetRow(i - 1).GetCell(7).SetCellValue(result3);
                sheet.GetRow(i - 1).GetCell(7).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00");
                nnn++;
            }
            }
            else
            {

            }
         
        }

你可能感兴趣的:(C#)