Excel interop - how to stop number (stored as text) being “evaluated”

前端 未结 4 1224
梦如初夏
梦如初夏 2021-01-13 09:24

I was wondering if anyone had come across the following problem and had any ideas on how to resolve it: I\'m exporting data from a C# application (.NET 3.5) to Excel (2003)

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-13 09:31

    The correct type is not General, because general will try to guess the correct type, which in this case is a numeric, but you have to specify it as text to not truncate the leading zeros.

    Try the following code:

    Range cell = ActiveWorksheet.Cells[1,1];
    //The @ is the indicator for Excel, that the content should be text
    const string textIndicator = "@";
    //Change the NumberFormat from 'General' to 'Text'
    cell.NumberFormat = textIndicator;
    //Set the Value
    cell.Value2 = "000123";
    

提交回复
热议问题