C#.NET操作Excel高效低內(nèi)存的開源框架 - 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")] // 系統(tǒng)會自動轉(zhuǎn)換"I"為第8列 10. public string Test4 { get; set; } 11. public string Test5 { get; } //系統(tǒng)會忽略此列 12. public string Test6 { get; private set; } //set非公開,系統(tǒng)會忽略 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 編輯過 |
關鍵字查詢
相關文章
正在查詢... |