C#讀寫Excel的幾種方法


1 使用Office自帶的庫

前提是本機須安裝office才能運行,且不同的office版本之間可能會有兼容問題,從Nuget下載 Microsoft.Office.Interop.Excel

讀寫代碼如下:

 1 using Microsoft.Office.Interop.Excel;
 2 using Excel = Microsoft.Office.Interop.Excel;
 3 
 4         private void btn_Office_Click(object sender, EventArgs e)
 5         {
 6             string importExcelPath = "E:\\import.xlsx";
 7             string exportExcelPath = "E:\\export.xlsx";
 8             //創建
 9             Excel.Application xlApp = new Excel.Application();
10             xlApp.DisplayAlerts = false;
11             xlApp.Visible = false;
12             xlApp.ScreenUpdating = false;
13             //打開Excel
14             Excel.Workbook xlsWorkBook = xlApp.Workbooks.Open(importExcelPath, System.Type.Missing, System.Type.Missing, System.Type.Missing,
15             System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
16             System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
17 
18             //處理數據過程,更多操作方法自行百度
19             Excel.Worksheet sheet = xlsWorkBook.Worksheets[1];//工作薄從1開始,不是0
20             sheet.Cells[1, 1] = "test";
21 
22             //另存
23             xlsWorkBook.SaveAs(exportExcelPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
24                 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
25             //關閉Excel進程
26             ClosePro(xlApp, xlsWorkBook);
27         }
28 
29         public void ClosePro(Excel.Application xlApp, Excel.Workbook xlsWorkBook)
30         {
31             if (xlsWorkBook != null)
32                 xlsWorkBook.Close(true, Type.Missing, Type.Missing);
33             xlApp.Quit();
34             // 安全回收進程
35             System.GC.GetGeneration(xlApp);
36             IntPtr t = new IntPtr(xlApp.Hwnd);   //獲取句柄
37             int k = 0;
38             GetWindowThreadProcessId(t, out k);   //獲取進程唯一標志
39             System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
40             p.Kill();     //關閉進程
41         }

2. 使用NPOI  

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

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

 

讀寫代碼如下:

 1 using System.IO;
 2 using NPOI;
 3 using NPOI.SS.UserModel;
 4 
 5         private void btn_NPOI_Click(object sender, EventArgs e)
 6         {
 7             string importExcelPath = "E:\\import.xlsx";
 8             string exportExcelPath = "E:\\export.xlsx";
 9             IWorkbook workbook = WorkbookFactory.Create(importExcelPath);
10             ISheet sheet = workbook.GetSheetAt(0);//獲取第一個工作薄
11             IRow row = (IRow)sheet.GetRow(0);//獲取第一行
12 
13             //設置第一行第一列值,更多方法請參考源官方Demo
14             row.CreateCell(0).SetCellValue("test");//設置第一行第一列值
15 
16             //導出excel
17             FileStream fs = new FileStream(exportExcelPath, FileMode.Create, FileAccess.ReadWrite);
18             workbook.Write(fs);
19             fs.Close();
20         }

3. 使用ClosedXml  

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

從Nuget下載 ClosedXml

 

讀寫代碼如下:

 1 using ClosedXML;
 2 using ClosedXML.Excel;
 3 
 4         private void btn_ClosedXML_Click(object sender, EventArgs e)
 5         {
 6             string importExcelPath = "E:\\import.xlsx";
 7             string exportExcelPath = "E:\\export.xlsx";
 8             var workbook = new XLWorkbook(importExcelPath);
 9 
10             IXLWorksheet sheet = workbook.Worksheet(1);//這個庫也是從1開始
11             //設置第一行第一列值,更多方法請參考官方Demo
12             sheet.Cell(1, 1).Value = "test";//該方法也是從1開始,非0
13 
14             workbook.SaveAs(exportExcelPath);
15         }

4. 使用 spire.xls  

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

spire分免費和收費,無特殊需求用免費即可

從Nuget下載 Free Spire.xls For .NET

 

讀寫代碼如下:

 1 using Spire.Xls;
 2 
 3         private void btnSpire_Click(object sender, EventArgs e)
 4         {
 5             string importExcelPath = "E:\\import.xlsx";
 6             string exportExcelPath = "E:\\export.xlsx";
 7 
 8             Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
 9             workbook.LoadFromFile(importExcelPath);
10             //處理Excel數據,更多請參考官方Demo
11             Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
12             sheet.Range[1,1].Text = "test";//該方法也是從1開始,非0
13 
14             workbook.SaveToFile(exportExcelPath);
15         }

5. EPPLUS  

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM