1 MiniExcel
简单、高效避免OOM的.NET处理Excel工具。具体可以进入官网了解。
2 做什么?
有如下表格:
需要将表格B列,D列,按照类别输出到不同的excel中,如:将性别为男的所有数据输出到一个excel中。
3 如何做?
1 通过nuget添加依赖
2 上代码:
string path = "G:\\桌面\\测试excel.xlsx";
string startcell = "2";
List<string> coldistinct = new List<string>() { "B", "D" };
var rows = MiniExcel.Query(path, startCell: "A" + startcell);
Dictionary<string, List<IDictionary<string, object>>> keysData = new Dictionary<string, List<IDictionary<string, object>>>();
foreach(IDictionary<string, object> row in rows)
{
if(row["A"] != null)
{
foreach(string col in coldistinct)
{
if (keysData.ContainsKey(col + row[col].ToString()))
{
keysData[col + row[col].ToString()].Add(row);
}
else
{
keysData.Add(col + row[col].ToString(), new List<IDictionary<string, object>>() { row });
}
}
}
}
var savePath = "G:\\桌面\\SaveXlsx";
foreach(var data in keysData)
{
savePath = Path.Combine(savePath, $"{ data.Key }.xlsx");
MiniExcel.SaveAs(savePath, data.Value);
savePath = "G:\\桌面\\SaveXlsx";
}
3.部分结果展示:
4 思路:
a.通过:MiniExcel.Query方法读取excel
b.定义变量,循环读出的数据并分类:
Dictionary<string, List<IDictionary<string, object>>> keysData = new Dictionary<string, List<IDictionary<string, object>>>();
foreach(IDictionary<string, object> row in rows)
c.读取已分类的数据keysData,分别写入不同的文件。
4 备注
代码仅进行简单的功能实现,有细节部分没有完善,可根据自己的实际情况修改即可。