写入多个excel工作表时没有多线程的原因

时间:2014-07-22 16:04:59

标签: c# excel backgroundworker

我正在研究我替换的开发人员编写的一些代码。他写了一段冗长的代码,在同一个excel文件中写入多个excel工作表。我正在考虑使用几个后台工作程序来加快写入四个excel工作表的过程。是否有理由将所有这些留在一个线程上是个好主意?我之前使用过多线程,但不是在c#中而是写入excel。我找不到任何文件。

这是代码

        xlWorkSheet = xlWorkBook.Worksheets.get_Item(1);

        // work order
        xlWorkSheet.Cells[4, 4] = nld.s_WorkOrderNumber;
        // technician
        xlWorkSheet.Cells[6, 4] = nld.s_TechnicianName;
        // date and time
        xlWorkSheet.Cells[4, 10] = (string)DateTime.Now.ToShortDateString();
        xlWorkSheet.Cells[6, 10] = (string)DateTime.Now.ToShortTimeString();

        row = 30;
        col = 1;
        // left connectors and part number
        conCount = nld.n_LeftConnCount;

        for (i = 0; i < conCount; i++)
        {
            xlWorkSheet.Cells[row, col] = "Name: " + nld.ConnDataLeft[i].s_ConnName + "  PartNo: " + nld.ConnDataLeft[i].s_ConnPartNumber;
            row++;
        }

        // Right connectors and part number
        row = 30;
        col = 7;
        conCount = nld.n_RightConnCount;

        for (i = 0; i < conCount; i++)
        {
            xlWorkSheet.Cells[row, col] = "Name: " + nld.ConnDataRight[i].s_ConnName + "  PartNo: " + nld.ConnDataRight[i].s_ConnPartNumber;
            row++;
        }

        // put down the pin map onNetlist worksheet
        xlWorkSheet = xlWorkBook.Worksheets.get_Item(2);
        row = 5;
        col = 1;
        i = 0;
        leftPinNum = 0;

        int connCount = nld.pinMap.Count;

        for(i = 0; i < connCount; i++)
        {
            xlWorkSheet.Cells[row, col] = (i+1).ToString();
            leftPinNum = nld.pinMap[i].pinLeft;
            xlWorkSheet.Cells[row, col + 1] = nld.pinMap[i].conLeftName;
            xlWorkSheet.Cells[row, col + 2] = nld.pinMap[i].pinLeftName;
            xlWorkSheet.Cells[row, col + 4] = nld.pinMap[i].conRightName;
            xlWorkSheet.Cells[row, col + 5] = nld.pinMap[i].pinRightName;

            row++;
        }


        // put down the pin map onNetlist worksheet
        xlWorkSheet = xlWorkBook.Worksheets.get_Item(3);
        row = 5;
        col = 1;
        i = 0;
        leftPinNum = 0;


        for (i = 0; i < connCount; i++)
        {
            xlWorkSheet.Cells[row, col] = (i + 1).ToString();
            leftPinNum = nld.pinMap[i].pinLeft;
            xlWorkSheet.Cells[row, col + 1] = nld.pinMap[i].conLeftName;
            xlWorkSheet.Cells[row, col + 2] = nld.pinMap[i].pinLeftName;
            xlWorkSheet.Cells[row, col + 4] = nld.pinMap[i].conRightName;
            xlWorkSheet.Cells[row, col + 5] = nld.pinMap[i].pinRightName;
            if (facadeIntoNetList.ReturnIfUseShort(i))
            {
                xlWorkSheet.Cells[row, col + 7] = "True";
            }
            else
            {
                xlWorkSheet.Cells[row, col + 9] = "True";
            }
            row++;
        }

        // put down the pin map onNetlist worksheet
        xlWorkSheet = xlWorkBook.Worksheets.get_Item(4);
        row = 5;
        col = 1;
        i = 0;
        leftPinNum = 0;


        for (i = 0; i < connCount; i++)
        {
            xlWorkSheet.Cells[row, col] = (i + 1).ToString();
            leftPinNum = nld.pinMap[i].pinLeft;
            xlWorkSheet.Cells[row, col + 1] = nld.pinMap[i].conLeftName;
            xlWorkSheet.Cells[row, col + 2] = nld.pinMap[i].pinLeftName;
            xlWorkSheet.Cells[row, col + 6] = nld.pinMap[i].conRightName;
            xlWorkSheet.Cells[row, col + 7] = nld.pinMap[i].pinRightName;
            row++;
        }

1 个答案:

答案 0 :(得分:4)

我知道这样做的诱惑:那些Office COM接口非常缓慢。但他们也根本不支持多线程。它不是C#问题,它是Excel + COM问题。如果您需要速度,请使用第三方库编写.xlsx,然后启动Excel以打开该文件。这可能会快几百倍。