Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Openxml时System.Threading.Tasks.Parallel.For出现意外结果_C#_Multithreading_Openxml - Fatal编程技术网

C# 使用Openxml时System.Threading.Tasks.Parallel.For出现意外结果

C# 使用Openxml时System.Threading.Tasks.Parallel.For出现意外结果,c#,multithreading,openxml,C#,Multithreading,Openxml,我正在使用以下代码 SheetData sheetdata = worksheet.Worksheet.GetFirstChild<SheetData>(); System.Threading.Tasks.Parallel.For((excelStartRow + 1), (endRowvalue + 1), (i) => { Row contentRow = new Row(); //do something Cell cont

我正在使用以下代码

SheetData sheetdata = worksheet.Worksheet.GetFirstChild<SheetData>();
System.Threading.Tasks.Parallel.For((excelStartRow + 1), (endRowvalue + 1),
  (i) =>
  {
      Row contentRow = new Row();

      //do something

      Cell content = createTextCell(
          dtspSheetData.Tables[0].Columns.IndexOf(column) + excelStartColumn, i,
          dtspSheetData.Tables[0].Rows[j][column.ColumnName],
          formatCount, 1, cellFormatCount);
      contentRow.AppendChild(content);

      sheetData.AppendChild(contentRow);
  });
SheetData SheetData=worksheet.worksheet.GetFirstChild();
System.Threading.Tasks.Parallel.For((excelStartRow+1),(endRowvalue+1),
(i) =>
{
行内容行=新行();
//做点什么
单元格内容=createTextCell(
dtspSheetData.Tables[0]。Columns.IndexOf(column)+excelStartColumn,i,
dtspSheetData.Tables[0]。行[j][column.ColumnName],
formatCount,1,cellFormatCount);
contentRow.AppendChild(内容);
sheetData.AppendChild(contentRow);
});

我试图在使用OpenXML创建Excel时实现Parallelism。问题是在Excel文件中创建了行,但有些行没有创建,并且在打开时显示警告。我知道,当其他线程尝试将行插入sheetData对象时,sheetData对象被锁定。如何纠正此问题。

这些Excel对象不是设计为同时从多个线程访问的。考虑到您在这个循环中执行的唯一一个特别“昂贵”的任务是必须序列化的任务,您最好的选择是在一个线程中,在一个常规的
for
循环中完成整个任务


如果实际计算每个单元格的值是昂贵的,那么你可以考虑并行循环来简单地生成每个单元格的内容,将它们放入内存中的中间存储位置,然后将这些值从单个线程复制到Excel中,但是,由于这个循环似乎没有进行任何如此昂贵的计算,因此这里没有必要这样做。

它在不使用并行的情况下工作。例如?是的。它在不使用并行的情况下工作。您应该检查所有方法是否都是线程安全的。例如,createTextCell方法是否线程安全?createTextCell方法不执行任何写入操作。只创建了一个单元格对象。问题是整行无法附加到工作表中。谢谢。线程尝试访问中间存储位置时,中间存储位置被锁定,因此,只添加了几行。问题在于,无论从何处读取数据,都无法访问数据。问题在于,您试图从多个线程将数据写入excel文件,而excel文件不支持这一点。您需要按顺序将数据写入excel。唯一有意义的并行化(取决于您正在做什么)是填充从这里提取数据的数据源。