Skip to main content
 首页 » 编程设计

c#之写入多个 Excel 工作表时不使用多线程的原因

2024年02月06日11tintown

我正在处理我取代的开发人员编写的一些代码。他写了一段很长的代码,将写入同一个 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++; 
        } 

请您参考如下方法:

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