I am working on a Excel VSTO project with C#. Excel will auto-format codes like "SEP-1" to a date, which is not what is supposed to happen. Is there a way to turn off this feature on a spreadsheet in code? Thanks!
2 Answers
While it seems there is no way to turn it off, the best solution I find is to set the cell's NumberFormat to Text to prevent Excel to auto-format it.
You can prefix the text value with a single quote.
The following snippet of code is provided as pseudo-code to see how it can be done in c#.
Excel.Range vsto_cell = vsto_worksheet.Cells[row_index + 1, col_index + 1];
if( ( ssg_cell.NumberFormatType == SpreadsheetGear.NumberFormatType.Date )
|| ( ssg_cell.NumberFormatType == SpreadsheetGear.NumberFormatType.DateTime )
|| ( ssg_cell.NumberFormatType == SpreadsheetGear.NumberFormatType.Time ) )
{
vsto_cell.Value2 = ssg_cell.Text; //date should be recognized by Excel as a date
}
else if( ssg_cell.ValueType == SpreadsheetGear.ValueType.Text )
{
vsto_cell.Value2 = "'" + ssg_cell.Text;
}
else
vsto_cell.Value2 = ssg_cell.Value;