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

详解C#读写Excel的几种方法

程序员文章站 2023-09-07 15:49:44
1 使用office自带的库 前提是本机须安装office才能运行,且不同的office版本之间可能会有兼容问题,从nuget下载 microsoft.office.interop....

1 使用office自带的库

前提是本机须安装office才能运行,且不同的office版本之间可能会有兼容问题,从nuget下载 microsoft.office.interop.excel

详解C#读写Excel的几种方法

读写代码如下:

using microsoft.office.interop.excel;
using excel = microsoft.office.interop.excel;

    private void btn_office_click(object sender, eventargs e)
    {
      string importexcelpath = "e:\\import.xlsx";
      string exportexcelpath = "e:\\export.xlsx";
      //创建
      excel.application xlapp = new excel.application();
      xlapp.displayalerts = false;
      xlapp.visible = false;
      xlapp.screenupdating = false;
      //打开excel
      excel.workbook xlsworkbook = xlapp.workbooks.open(importexcelpath, system.type.missing, system.type.missing, system.type.missing,
      system.type.missing, system.type.missing, system.type.missing, system.type.missing, system.type.missing, system.type.missing, system.type.missing,
      system.type.missing, system.type.missing, system.type.missing, system.type.missing);

      //处理数据过程,更多操作方法自行百度
      excel.worksheet sheet = xlsworkbook.worksheets[1];//工作薄从1开始,不是0
      sheet.cells[1, 1] = "test";

      //另存
      xlsworkbook.saveas(exportexcelpath, type.missing, type.missing, type.missing, type.missing, type.missing, xlsaveasaccessmode.xlnochange,
        type.missing, type.missing, type.missing, type.missing, type.missing);
      //关闭excel进程
      closepro(xlapp, xlsworkbook);
    }

    public void closepro(excel.application xlapp, excel.workbook xlsworkbook)
    {
      if (xlsworkbook != null)
        xlsworkbook.close(true, type.missing, type.missing);
      xlapp.quit();
      // 安全回收进程
      system.gc.getgeneration(xlapp);
      intptr t = new intptr(xlapp.hwnd);  //获取句柄
      int k = 0;
      getwindowthreadprocessid(t, out k);  //获取进程唯一标志
      system.diagnostics.process p = system.diagnostics.process.getprocessbyid(k);
      p.kill();   //关闭进程
    }

2. 使用npoi  

地址:

在不安装office的时候也是可以读写的,速度很快,从nuget下载 npoi

详解C#读写Excel的几种方法

读写代码如下:

using system.io;
using npoi;
using npoi.ss.usermodel;

    private void btn_npoi_click(object sender, eventargs e)
    {
      string importexcelpath = "e:\\import.xlsx";
      string exportexcelpath = "e:\\export.xlsx";
      iworkbook workbook = workbookfactory.create(importexcelpath);
      isheet sheet = workbook.getsheetat(0);//获取第一个工作薄
      irow row = (irow)sheet.getrow(0);//获取第一行

      //设置第一行第一列值,更多方法请参考源官方demo
      row.createcell(0).setcellvalue("test");//设置第一行第一列值

      //导出excel
      filestream fs = new filestream(exportexcelpath, filemode.create, fileaccess.readwrite);
      workbook.write(fs);
      fs.close();
    }

3. 使用closedxml  

地址:https://github.com/closedxml/closedxml

从nuget下载closedxml

详解C#读写Excel的几种方法

读写代码如下:

using closedxml;
using closedxml.excel;

    private void btn_closedxml_click(object sender, eventargs e)
    {
      string importexcelpath = "e:\\import.xlsx";
      string exportexcelpath = "e:\\export.xlsx";
      var workbook = new xlworkbook(importexcelpath);

      ixlworksheet sheet = workbook.worksheet(1);//这个库也是从1开始
      //设置第一行第一列值,更多方法请参考官方demo
      sheet.cell(1, 1).value = "test";//该方法也是从1开始,非0

      workbook.saveas(exportexcelpath);
    }

4. 使用 spire.xls  

地址:https://www.e-iceblue.com/introduce/free-xls-component.html

spire分免费和收费,无特殊需求用免费即可

从nuget下载free spire.xls for .net

详解C#读写Excel的几种方法

读写代码如下:

using spire.xls;

    private void btnspire_click(object sender, eventargs e)
    {
      string importexcelpath = "e:\\import.xlsx";
      string exportexcelpath = "e:\\export.xlsx";

      spire.xls.workbook workbook = new spire.xls.workbook();
      workbook.loadfromfile(importexcelpath);
      //处理excel数据,更多请参考官方demo
      spire.xls.worksheet sheet = workbook.worksheets[0];
      sheet.range[1,1].text = "test";//该方法也是从1开始,非0

      workbook.savetofile(exportexcelpath);
    }

5. epplus  

地址:https://github.com/pruiz/epplus/tree/master/epplus

没用过这个,暂时就不做介绍了

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