当前位置: 首页> 健康> 知识 > 【C#】C#读写Excel文件

【C#】C#读写Excel文件

时间:2025/9/28 12:25:13来源:https://blog.csdn.net/qq_45306739/article/details/139529313 浏览次数:0次

1.工具库选择

使用EPPlus读取Excel文件,在visual studio2022中安装最新NuGet。

2.读文件测试

using OfficeOpenXml;
using OfficeOpenXml.Packaging.Ionic.Zip;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{internal class Program{static void Main(string[] args){ExcelPackage.LicenseContext = LicenseContext.NonCommercial;// Excel文件路径string filePath = "D:\\Users\\.xlsx";// 打开Excel文件using (var package = new ExcelPackage(new FileInfo(filePath))){// 获取第一个工作表var worksheet = package.Workbook.Worksheets[0];// 读取工作表内容for (int row = 1; row <= worksheet.Dimension.End.Row; row++){for (int col = 1; col <= worksheet.Dimension.End.Column; col++){Console.Write(worksheet.Cells[row, col].Text + "\t");}Console.WriteLine();}}Console.ReadKey();}}
}

3.写文件测试

using OfficeOpenXml;
using OfficeOpenXml.Packaging.Ionic.Zip;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{internal class Program{static void Main(string[] args){ExcelPackage.LicenseContext = LicenseContext.NonCommercial;using(var package = new ExcelPackage()){var worksheet = package.Workbook.Worksheets.Add("sheet1");worksheet.Cells[1, 1].Value = "1";worksheet.Cells[1, 2].Value = "2";string filepath = "D:\\Users\\59723\\Desktop\\222.xlsx";FileInfo fileInfo = new FileInfo(filepath);package.SaveAs(fileInfo);Console.WriteLine("Excel file created successfully!");}          Console.ReadKey();}}
}
关键字:【C#】C#读写Excel文件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: