-
C#教程之详解C#读写Excel的几种方法
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
1 使用Office自带的库
前提是本机须安装office才能运行,且不同的office版本之间可能会有兼容问题,从Nuget下载 Microsoft.Office.Interop.Excel
读写代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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
读写代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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
读写代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
读写代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
没用过这个,暂时就不做介绍了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日