列不应该以特殊字符开头吗?

时间:2015-06-03 09:52:19

标签: c# excel excel-interop

我正在处理.xls格式的Excel工作表并将数据写入其中。问题是,只要数据不以字母开头,它就会提示我不同的错误。

例如:当我的列数据以 =?us-ascii?Q?Google关键字?= 开头时,它给了我一个例外:

 Exception from HRESULT: 0x800A03EC  

当我的数据像 --------原始消息--------主题:错误是:

Not enough storage is available to complete this operation. (Exception from HRESULT: 0x8007000E (E_OUTOFMEMORY))

这就是我写数据的方式:

 foreach (AllCasesReplies infoList in allCasesReplies)
  {
      n = 0;
      mWorkSheet.Cells[l + m, ++n] = infoList.id;
      mWorkSheet.Cells[l + m, ++n] = infoList.replies;
      m++;
   }

这就是我清理对象的方式:

private static void SaveAndCollecttheGarbage(Microsoft.Office.Interop.Excel.Application excelApp, string path, Microsoft.Office.Interop.Excel.Sheets sheet)
{
        excelApp.Columns.AutoFit();
        mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
        Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value);
        mWorkBook.Close(true, Missing.Value, Missing.Value);
        sheet = null;
        mWorkBook = null;
        excelApplication.Quit();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
}

所以我尝试省略这些数据并且它们工作得很好。

列是否必须以特定字符开头,如果有,这些是什么?

1 个答案:

答案 0 :(得分:0)

如果输入“?”在具有通用格式的excel单元格中,excel automaticall将其解释为公式。在将文本插入单元格之前,需要更改单元格格式。

类似的东西:

foreach (AllCasesReplies infoList in allCasesReplies)
  {
      n = 0;
      Microsoft.Office.Interop.Excel.Range range1 = mWorkSheet.Cells[l + m, ++n] as Range;
      range1.NumberFormat = "@";
      range1.Value2 = infoList.id;

      Microsoft.Office.Interop.Excel.Range range2 = mWorkSheet.Cells[l + m, ++n] as Range;
      range2.NumberFormat = "@";
      range2.Value2 = infoList.replies;

      m++;
   }