Several methods of reading and writing Excel

1 use the library that comes with Office

The premise is that this computer needs to install Office to run, and there may be compatibility problems between different office versions. Download Microsoft.Office.Interop.Excel from Nuget

The read-write code is as follows:

 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             //Establish
 9             Excel.Application xlApp = new Excel.Application();
10             xlApp.DisplayAlerts = false;
11             xlApp.Visible = false;
12             xlApp.ScreenUpdating = false;
13             //open 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             //Data processing, more operation methods
19             Excel.Worksheet sheet = xlsWorkBook.Worksheets[1];//Workbooks start at 1, not 0
20             sheet.Cells[1, 1] = "test";
21 
22             //Save another
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             //Close Excel process
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             // Security recovery process
35             System.GC.GetGeneration(xlApp);
36             IntPtr t = new IntPtr(xlApp.Hwnd);   //Get handle
37             int k = 0;
38             GetWindowThreadProcessId(t, out k);   //Get process unique flag
39             System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
40             p.Kill();     //Closing process
41         }

2. Use NPOI

You can also read and write when you do not install office. It is very fast. Download NPOI from Nuget

 

The read-write code is as follows:

 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);//Get the first Workbook
11             IRow row = (IRow)sheet.GetRow(0);//Get the first line
12 
13             //Set the value of the first row and the first column,For more methods, please refer to the official source Demo
14             row.CreateCell(0).SetCellValue("test");//Set the value of the first row and the first column
15 
16             //export excel
17             FileStream fs = new FileStream(exportExcelPath, FileMode.Create, FileAccess.ReadWrite);
18             workbook.Write(fs);
19             fs.Close();
20         }

3. Use ClosedXml to download ClosedXml from Nuget

 

The read-write code is as follows:

 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);//This library also starts from 1
11             //Set the value of the first row and the first column,For more information, please refer to the official Demo
12             sheet.Cell(1, 1).Value = "test";//This method also starts from 1, not 0
13 
14             workbook.SaveAs(exportExcelPath);
15         }

4. Use spire.xls, and spire is free and charged, free for no special needs

Download free flame.xls for. Net from Nuget

 

The read-write code is as follows:

 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             //Handle Excel For more information, please refer to the official Demo
11             Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
12             sheet.Range[1,1].Text = "test";//This method also starts from 1, not 0
13 
14             workbook.SaveToFile(exportExcelPath);
15         }

5. EPPLUS, I haven't used this, so I won't introduce it for the time being

Keywords: C# Excel

Added by BigE on Wed, 20 Nov 2019 17:08:34 +0200