添加miniexcel引用
1,新建winform,插入 dataGridView1 控件,
2,新建一个 D:\测试文档.xlsx 文档,标题列为 class Test 的内容
程序运行时,会读取文件
保存文件,可以直接保存,或修改标题保存
using MiniExcelLibs; using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace TEST1215 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //读取文件,并显示在表格中 string path = @"D:\测试文档.xlsx"; var rows = MiniExcel.Query<Test>(path).ToList(); dataGridView1.DataSource = rows; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //保存文件 MiniExcel.SaveAs(@"D:\测试文档全部导出.xlsx", dataGridView1.DataSource, overwriteFile: true); //修改标题,保存文件 var list = (List<Test>)dataGridView1.DataSource; var res=new List<Test1>(); foreach (var item in list) { res.Add(new Test1 {编号 = item.Id, 名字 = item.Name, 职业 = item.Title }); } MiniExcel.SaveAs(@"D:\测试文档全部导出1.xlsx", res, overwriteFile: true); } } public class Test { public int Id { get; set; } public string Name { get; set; } public string Title { get; set; } } public class Test1 { public int 编号 { get; set; } public string 名字 { get; set; } public string 职业 { get; set; } } }
标签:set,string,get,C#,MINIEXCEL,文档,using,public From: https://www.cnblogs.com/hiyanhao/p/16995737.html