详解C#读写Excel的几种方法
作者:jqw2009 时间:2022-10-23 14:23:28
1 使用Office自带的库
前提是本机须安装office才能运行,且不同的office版本之间可能会有兼容问题,从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";
//创建
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);
//处理数据过程,更多操作方法自行百度
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);
//关闭Excel进程
ClosePro(xlApp, xlsWorkBook);
}
public void ClosePro(Excel.Application xlApp, Excel.Workbook xlsWorkBook)
{
if (xlsWorkBook != null)
xlsWorkBook.Close(true, Type.Missing, Type.Missing);
xlApp.Quit();
// 安全回收进程
System.GC.GetGeneration(xlApp);
IntPtr t = new IntPtr(xlApp.Hwnd); //获取句柄
int k = 0;
GetWindowThreadProcessId(t, out k); //获取进程唯一标志
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill(); //关闭进程
}
2. 使用NPOI
地址:https://github.com/tonyqus/npoi
在不安装office的时候也是可以读写的,速度很快,从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);//获取第一个工作薄
IRow row = (IRow)sheet.GetRow(0);//获取第一行
//设置第一行第一列值,更多方法请参考源官方Demo
row.CreateCell(0).SetCellValue("test");//设置第一行第一列值
//导出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);//这个库也是从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下载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数据,更多请参考官方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
没用过这个,暂时就不做介绍了
来源:https://www.cnblogs.com/kenjiang/p/10790652.html
标签:C#,Excel,读写
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring boot validation参数校验实例分析
2023-02-03 02:50:38
springboot添加https服务器的方法
2022-08-19 06:14:31
C#模拟实现鼠标自动点击与消息发送功能
2023-02-09 08:51:14
![](https://img.aspxhome.com/file/2023/7/78157_0s.gif)
Android实现每天定时提醒功能
2021-10-18 09:52:33
C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能
2022-01-09 15:30:45
![](https://img.aspxhome.com/file/2023/5/122645_0s.png)
Java-String类最全汇总(上篇)
2023-04-15 11:47:10
![](https://img.aspxhome.com/file/2023/1/83571_0s.jpg)
Flyway详解及Springboot集成Flyway的详细教程
2022-09-21 10:37:49
![](https://img.aspxhome.com/file/2023/4/70304_0s.jpg)
Android SharePreferences与数据库SQLite存储实现方法介绍
2023-03-13 11:20:45
![](https://img.aspxhome.com/file/2023/6/120126_0s.png)
基于Java代码操作Redis过程详解
2022-03-28 06:53:58
![](https://img.aspxhome.com/file/2023/9/113259_0s.jpg)
SpringBoot+Hutool+thymeleaf完成导出Excel的实现方法
2023-09-05 17:39:10
![](https://img.aspxhome.com/file/2023/2/71682_0s.png)
Android 6.0区别U盘和SD卡设备的方法详解
2022-09-09 13:10:58
深入讲解SPI 在 Spring 中的应用
2022-10-21 03:44:55
![](https://img.aspxhome.com/file/2023/9/105909_0s.png)
java搭建ftp/sftp进行数据传递的全过程
2023-11-29 15:08:10
![](https://img.aspxhome.com/file/2023/6/60486_0s.png)
C#封装的Sqlite访问类实例
2022-04-28 15:38:13
Android选择图片或拍照图片上传到服务器
2022-12-15 01:48:41
Android 通知栏的使用方法
2022-11-12 20:00:29
C#中Equals方法的常见误解
2023-03-10 21:59:23
java中mybatis和hibernate的用法总结
2022-08-23 07:24:38
C#使用Task实现异步方法
2022-09-02 20:26:53
![](https://img.aspxhome.com/file/2023/6/70436_0s.jpg)
JAVA发送HTTP请求的四种方式总结
2023-08-23 20:27:23