详解C#读写Excel的几种方法
1使用Office自带的库
前提是本机须安装office才能运行,且不同的office版本之间可能会有兼容问题,从Nuget下载Microsoft.Office.Interop.Excel
读写代码如下:
usingMicrosoft.Office.Interop.Excel;
usingExcel=Microsoft.Office.Interop.Excel;
privatevoidbtn_Office_Click(objectsender,EventArgse)
{
stringimportExcelPath="E:\\import.xlsx";
stringexportExcelPath="E:\\export.xlsx";
//创建
Excel.ApplicationxlApp=newExcel.Application();
xlApp.DisplayAlerts=false;
xlApp.Visible=false;
xlApp.ScreenUpdating=false;
//打开Excel
Excel.WorkbookxlsWorkBook=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);
//处理数据过程,更多操作方法自行百度
Excel.Worksheetsheet=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);
//关闭Excel进程
ClosePro(xlApp,xlsWorkBook);
}
publicvoidClosePro(Excel.ApplicationxlApp,Excel.WorkbookxlsWorkBook)
{
if(xlsWorkBook!=null)
xlsWorkBook.Close(true,Type.Missing,Type.Missing);
xlApp.Quit();
//安全回收进程
System.GC.GetGeneration(xlApp);
IntPtrt=newIntPtr(xlApp.Hwnd);//获取句柄
intk=0;
GetWindowThreadProcessId(t,outk);//获取进程唯一标志
System.Diagnostics.Processp=System.Diagnostics.Process.GetProcessById(k);
p.Kill();//关闭进程
}
2.使用NPOI
地址:https://github.com/tonyqus/npoi
在不安装office的时候也是可以读写的,速度很快,从Nuget下载NPOI
读写代码如下:
usingSystem.IO;
usingNPOI;
usingNPOI.SS.UserModel;
privatevoidbtn_NPOI_Click(objectsender,EventArgse)
{
stringimportExcelPath="E:\\import.xlsx";
stringexportExcelPath="E:\\export.xlsx";
IWorkbookworkbook=WorkbookFactory.Create(importExcelPath);
ISheetsheet=workbook.GetSheetAt(0);//获取第一个工作薄
IRowrow=(IRow)sheet.GetRow(0);//获取第一行
//设置第一行第一列值,更多方法请参考源官方Demo
row.CreateCell(0).SetCellValue("test");//设置第一行第一列值
//导出excel
FileStreamfs=newFileStream(exportExcelPath,FileMode.Create,FileAccess.ReadWrite);
workbook.Write(fs);
fs.Close();
}
3.使用ClosedXml
地址:https://github.com/ClosedXML/ClosedXML
从Nuget下载ClosedXml
读写代码如下:
usingClosedXML;
usingClosedXML.Excel;
privatevoidbtn_ClosedXML_Click(objectsender,EventArgse)
{
stringimportExcelPath="E:\\import.xlsx";
stringexportExcelPath="E:\\export.xlsx";
varworkbook=newXLWorkbook(importExcelPath);
IXLWorksheetsheet=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下载FreeSpire.xlsFor.NET
读写代码如下:
usingSpire.Xls;
privatevoidbtnSpire_Click(objectsender,EventArgse)
{
stringimportExcelPath="E:\\import.xlsx";
stringexportExcelPath="E:\\export.xlsx";
Spire.Xls.Workbookworkbook=newSpire.Xls.Workbook();
workbook.LoadFromFile(importExcelPath);
//处理Excel数据,更多请参考官方Demo
Spire.Xls.Worksheetsheet=workbook.Worksheets[0];
sheet.Range[1,1].Text="test";//该方法也是从1开始,非0
workbook.SaveToFile(exportExcelPath);
}
5.EPPLUS
地址:https://github.com/pruiz/EPPlus/tree/master/EPPlus
没用过这个,暂时就不做介绍了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。