c# - 将大对象数据导出到Excel文件
我的程序通过使用 LINQ 选择数据来从不同的 XML 文件收集数据。 结果是我的对象集合的每个元素都具有以下属性。 每个馆藏约有 10k 件物品(包含 11 个属性),15 个馆藏。
https://i.sstatic.net/5R1Hj.jpg
一切都很好,直到我需要导出他们到Excel文件。 我如下循环每个单元格数据,那么这太长了。 如上所述,有大量数据需要导出。
foreach (dataContainer elt in Liste)
{
for(int b = 1; b <= 11; b++)
{
Excel.Range currentRange = outputWorksheet.Cells[Liste.IndexOf(elt)+2, b];
switch (b)
{
case 1: currentRange.Value2 = elt.ctNumber;break;
case 2: currentRange.Value2 = elt.isoType; break;
case 3: currentRange.Value2 = elt.bkgNumber; break;
case 4: currentRange.Value2 = elt.POO; break;
case 5: currentRange.Value2 = elt.POL; break;
case 6: currentRange.Value2 = elt.POD; break;
case 7: currentRange.Value2 = elt.OPE; break;
case 8: currentRange.Value2 = elt.Empty; break;
case 9: currentRange.Value2 = elt.flagOOG; break;
case 10: currentRange.Value2 = elt.teus; break;
case 11: currentRange.Value2 = elt.weight; break;
}
}
}
如何改进在 Excel 中导出数据的方式?知道我无法使用 .csv 文件。
my program gather data from different XML files by selecting data with LINQ.
It results having below proprieties for each element of my object collections.
Each collection having arround 10k objects (with 11 proprieties), 15 collection.
https://i.sstatic.net/5R1Hj.jpg
Everything's fine until i need to export them to excel file.
I'm looping each cell data as below, then this is far too long.
As describe above, there is large data to export.
foreach (dataContainer elt in Liste)
{
for(int b = 1; b <= 11; b++)
{
Excel.Range currentRange = outputWorksheet.Cells[Liste.IndexOf(elt)+2, b];
switch (b)
{
case 1: currentRange.Value2 = elt.ctNumber;break;
case 2: currentRange.Value2 = elt.isoType; break;
case 3: currentRange.Value2 = elt.bkgNumber; break;
case 4: currentRange.Value2 = elt.POO; break;
case 5: currentRange.Value2 = elt.POL; break;
case 6: currentRange.Value2 = elt.POD; break;
case 7: currentRange.Value2 = elt.OPE; break;
case 8: currentRange.Value2 = elt.Empty; break;
case 9: currentRange.Value2 = elt.flagOOG; break;
case 10: currentRange.Value2 = elt.teus; break;
case 11: currentRange.Value2 = elt.weight; break;
}
}
}
How can i improve my way to export data in excel ? Knowing i can't use .csv file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要创建独立的 Excel 文件而不是更新当前显示的文件,则使用 Open XML SDK 而不是 Excel 对象模型直接创建文件会更快 - 从 https://learn.microsoft.com/en-us/office/open-xml/spreadsheets
If you are creating a standalone Excel file instead of updating the one currently being displayed, directly creating the file using Open XML SDK rather than Excel Object Model would be faster - start at https://learn.microsoft.com/en-us/office/open-xml/spreadsheets
经过很少的研究,我发现了案件的最佳方法,即非常快的方法。
我创建一个将包含我的2D数据的对象,然后将Excel范围值分配给此对象。
After few research, i found out the best way to proceed for my case, very quick method.
I create a an object that will contains my 2D data, then assign an excel range value = to this object.