1. 程式人生 > 程式設計 >詳解C#讀寫Excel的幾種方法

詳解C#讀寫Excel的幾種方法

1 使用Office自帶的庫

前提是本機須安裝office才能執行,且不同的office版本之間可能會有相容問題,從Nuget下載 Microsoft.Office.Interop.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);

      //處理資料過程,更多操作方法自行百度
      Excel.Worksheet sheet = xlsWorkBook.Worksheets[1];//工作薄從1開始,不是0
      sheet.Cells[1,1] = "test";

      //另存
      xlsWorkBook.SaveAs(exportExcelPath,Type.Missing,XlSaveAsAccessMode.xlNoChange,Type.Missing);
      //關閉Excel程序
      ClosePro(xlApp,xlsWorkBook);
    }

    public void ClosePro(Excel.Application xlApp,Excel.Workbook xlsWorkBook)
    {
      if (xlsWorkBook != null)
        xlsWorkBook.Close(true,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  

地址:https://github.com/tonyqus/npoi

在不安裝office的時候也是可以讀寫的,速度很快,從Nuget下載 NPOI

讀寫程式碼如下:

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

讀寫程式碼如下:

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

讀寫程式碼如下:

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

沒用過這個,暫時就不做介紹了

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。