詳解C#讀寫Excel的幾種方法
1 使用Office自帶的庫(kù)
前提是本機(jī)須安裝office才能運(yùn)行,且不同的office版本之間可能會(huì)有兼容問題,從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"; //創(chuàng)建 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); //處理數(shù)據(jù)過程,更多操作方法自行百度 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); //關(guān)閉Excel進(jìn)程 ClosePro(xlApp, xlsWorkBook); } public void ClosePro(Excel.Application xlApp, Excel.Workbook xlsWorkBook) { if (xlsWorkBook != null) xlsWorkBook.Close(true, Type.Missing, Type.Missing); xlApp.Quit(); // 安全回收進(jìn)程 System.GC.GetGeneration(xlApp); IntPtr t = new IntPtr(xlApp.Hwnd); //獲取句柄 int k = 0; GetWindowThreadProcessId(t, out k); //獲取進(jìn)程唯一標(biāo)志 System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); p.Kill(); //關(guān)閉進(jìn)程 }
2. 使用NPOI
地址:https://github.com/tonyqus/npoi
在不安裝office的時(shí)候也是可以讀寫的,速度很快,從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);//獲取第一個(gè)工作薄 IRow row = (IRow)sheet.GetRow(0);//獲取第一行 //設(shè)置第一行第一列值,更多方法請(qǐng)參考源官方Demo row.CreateCell(0).SetCellValue("test");//設(shè)置第一行第一列值 //導(dǎo)出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);//這個(gè)庫(kù)也是從1開始 //設(shè)置第一行第一列值,更多方法請(qǐng)參考官方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分免費(fèi)和收費(fèi),無特殊需求用免費(fèi)即可
從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數(shù)據(jù),更多請(qǐng)參考官方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
沒用過這個(gè),暫時(shí)就不做介紹了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Unity UGUI的Toggle復(fù)選框組件使用詳解
這篇文章主要為大家介紹了Unity UGUI的Toggle復(fù)選框組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印?
這篇文章主要介紹了C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印,水印是指在 Word 文檔的背景中以淡色或灰色顯示的文本或圖像。文章圍繞主題展開介紹,需要的朋友可以參考一下2022-08-08C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì)(1)
這篇文章主要介紹了C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì),獻(xiàn)上了9個(gè)類的設(shè)計(jì),需要的朋友可以參考下2015-11-11C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則的示例詳解
在面向?qū)ο缶幊讨?,SOLID?是五個(gè)設(shè)計(jì)原則的首字母縮寫,旨在使軟件設(shè)計(jì)更易于理解、靈活和可維護(hù)。本文將通過實(shí)例詳細(xì)講講C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則,需要的可以參考一下2022-07-07C#使用TreeView控件實(shí)現(xiàn)的二叉樹泛型節(jié)點(diǎn)類及其方法
TreeView?控件在?C#?中主要用于顯示分層結(jié)構(gòu)的數(shù)據(jù),這通常是一個(gè)文件系統(tǒng)的表示,但也可以是任何具有父子關(guān)系的數(shù)據(jù)集合,本文給大家介紹了C#使用TreeView控件實(shí)現(xiàn)的二叉樹泛型節(jié)點(diǎn)類及其方法,需要的朋友可以參考下2024-03-03Unity中 ShaderGraph 實(shí)現(xiàn)旋渦傳送門效果入門級(jí)教程(推薦)
通過Twirl 旋轉(zhuǎn)節(jié)點(diǎn)對(duì)Gradient Noise 梯度噪聲節(jié)點(diǎn)進(jìn)行操作,就可得到一個(gè)旋轉(zhuǎn)的旋渦效果。具體實(shí)現(xiàn)代碼跟隨小編一起通過本文學(xué)習(xí)下吧2021-07-07C# GetMethod方法的應(yīng)用實(shí)例講解
GetMethod 是獲取當(dāng)前 Type 的特定方法,具有多個(gè)重載, GetMethod 即使用指定的綁定約束搜索指定方法,本文給大家介紹了C# GetMethod方法的應(yīng)用實(shí)例,需要的朋友可以參考下2024-04-04