Shouldn't the column start with special characters?

0

I'm working with Excel sheet of .xls format and writing data into it. The problem is that whenever the data doesn't start with alphabets it is prompting me different errors.

For example: When my column data started with =?us-ascii?Q?Google Keywords?= it threw me an exception:

 Exception from HRESULT: 0x800A03EC  

and when my data is like -------- Original Message --------Subject: the error was:

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

This is how I'm writing data:

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

This is how I clean my objects:

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();
}

So I have tried omitting these data and they are working great.

Are there any rules that the column has to start with specific characters and if so, what are these?

c#
excel
excel-interop
asked on Stack Overflow Jun 3, 2015 by Coder • edited Jun 4, 2015 by pnuts

1 Answer

0

if you type "?" in excel cell that has general format, the excel automaticall interprete that as a formula. You need to change the cell format before inserting the text into the cell.

something like:

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++;
   }
answered on Stack Overflow Jun 3, 2015 by Jegan • edited Jun 3, 2015 by Jegan

User contributions licensed under CC BY-SA 3.0