【问题标题】:C# How to improve loop populating Excel cellsC#如何改进循环填充Excel单元格
【发布时间】: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


【解决方案1】:

这可能是一个很小的改进,但是这一行:

DateTime hour = Convert.ToDateTime(tempLine[6]);

应该移到y 循环之外,因为它不依赖它。

除此之外,您可能应该研究一些同时设置多个单元格的方法——大部分时间可能都花在往返于 Excel 上。 (看起来这就是@Gusman 在 cmets 中的建议)。

@Mohit 的回答也很好,因为它更短更简单。

【讨论】:

    【解决方案2】:

    你可以试试:

    //populate into worksheet
    DateTime hour;
    string[] tempLine;
    StringBuilder output = new StringBuilder();
    for (int x = 0; x < rawLine.Length; x++)
    {
        tempLine = rawLine[x].Split(';');    
        for (int y = 0; y < tempLine.Length; y++)
        {
            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];
        }    
        output.AppendLine("Current line = " + x);
    }
    Console.WriteLine(output.ToString());
    

    【讨论】:

      【解决方案3】:

      可能只是为了改进你可以这样写的循环。这不会提高性能,但看起来会更干净。

      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]);
              for(int z=0; z<11; z++)
              {
                  xlWorkSheet.Cells[y + 2, (z+1)] = tempLine[z];
              }
              xlWorkSheet.Cells[y + 2, 12] = hour.Hour;
              xlWorkSheet.Cells[y + 2, 13] = tempLine[8] == "0" ? "SAME" : tempLine[9];
          }
          Console.WriteLine("Current line = " + x + "\n");
      }
      

      【讨论】:

      • 因为你有一个内部 for 循环,所以循环总是比直接代码慢。
      • @Gusman 仅供参考。您关于循环的信息是错误的。它与编写直接代码一样等效。循环“内部”的代码被遵守指定的次数,或者对每个项目集合执行一次,或者直到满足某些条件。
      • 仅供参考:您对编程一无所知,for 循环每次迭代至少比展开代码多两条指令,每次迭代操作(在本例中为 y ++)和 per-迭代检查(int 这种情况下 z
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多