在写入多个Excel工作表时,不要multithreading的原因

我正在研究一些我replace的开发人员编写的代码。 他写了一段冗长的代码,写在同一个excel文件上的多个excel工作表中。 我正在考虑使用几个后台工作来加快写入四个Excel工作表的过程。 会有一个原因,把这一切留在一条线上是一个好主意吗? 我之前使用过multithreading,但在c#中没有写入excel。 我无法find任何文件。

这是代码

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接口非常缓慢。 但是他们也根本不支持multithreading。 这不是一个C#的问题,这是一个Excel + COM的问题。 如果您需要速度,请使用第三方库编写.xlsx文件,然后启动Excel打开文件。 这实际上可能快上百倍。