Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

There are several ways for C # to read and write Excel

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What this article shares with you is that there are several ways to read and write Excel in C#. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

1 use the libraries that come with Office

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

The read and write code is as follows:

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"; / / create Excel.Application xlApp = new Excel.Application (); xlApp.DisplayAlerts = false; xlApp.Visible = false; xlApp.ScreenUpdating = false / / Open Excel Excel.Workbook xlsWorkBook = xlApp.Workbooks.Open (importExcelPath, System.Type.Missing, System.Type.Missing) / / for more operations, Baidu Excel.Worksheet sheet = xlsWorkBook.Worksheets [1]; / / start the workbook from 1, not 0 sheet.Cells [1,1] = "test"; / / Save xlsWorkBook.SaveAs (exportExcelPath, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing); / / close the Excel process ClosePro (xlApp, xlsWorkBook) } public void ClosePro (Excel.Application xlApp, Excel.Workbook xlsWorkBook) {if (xlsWorkBook! = null) xlsWorkBook.Close (true, Type.Missing, Type.Missing); xlApp.Quit (); / / secure recycling process System.GC.GetGeneration (xlApp); IntPtr t = new IntPtr (xlApp.Hwnd); / / get handle int k = 0; GetWindowThreadProcessId (t, out k); / / get process unique flag System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById (k); p.Kill () / / close the process}

two。 Use NPOI

Address: https://github.com/tonyqus/npoi

You can also read and write when you don't install office. It's fast. Download NPOI from Nuget.

The read and write code is as follows:

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); / / get the first workbook IRow row = (IRow) sheet.GetRow (0) / / get the first row / / set the first row and first column values. For more methods, please refer to the source official Demo row.CreateCell (0). SetCellValue ("test"); / / set the first row and first column values / / export excel FileStream fs = new FileStream (exportExcelPath, FileMode.Create, FileAccess.ReadWrite); workbook.Write (fs); fs.Close ();}

3. Use ClosedXml

Address: https://github.com/ClosedXML/ClosedXML

Download ClosedXml from Nuget

The read and write code is as follows:

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); / / this library also starts from 1 / sets the values of the first row and the first column. For more methods, please refer to the official Demo sheet.Cell (1,1). Value = "test". / / this method also starts at 1, not 0 workbook.SaveAs (exportExcelPath);}

4. Use spire.xls

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

Spire is divided into free and charge, no special needs can be used free of charge

Download Free Spire.xls For .NET from Nuget

The read and write code is as follows:

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); / / processing Excel data. For more information, please refer to the official Demo Spire.Xls.Worksheet sheet = workbook.Worksheets [0]; sheet.Range [1J 1] .Text = "test" / / this method also starts at 1, not 0 workbook.SaveToFile (exportExcelPath);}

5. EPPLUS

Address: https://github.com/pruiz/EPPlus/tree/master/EPPlus

If I haven't used this, I won't make an introduction for the time being.

The above are several ways for C # to read and write Excel, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report