Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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# 获得;保存文件时出错";检测到消息(或)System.OutOfMemoryException(或)ContextSwitchDeadlock_C#_Excel - Fatal编程技术网

C# 获得;保存文件时出错";检测到消息(或)System.OutOfMemoryException(或)ContextSwitchDeadlock

C# 获得;保存文件时出错";检测到消息(或)System.OutOfMemoryException(或)ContextSwitchDeadlock,c#,excel,C#,Excel,我用C#开发了一个Windows窗体应用程序 应用程序将读取Excel文件,将该值存储到数据表中,进行一些处理,并将处理后的信息写入新的Excel文件 写入

我用C#开发了一个Windows窗体应用程序

应用程序将读取Excel文件,将该值存储到数据表中,进行一些处理,并将处理后的信息写入新的Excel文件

写入<400行时没有问题,如果大于400,则应用程序会抛出异常
保存文件时出错
系统。检测到OutOfMemoryException
ContextSwitchDeadlock

<pre>ExcelWorksheet worksheetToWrite = packageToWrite1.Workbook.Worksheets["Lead Owners"];

     for (int i = 0; i < dtSalesPersonSingleSB.Columns.Count; i++)
     {
       worksheetToWrite.Cells[1, (i + 1)].Value = dtSalesPersonSingleSB.Columns[i].ColumnName; //dtSalesPersonSingle.Columns[i].ColumnName;
     }

     for (int i = 0; i < dtSalesPersonSingleSB.Rows.Count; i++)
     {
     // to do: format datetime values before printing
      for (int j = 0; j < dtSalesPersonSingleSB.Columns.Count; j++)
      {
        worksheetToWrite.Cells[(i + 2), (j + 1)].Value = dtSalesPersonSingleSB.Rows[i][j];
      }
     }
     packageToWrite1.Save();
同时显示以下错误(在visual studio窗口中):
CLR已经60秒无法从COM上下文0x592b88转换到COM上下文0x592cf8。拥有目标上下文/单元的线程很可能正在执行非泵送等待或在不泵送Windows消息的情况下处理长时间运行的操作。这种情况通常会对性能产生负面影响,甚至可能导致应用程序变得无响应或内存使用量随时间不断累积。为避免此问题,所有单线程单元(STA)线程应使用泵送等待原语(如CoWaitForMultipleHandles),并在长时间运行操作期间定期泵送消息。

worksheetToWrite.Cells["A" + Iteration1].Value = sMailID2.Trim();
worksheetToWrite.Cells["B" + Iteration1].Value = intCurrentBatchSize.ToString();

if (sFull1.Length <= 4000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, sFull1.Length);
    worksheetToWrite.Cells["D" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["E" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["F" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["G" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 4000 && sFull1.Length <= 8000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
    worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, sFull1.Length - 4000);
    worksheetToWrite.Cells["E" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["F" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["G" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 8000 && sFull1.Length <= 12000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
    worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
    worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, sFull1.Length - 8000);
    worksheetToWrite.Cells["F" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["G" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 12000 && sFull1.Length <= 16000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
    worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
    worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
    worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, sFull1.Length - 12000);
    worksheetToWrite.Cells["G" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 16000 && sFull1.Length <= 20000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
    worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
    worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
    worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
    worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, sFull1.Length - 16000);
    worksheetToWrite.Cells["H" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 20000 && sFull1.Length <= 24000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
    worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
    worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
    worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
    worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
    worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, sFull1.Length - 20000);
    worksheetToWrite.Cells["I" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 24000 && sFull1.Length <= 28000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
    worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
    worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
    worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
    worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
    worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, 4000);
    worksheetToWrite.Cells["I" + Iteration1].Value = sFull1.Substring(24000, sFull1.Length - 24000);
    worksheetToWrite.Cells["J" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 28000 && sFull1.Length <= 32000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
    worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
    worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
    worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
    worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
    worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, 4000);
    worksheetToWrite.Cells["I" + Iteration1].Value = sFull1.Substring(24000, 4000);
    worksheetToWrite.Cells["J" + Iteration1].Value = sFull1.Substring(28000, sFull1.Length - 28000);
    worksheetToWrite.Cells["K" + Iteration1].Value = string.Empty;
    worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 32000 && sFull1.Length <= 36000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
    worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
    worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
    worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
    worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
    worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, 4000);
    worksheetToWrite.Cells["I" + Iteration1].Value = sFull1.Substring(24000, 4000);
    worksheetToWrite.Cells["J" + Iteration1].Value = sFull1.Substring(28000, 4000);
    worksheetToWrite.Cells["K" + Iteration1].Value = sFull1.Substring(32000, sFull1.Length - 32000);
    worksheetToWrite.Cells["L" + Iteration1].Value = string.Empty;
}
else if (sFull1.Length > 36000 && sFull1.Length <= 40000)
{
    worksheetToWrite.Cells["C" + Iteration1].Value = sFull1.Substring(0, 4000);
    worksheetToWrite.Cells["D" + Iteration1].Value = sFull1.Substring(4000, 4000);
    worksheetToWrite.Cells["E" + Iteration1].Value = sFull1.Substring(8000, 4000);
    worksheetToWrite.Cells["F" + Iteration1].Value = sFull1.Substring(12000, 4000);
    worksheetToWrite.Cells["G" + Iteration1].Value = sFull1.Substring(16000, 4000);
    worksheetToWrite.Cells["H" + Iteration1].Value = sFull1.Substring(20000, 4000);
    worksheetToWrite.Cells["I" + Iteration1].Value = sFull1.Substring(24000, 4000);
    worksheetToWrite.Cells["J" + Iteration1].Value = sFull1.Substring(28000, 4000);
    worksheetToWrite.Cells["K" + Iteration1].Value = sFull1.Substring(32000, 4000);
    worksheetToWrite.Cells["L" + Iteration1].Value = sFull1.Substring(36000, sFull1.Length - 36000);
}
worksheetToWrite.Cells[“A”+Iteration1].Value=smaild2.Trim();
worksheetToWrite.Cells[“B”+Iteration1].Value=intCurrentBatchSize.ToString();

如果(sFull1.Length 4000&&sFull1.Length 8000&&sFull1.Length 12000&&sFull1.Length 16000&&sFull1.Length 20000&&sFull1.Length 24000&&sFull1.Length 28000&&sFull1.Length 32000&&sFull1.Length 36000&&sFull1.Length以前我在每次迭代中都保存Excel文件。现在我创建了一个新的数据表,整合了所有信息数据表中的信息(写入Excel)。因此,在写入之前,所有迭代都已在此处完成。在将所有内容写入工作簿后,我将保存它

它现在运行良好,在处理大量数据时没有问题

代码:

ExcelWorksheet worksheetToWrite=packageToWrite1.工作簿.Worksheets[“主要所有者”];
for(int i=0;i

感谢您的支持,以实现预期结果。

您的数据表是固定行吗?否。我已从Excel中读取多达3500行并将其存储到数据表中。读取时没有问题。错误仅在写入时发生。此外,现在我正在获取System.OutOfMemoryException您可以发布完整的错误消息吗?
System.OutOfMemoryException在哪一行?为什么要写
string.Empty;
?不能省略这些代码行吗?