C#.NET操作Excel高效低内存的开源框架 - MiniExcel
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
特点性能比较、测试1、读/导入 Excel1. public class UserAccount 2. { 3. public Guid ID { get; set; } 4. public string Name { get; set; } 5. public DateTime BoD { get; set; } 6. public int Age { get; set; } 7. public bool VIP { get; set; } 8. public decimal Points { get; set; } 9. } 10. var rows = MiniExcel.Query<UserAccount>(path); 11. // or 12. using (var stream = File.OpenRead(path)) 13. var rows = stream.Query<UserAccount>(); 1. var row = MiniExcel.Query(path).First(); 2. Assert.Equal("HelloWorld", row.A); 3. // or 4. using (var stream = File.OpenRead(path)) 5. { 6. var row = stream.Query().First(); 7. Assert.Equal("HelloWorld", row.A); 8. } 1. var config = new OpenXmlConfiguration { EnableSharedStringCache = false }; 2. MiniExcel.Query(path,configuration: config) 1. var config = new OpenXmlConfiguration { SharedStringCacheSize=500*1024*1024 }; 2. MiniExcel.Query(path, configuration: config); var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx"); MiniExcel.SaveAs(path, new[] { new { Column1 = "MiniExcel", Column2 = 1 }, new { Column1 = "Github", Column2 = 2} }); · using (var cnn = Connection) · { · cnn.Open(); · var sheets = new Dictionary<string,object>(); · sheets.Add("sheet1", cnn.executeReader("select 1 id")); · sheets.Add("sheet2", cnn.executeReader("select 2 id")); · MiniExcel.SaveAs("Demo.xlsx", sheets); · } 1. // 1. By POCO 2. var value = new 3. { 4. Name = "Jack", 5. createDate = new DateTime(2021, 01, 01), 6. VIP = true, 7. Points = 123 8. }; 9. MiniExcel.SaveAsByTemplate(path, templatePath, value); 1. // 2. By Dictionary 2. var value = new Dictionary<string, object>() 3. { 4. ["Name"] = "Jack", 5. ["createDate"] = new DateTime(2021, 01, 01), 6. ["VIP"] = true, 7. ["Points"] = 123 8. }; 9. MiniExcel.SaveAsByTemplate(path, templatePath, value); 1. // 1. By POCO 2. var value = new 3. { 4. title = "FooCompany", 5. managers = new[] { 6. new {name="Jack",department="HR"}, 7. new {name="Loan",department="IT"} 8. }, 9. employees = new[] { 10. new {name="Wade",department="HR"}, 11. new {name="Felix",department="HR"}, 12. new {name="Eric",department="IT"}, 13. new {name="Keaton",department="IT"} 14. } 15. }; 16. MiniExcel.SaveAsByTemplate(path, templatePath, value);
1. public class ExcelAttributeDemo 2. { 3. [ExcelColumnName("Column1")] 4. public string Test1 { get; set; } 5. [ExcelColumnName("Column2")] 6. public string Test2 { get; set; } 7. [ExcelIgnore] 8. public string Test3 { get; set; } 9. [ExcelColumnIndex("I")] // 系统会自动转换"I"为第8列 10. public string Test4 { get; set; } 11. public string Test5 { get; } //系统会忽略此列 12. public string Test6 { get; private set; } //set非公开,系统会忽略 13. [ExcelColumnIndex(3)] // 从0开始索引 14. public string Test7 { get; set; } 15. }
1. var config = new OpenXmlConfiguration 2. { 3. DynamicColumns = new DynamicExcelColumn[] { 4. new DynamicExcelColumn("id"){Ignore=true}, 5. new DynamicExcelColumn("name"){Index=1,Width=10}, 6. new DynamicExcelColumn("createdate"){Index=0,Format="yyyy-MM-dd",Width=15}, 7. new DynamicExcelColumn("point"){Index=2,Name="Account Point"}, 8. } 9. }; 10. var path = PathHelper.GetTempPath(); 11. var value = new[] { new { id = 1, name = "Jack", createdate = new DateTime(2022, 04, 12) ,point = 123.456} }; 12. MiniExcel.SaveAs(path, value, configuration: config); Excel 类别自动判断
该文章在 2023/9/7 17:26:42 编辑过 |
关键字查询
相关文章
正在查询... |