初探NPOI 一款非常好用的Excel操作类库,导出速度快,功能强大

最近开发的一个项目中,需要数据导出到Excel ,使用了微软自带的库 Microsoft.Office.Interop.Excel 写了一个方法,完成后发现导出数据几十条的情况下速度都很慢,需要等待好几秒钟才能完成!

 

然后去网上翻阅了下资料,发现 NPOI 这个开源的 Excel 类库口碑还不错,这次拿来初步学习和使用了下,学习成本也不高,也相当好用,几百条数据秒操作完成!攒一个!

 

随笔记录下这次代码,供小伙伴和自己以后参考!(本人比较懒,没怎么特别整理,大家讲究着看!)

 

 #region 使用 NPOI 方法

                        string fileName = "";

                        #region /*保存对话框*/
                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Filter = "导出Excel(*.xls)|*.xls";
                        sfd.FileName = "车贷客户筛选资料 - " + DateTime.Now.ToString("yyyyMMddHHmmss");

                        if (sfd.ShowDialog() == DialogResult.OK)
                        {

                            short lineheight_header = 400;  //标题列行高
                            short lineheight_content = 300; //记录行行高

                            short fontsize_header = 12; //标题字体大小
                            short fontsize_content = 11;//记录字体大小

                            fileName = sfd.FileName;

                            /* xls */
                            IWorkbook book = new HSSFWorkbook();

                            /* xlsx */
                            //IWorkbook book = new XSSFWorkbook();

                            #region 定义样式


                           

                            // 列名 样式
                            ICellStyle style_ColumnName = book.CreateCellStyle();
                            style_ColumnName.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//文字水平对齐方式
                            style_ColumnName.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直对齐方式
                            
                            //style_ColumnName.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;//设置边框
                            //style_ColumnName.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_ColumnName.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_ColumnName.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;

                            IFont font_ColumnName = book.CreateFont();//字体
                            font_ColumnName.FontName = "宋体";
                            font_ColumnName.FontHeightInPoints = fontsize_header;
                            font_ColumnName.Boldweight = (short)FontBoldWeight.Bold;//字体加粗样式
                            font_ColumnName.Color = HSSFColor.Black.Index;//字体颜色
                            style_ColumnName.SetFont(font_ColumnName);


                            IFont font_contentText = book.CreateFont();//字体
                            font_contentText.FontName = "宋体";
                            font_contentText.FontHeightInPoints = fontsize_content;


                            /* Content - text - Left*/
                            ICellStyle style_contentText_Left = book.CreateCellStyle();
                            style_contentText_Left.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//文字水平对齐方式
                            style_contentText_Left.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直对齐方式

                            //style_contentText_Left.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;//设置边框
                            //style_contentText_Left.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_contentText_Left.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_contentText_Left.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                            style_contentText_Left.SetFont(font_contentText);




                            /* Content - text - Center */
                            ICellStyle style_contentText_Center = book.CreateCellStyle();
                            style_contentText_Center.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//文字水平对齐方式
                            style_contentText_Center.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直对齐方式

                            //style_contentText_Center.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;//设置边框
                            //style_contentText_Center.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_contentText_Center.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_contentText_Center.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                            style_contentText_Center.SetFont(font_contentText);




                            /* Content - DateTime */
                            ICellStyle style_date = book.CreateCellStyle();
                            style_date.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//文字水平对齐方式
                            style_date.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直对齐方式

                            //style_date.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;//设置边框
                            //style_date.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_date.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_date.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;

                            IFont font_date = book.CreateFont();//字体
                            font_date.FontName = "宋体";
                            font_date.FontHeightInPoints = fontsize_content;
                            style_date.SetFont(font_date);

                            IDataFormat dataFormatCustom = book.CreateDataFormat(); //定义数据格式
                            style_date.DataFormat = dataFormatCustom.GetFormat("yyyy-MM-dd");



                            /* Content - numeric  */
                            ICellStyle style_numeric = book.CreateCellStyle();
                            style_numeric.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;//文字水平对齐方式
                            style_numeric.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直对齐方式

                            //style_numeric.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;//设置边框
                            //style_numeric.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_numeric.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                            //style_numeric.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;

                            IFont font_numeric = book.CreateFont();//字体
                            font_numeric.FontName = "宋体";
                            font_numeric.FontHeightInPoints = fontsize_content;
                            style_numeric.SetFont(font_date);

                            IDataFormat numericFormatCustom = book.CreateDataFormat(); //定义数据格式
                            style_numeric.DataFormat = numericFormatCustom.GetFormat("0.00");

                            #endregion

                            #region 创建表单

                            /* 创建一个表单 */
                            ISheet sheet = book.CreateSheet("Sheet0");


                            #region 第一行 标题列

                            IRow row_head1 = sheet.CreateRow(0); //建立行,参数为行号,从0计
                            row_head1.Height = lineheight_header;
                            

                            sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 4));
                            ICell row0cell0 = row_head1.CreateCell(0);  //档案信息
                            row0cell0.SetCellValue("档案信息");
                            row0cell0.CellStyle = style_ColumnName;
                            


                            sheet.AddMergedRegion(new CellRangeAddress(0, 0, 5, 12));
                            ICell row0cell1 = row_head1.CreateCell(5);   //借款人
                            row0cell1.SetCellValue("借款人");
                            row0cell1.CellStyle = style_ColumnName;
                            

                            #endregion


                            #region 第二行 标题列

                            IRow row_head2 = sheet.CreateRow(1); //建立行,参数为行号,从0计
                            row_head2.Height = lineheight_header;

                            /* 档案信息 */
                            ICell row1cell_0 = row_head2.CreateCell(0);  //客户编号 
                            row1cell_0.SetCellValue("客户编号");
                            row1cell_0.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(0, 15 * 256);

                            ICell row1cell_1 = row_head2.CreateCell(1);  //合同号 
                            row1cell_1.SetCellValue("合同号");
                            row1cell_1.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(1, 30 * 256);

                            ICell row1cell_2 = row_head2.CreateCell(2);  //调查员 
                            row1cell_2.SetCellValue("调查员");
                            row1cell_2.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(2, 10 * 256);

                            ICell row1cell_3 = row_head2.CreateCell(3);  //客户状态 
                            row1cell_3.SetCellValue("客户状态");
                            row1cell_3.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(3, 10 * 256);

                            ICell row1cell_4 = row_head2.CreateCell(4);  //备注 
                            row1cell_4.SetCellValue("备注");
                            row1cell_4.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(4, 25 * 256);


                            /* 借款人 */
                            ICell row1cell_5 = row_head2.CreateCell(5);  //姓名 
                            row1cell_5.SetCellValue("姓名");
                            row1cell_5.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(5, 15 * 256);

                            ICell row1cell_6 = row_head2.CreateCell(6);  //身份证号 
                            row1cell_6.SetCellValue("身份证号");
                            row1cell_6.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(6, 25 * 256);

                            ICell row1cell_7 = row_head2.CreateCell(7);  //手机 
                            row1cell_7.SetCellValue("手机");
                            row1cell_7.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(7, 15 * 256);

                            ICell row1cell_8 = row_head2.CreateCell(8);  //企业单位
                            row1cell_8.SetCellValue("企业单位");
                            row1cell_8.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(8, 30 * 256);

                            ICell row1cell_9 = row_head2.CreateCell(9);  //企业地址
                            row1cell_9.SetCellValue("企业地址");
                            row1cell_9.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(9, 30 * 256);

                            ICell row1cell_10 = row_head2.CreateCell(10);  //企业电话
                            row1cell_10.SetCellValue("企业电话");
                            row1cell_10.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(10, 15 * 256);

                            ICell row1cell_11 = row_head2.CreateCell(11);  //住宅地址
                            row1cell_11.SetCellValue("住宅地址");
                            row1cell_11.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(11, 30 * 256);

                            ICell row1cell_12 = row_head2.CreateCell(12);  //住宅电话
                            row1cell_12.SetCellValue("住宅电话");
                            row1cell_12.CellStyle = style_ColumnName;
                            sheet.SetColumnWidth(12, 15 * 256);




                            #endregion

                            #region 冻结 标题两行
                            sheet.CreateFreezePane(0, 2, 0, 2);
                            #endregion

                            #region 数据行

                            int rowindex = 2;

                            foreach (CarCustomer cc in customers)
                            {
                                IRow datarow = sheet.CreateRow(rowindex);
                                datarow.Height = lineheight_content;

                                ICell datacell_0 = datarow.CreateCell(0);  //客户编号 
                                datacell_0.SetCellValue(cc.customerid);
                                datacell_0.CellStyle = style_contentText_Center;
                                
                                ICell datacell_1 = datarow.CreateCell(1);  //合同号 
                                datacell_1.SetCellValue(cc.bankcompactid);
                                datacell_1.CellStyle = style_contentText_Center;

                                ICell datacell_2 = datarow.CreateCell(2);  //调查员 
                                datacell_2.SetCellValue(cc.investigator.name);
                                datacell_2.CellStyle = style_contentText_Center;

                                ICell datacell_3 = datarow.CreateCell(3);  //客户状态 
                                datacell_3.SetCellValue(cc.customerstate.name);
                                datacell_3.CellStyle = style_contentText_Center;

                                ICell datacell_4 = datarow.CreateCell(4);  //备注 
                                datacell_4.SetCellValue(cc.remark);
                                datacell_4.CellStyle = style_contentText_Left;


                                /* 借款人 */
                                ICell datacell_5 = datarow.CreateCell(5);  //姓名 
                                datacell_5.SetCellValue(cc.customername);
                                datacell_5.CellStyle = style_contentText_Center;

                                ICell datacell_6 = datarow.CreateCell(6);  //身份证号 
                                datacell_6.SetCellValue(cc.identitycard);
                                datacell_6.CellStyle = style_contentText_Center;

                                ICell datacell_7 = datarow.CreateCell(7);  //手机 
                                datacell_7.SetCellValue(cc.phone);
                                datacell_7.CellStyle = style_contentText_Center;

                                ICell datacell_8 = datarow.CreateCell(8);  //企业单位
                                datacell_8.SetCellValue(cc.job);
                                datacell_8.CellStyle = style_contentText_Left;

                                ICell datacell_9 = datarow.CreateCell(9);  //企业地址
                                datacell_9.SetCellValue(cc.workaddress);
                                datacell_9.CellStyle = style_contentText_Left;

                                ICell datacell_10 = datarow.CreateCell(10);  //企业电话
                                datacell_10.SetCellValue(cc.worktel);
                                datacell_10.CellStyle = style_contentText_Center;

                                ICell datacell_11 = datarow.CreateCell(11);  //住宅地址
                                datacell_11.SetCellValue(cc.homeaddress);
                                datacell_11.CellStyle = style_contentText_Left;

                                ICell datacell_12 = datarow.CreateCell(12);  //住宅电话
                                datacell_12.SetCellValue(cc.homephone);
                                datacell_12.CellStyle = style_contentText_Center;

                               

                                rowindex += 1;
                            }


                            #endregion



                            #region 导入 Excel

                            // 转为字节数组
                            MemoryStream stream = new MemoryStream();
                            book.Write(stream);
                            var buf = stream.ToArray();

                            //保存为Excel文件  
                            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                            {
                                fs.Write(buf, 0, buf.Length);
                                fs.Flush();
                                MessageBox.Show("导出 EXECL 成功!");
                            }

                            #endregion

                            #endregion








                        }

                        #endregion

 

你可能感兴趣的:(随笔,Knowledge,Ohters,Winform)