0

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 2

0

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.

0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.