public class UserAccount
{ public Guid ID { get; set; } public string Name { get; set; } public DateTime BoD { get; set; } public int Age { get; set; } public bool VIP { get; set; } public decimal Points { get; set; }
}
var rows = MiniExcel.Query<UserAccount>(path);// orusing (var stream = File.OpenRead(path))
var rows = stream.Query<UserAccount>();
图片
Query 查询 Excel 返回Dynamic IEnumerable 数据
var rows = MiniExcel.Query(path).ToList();// or using (var stream = File.OpenRead(path)){
var rows = stream.Query().ToList();
Assert.Equal("MiniExcel", rows[0].A);
Assert.Equal(1, rows[0].B);
Assert.Equal("Github", rows[1].A);
Assert.Equal(2, rows[1].B);}
支持集合<匿名类别>或是<强型别>
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");MiniExcel.SaveAs(path, new[] {
new { Column1 = "MiniExcel", Column2 = 1 },
new { Column1 = "Github", Column2 = 2}
});
IEnumerable<IDictionary<string, object>>
var values = new List<Dictionary<string, object>>(){
new Dictionary<string,object>{{ "Column1", "MiniExcel" }, { "Column2", 1 } },
new Dictionary<string,object>{{ "Column1", "Github" }, { "Column2", 2 } }
};MiniExcel.SaveAs(path, values);
图片
IDataReader
推荐使用,可以避免载入全部数据到内存.
MiniExcel.SaveAs(path, reader);
图片
推荐 DataReader 多表格导出方式(建议使用 Dapper executeReader )
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);}