NPOI导出Excel

 private void ExportToExcel(string sheetName, string headerName )
        {


            if (dt_Grid == null) return;
            HSSFWorkbook workBook = new HSSFWorkbook();

            ISheet sheet = workBook.CreateSheet(sheetName);

           

            //创建表头

            int  rowCount = 1;

            IRow row01 = sheet.CreateRow(rowCount);
            int data_colIndex = 0;


            ICellStyle style_cell01 = workBook.CreateCellStyle();
            style_cell01.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; //居中

            style_cell01.BorderTop = style_cell01.BorderBottom = style_cell01.BorderLeft = style_cell01.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;

            style_cell01.LeftBorderColor = style_cell01.RightBorderColor = style_cell01.TopBorderColor = style_cell01.BottomBorderColor
                = HSSFColor.BLACK.index;
            IFont font_cell01 = workBook.CreateFont();
            font_cell01.FontHeightInPoints = 12; //字体大小
            font_cell01.Boldweight = 600;
            style_cell01.SetFont(font_cell01);
          
            foreach (DataColumn data_col in dt_Grid.Columns)
            {

                ICell cell_col = row01.CreateCell(data_colIndex);
                cell_col.SetCellValue(data_col.ColumnName);
                cell_col.CellStyle = style_cell01;
                data_colIndex++;

            }
           
           
#region 设置数据的字体
             IFont font_cells = workBook.CreateFont();
            font_cells.FontHeightInPoints = 12; //字体大小
            
           
#endregion

           rowCount = 2;
           foreach (DataRow data_row in dt_Grid.Rows)
           {

               IRow rows = sheet.CreateRow(rowCount);
               for (int cols = 0; cols < dt_Grid.Columns.Count; cols++)
               {
                   ICell cell_cols = rows.CreateCell(cols);
                   cell_cols.SetCellValue(data_row[cols].ToString());
                   style_cell01.SetFont(font_cells);
                   cell_cols.CellStyle = style_cell01;
               }
               rowCount++;
           }

            //设置列自动,在合并单元格前
           for (int col = 0; col < dt_Grid.Columns.Count; col++)
           {
               sheet.AutoSizeColumn(col);
           }


           #region

           rowCount = 0;
           IRow row00 = sheet.CreateRow(rowCount);

           ICell cell = row00.CreateCell(0);

           //创建-1个单元格(必须创建为了边框设置)
           for (int ii = 1; ii <= dt_Grid.Columns.Count - 1; ii++)
           {
               ICell cell_i = row00.CreateCell(ii);
           }


           int range_index = sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dt_Grid.Columns.Count - 1));
           cell.SetCellValue(headerName);
           CellRangeAddress range = sheet.GetMergedRegion(range_index);

           //解决合并丢失样式!!!
           for (int j = range.FirstRow; j <= range.LastRow; j++)
           {
               for (int k = range.FirstColumn; k <= range.LastColumn; k++)
               {

                   ICellStyle cellStyle = workBook.CreateCellStyle();
                   cellStyle.BorderTop = cellStyle.BorderBottom = cellStyle.BorderLeft = cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;

                   cellStyle.LeftBorderColor = cellStyle.RightBorderColor = cellStyle.TopBorderColor = cellStyle.BottomBorderColor
                       = HSSFColor.BLACK.index;
                   cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
                   IFont font_cell = workBook.CreateFont();
                   font_cell.FontHeightInPoints = 20; //字体大小
                   font_cell.Boldweight = 600;
                   cellStyle.SetFont(font_cell);
                   ICell cell_range = sheet.GetRow(j).GetCell(k);
                   if (cell_range != null)
                   {
                       cell_range.CellStyle = cellStyle;
                   }

               }
           }

           #endregion
           WriteToFile(workBook);





        }
        private void WriteToFile(HSSFWorkbook work)
        {

            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "Excel|*.xls";
            if (sf.ShowDialog() == DialogResult.OK)
            {

                FileStream file = new FileStream(sf.FileName, FileMode.Create, FileAccess.ReadWrite);
                work.Write(file);
                file.Close();
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值