欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#WinFrom导出Excel过程解析

程序员文章站 2023-11-09 18:48:58
这篇文章主要介绍了c#winfrom导出excel过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 采用的是以datagri...

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

采用的是以datagridview的形式导出,使用npoi.dll

1.由于使用的是datagridview,所以类需要创建在from的project下,dll导入npoi

C#WinFrom导出Excel过程解析

C#WinFrom导出Excel过程解析

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();
    }
  }
}

ps:openwtrie 打开或者创建新的文件写入

3.from窗口点击导出按钮

导出按钮

string[] columntitle = { "序号", "仓位", "facility", "供应商料号", "料号", "料卷id", "料卷数量", "储位号", "date code/lot", "生产日期", "供应商编码", "入仓时间" };
      string localfilepath = "";// filenameext, newfilename, filepath; 
      savefiledialog sfd = new savefiledialog();//保存文件窗口
      //设置文件类型 
      sfd.filter = "excel(97-2003)|*.xls";//保存类型为excel
      //保存对话框是否记忆上次打开的目录 
      sfd.restoredirectory = true;

      //点了保存按钮进入 
      if (sfd.showdialog() == dialogresult.ok)
      {
        localfilepath = sfd.filename.tostring(); //获得文件路径 
        ex.exporttoexcel(grddata, "出库表单", localfilepath, columntitle);
      }

通过以上三步,完成点击导出按钮,后选择保存位置并命名,调用eportexcel方法完成导出excel。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。