NPOI 插入行[转]

虽然NPOI中没有提示现有的插入行函数,但我们可以写一个类似效果的函数:

这里用到了NPOI中自带的ShiftRows函数,该函数功能:对指定行移动操作

        public void ShiftRows(int startRow, int endRow, int n);
        public void ShiftRows(int startRow, int endRow, int n, bool copyRowHeight, bool resetOriginalRowHeight);
        public void ShiftRows(int startRow, int endRow, int n, bool copyRowHeight, bool resetOriginalRowHeight, bool moveComments);

思路:

一:先确要从第几行插入----int InsertRowIndex;

二:指定要插入多少行----int InsertRowCount;

三:指定InsertRowIndex上面那行----HSSFRow mySourceStyleRow,并获取他的格式:

获取InsertRowIndex上面那行的方法:InsertRowIndex-1.

打开一个Workbook 可以用到函数NPOIOpenExcel(string fileName);

获取行:

HSSFSheet mySheet= Workbook.GetSheetAt(Workbook.ActiveSheetIndex);

HSSFRow mySourceStyleRow=mySheet.GetRow(InsertRowIndex-1);

四:对InsertRowIndex那一行开始到最后一行:HSSFSheet类下的属性.LastRowNum(获取最后有效的行数)都批量用ShiftRows函数往下移动InsertRowCount行,这时会在第InsertRowIndex行里,出现了InsertRowCount行空行,这里只要对空行的格式都设置为mySourceStyleRow行的格式的每一个相应的单元格格式即可

这里用到函数 MyInsertRow()即可

示例,假如我要在指定的表的第3行插入3行,可以使用这样写:

int InsertRowIndex=2;//指定在第几行插入,我们这里测试用第3行,对应NPOI的索引值2,因为从0起

int InsertRowCount=3;//要插入的行数

HSSFWorkbook Workbook=NPOIOpenExcel(@"E:\TEST\MyExcelTestFile.xls");//打开工作薄

HSSFSheet mySheet= Workbook.GetSheetAt(Workbook.ActiveSheetIndex);//获取工作表

HSSFRow mySourceStyleRow=mySheet.GetRow(InsertRowIndex-1);//获取源格式行

//调用插入行方法

MyInsertRow(mySheet,InsertRowIndex,InsertRowCount,mySourceStyleRow);

//参数说明

//第一个:指定操作的Sheet。

//第二个:指定在第几行指入(插入行的位置)

//第三个:指定要插入多少行

//第四个:源单元格格式的行,

函数部分:

        public HSSFWorkbook NPOIOpenExcel(string FileName)
        {

            HSSFWorkbook MyHSSFWorkBook;

            Stream MyExcelStream = OpenClasspathResource(FileName);

            MyHSSFWorkBook = new HSSFWorkbook(MyExcelStream);

            return MyHSSFWorkBook;
        }

        private void MyInsertRow(HSSFSheet sheet, int 插入行, int 插入行总数, HSSFRow 源格式行)
        {
            #region 批量移动行
                        sheet
                .ShiftRows
                (
                插入行,                                 //--开始行
                sheet
                .LastRowNum,                            //--结束行
                插入行总数,                             //--移动大小(行数)--往下移动
                true,                                   //是否复制行高
                false,                                  //是否重置行高
                true                                    //是否移动批注
                );
            #endregion

            #region 对批量移动后空出的空行插,创建相应的行,并以插入行的上一行为格式源(即:插入行-1的那一行)
            for (int i = 插入行; i < 插入行 + 插入行总数 - 1; i++)
            {
                HSSFRow targetRow = null;
                HSSFCell sourceCell = null;
                HSSFCell targetCell = null;

                targetRow = sheet.CreateRow(i + 1);

                for (int m = 源格式行.FirstCellNum; m < 源格式行.LastCellNum; m++)
                {
                    sourceCell = 源格式行.GetCell(m);
                    if (sourceCell == null)
                        continue;
                    targetCell = targetRow.CreateCell(m);

                    targetCell.Encoding = sourceCell.Encoding;
                    targetCell.CellStyle = sourceCell.CellStyle;
                    targetCell.SetCellType(sourceCell.CellType);

                }
                //CopyRow(sourceRow, targetRow);

                //Util.CopyRow(sheet, sourceRow, targetRow);
            }

            HSSFRow firstTargetRow = sheet.GetRow(插入行);
            HSSFCell firstSourceCell = null;
            HSSFCell firstTargetCell = null;

            for (int m = 源格式行.FirstCellNum; m < 源格式行.LastCellNum; m++)
            {
                firstSourceCell = 源格式行.GetCell(m);
                if (firstSourceCell == null)
                    continue;
                firstTargetCell = firstTargetRow.CreateCell(m);

                firstTargetCell.Encoding = firstSourceCell.Encoding;
                firstTargetCell.CellStyle = firstSourceCell.CellStyle;
                firstTargetCell.SetCellType(firstSourceCell.CellType);
            }
            #endregion
        }


出自http://hi.baidu.com/linrao/creat/blog/Npoi

转载于:https://www.cnblogs.com/kingangWang/archive/2011/08/31/2161319.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值