当前位置 主页 > 网站技术 > 代码类 >

    C#WinFrom导出Excel过程解析

    栏目:代码类 时间:2019-11-01 21:05

    这篇文章主要介绍了C#WinFrom导出Excel过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    采用的是以DataGridView的形式导出,使用NPOI.dll

    1.由于使用的是DataGridView,所以类需要创建在From的Project下,DLL导入NPOI

    2.代码如下

    ExportExcel

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using NPOI.SS.UserModel;    //NPOI
    using NPOI.HSSF.Util;      //NPOI
    using NPOI.HSSF.UserModel;   //NPOI
    using NPOI.XSSF.UserModel;   //NPOI
    using System.IO;
    namespace ESMT
    {
      public class ExportExcel
      {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="grdview">数据表</param>
        /// <param name="sheetName">工作簿名字</param>
        /// <param name="FilePath">文件路径</param>
        /// <param name="columnTitle">列头</param>
        public void ExportToExcel(DataGridView grdview, string sheetName, string FilePath, string[] columnTitle)
        {
    
          //不允许dataGridView显示添加行,负责导出时会报最后一行未实例化错误
          grdview.AllowUserToAddRows = false;
          HSSFWorkbook workbook = new HSSFWorkbook();
          ISheet sheet = workbook.CreateSheet(sheetName);//创建工作簿
          //设置表头
          IRow headerRow = sheet.CreateRow(0);//创建第一行
          headerRow.HeightInPoints = 40;
          headerRow.CreateCell(0).SetCellValue("出库表单");//单元格赋值
          ICellStyle headStyle = workbook.CreateCellStyle();
          headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//格式居中
          IFont font = workbook.CreateFont();
          font.Boldweight = 500;
          font.FontHeightInPoints = 20;
          headStyle.SetFont(font);
          headerRow.GetCell(0).CellStyle = headStyle;
          sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, grdview.ColumnCount - 2));//单元格合并 最后个参数是合并个数
    
          IRow headerRow2 = sheet.CreateRow(1);//创建第二行列头
          ICellStyle headStyle2 = workbook.CreateCellStyle();
          headStyle2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
          IFont font2 = workbook.CreateFont();
          font2.FontHeightInPoints = 10;
          font2.Boldweight = 700;
          headStyle2.SetFont(font2);
          for (int l = 0; l < grdview.ColumnCount - 1; l++) //列头填值
          {
            headerRow2.CreateCell(l).SetCellValue(columnTitle[l]);
            headerRow2.GetCell(l).CellStyle = headStyle2;
          }
    
          //设置列宽
          for (int l = 0; l < grdview.Columns.Count; l++)
          {
            sheet.DefaultColumnWidth = 15;
          }
    
          //填写内容
          for (int i = 0; i < grdview.Rows.Count; i++)
          {
            IRow row = sheet.CreateRow(i + 2);
            for (int j = 1; j < grdview.Columns.Count; j++)
            {
              row.CreateCell(j - 1, CellType.String).SetCellValue(grdview.Rows[i].Cells[j].Value.ToString());//j-1表示哪个单元格
            }
          }
    
          using (FileStream stream = File.OpenWrite(FilePath))//创建Excel并写入数据
          {
            workbook.Write(stream);
            stream.Close();
          }
          GC.Collect();
        }
      }
    }