Aaron
Aaron

Reputation: 4480

Reason to not multithread when writing to multiple excel worksheets

I am working on some code that a developer I replaced wrote. He wrote a lengthy piece of code what writes to multiple excel worksheets on the same excel file. I am thinking about using several background workers to speed up the process of writing to four excel worksheets. Would there be a reason why it would be a good idea to leave all this on one thread? I have used multi-threading before, but not in c# and not writing to excel. I could not find any documentation either way.

Here is the code

        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++;
        }

Upvotes: 1

Views: 116

Answers (1)

Moby Disk
Moby Disk

Reputation: 3861

I know the temptation to do this: Those Office COM interfaces are painfully slow. But they also don't support multithreading at all. It's not a C# issue, it is an Excel+COM issue. If you need speed, then write an .xlsx using a 3rd-party library then launch Excel to open the file. That might literally be hundreds of times faster.

Upvotes: 4

Related Questions