【发布时间】:2016-04-08 18:18:09
【问题描述】:
我主要是从文本文件中解析大量文本,然后将其填充到 excel 中。
//populate into worksheet
for (int x = 0; x < rawLine.Length; x++)
{
string[] tempLine = rawLine[x].Split(';');
for (int y = 0; y < tempLine.Length; y++)
{
DateTime hour = Convert.ToDateTime(tempLine[6]);
xlWorkSheet.Cells[y + 2, 1] = tempLine[0];
xlWorkSheet.Cells[y + 2, 2] = tempLine[1];
xlWorkSheet.Cells[y + 2, 3] = tempLine[2];
xlWorkSheet.Cells[y + 2, 4] = tempLine[3];
xlWorkSheet.Cells[y + 2, 5] = tempLine[4];
xlWorkSheet.Cells[y + 2, 6] = tempLine[5];
xlWorkSheet.Cells[y + 2, 7] = tempLine[6];
xlWorkSheet.Cells[y + 2, 8] = tempLine[7];
xlWorkSheet.Cells[y + 2, 9] = tempLine[8];
xlWorkSheet.Cells[y + 2, 10] = tempLine[9];
xlWorkSheet.Cells[y + 2, 11] = tempLine[10];
xlWorkSheet.Cells[y + 2, 12] = hour.Hour;
xlWorkSheet.Cells[y + 2, 13] = tempLine[8] == "0" ? "SAME" : tempLine[9];
}
Console.WriteLine("Current line = " + x + "\n");
}
目前,此代码有效,但耗时太长。有没有办法加快速度?我进行了一些搜索,但没有发现任何具体的内容。
提前致谢。
【问题讨论】:
-
不是一个一个地设置每个单元格的值,而是获取一个 Range 并将它的值设置为一个数组,看看这里:stackoverflow.com/questions/536636/write-array-to-excel-range
-
好的去看看 谢谢回复
-
是的,古斯曼就在这里。这是 VBA 中用于解决此问题的常用技术...
-
@Gusman 考虑将您的评论推广到答案。
标签: c# excel for-loop optimization