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

前端 未结 4 1195
梦如初夏
梦如初夏 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";
    
    0 讨论(0)
  • 2021-01-13 09:36

    I know it's late, but maybe someone need this in the future. It's not really for the performance but you can store it in a two-dimentional array first.

    object[,] Values = new object[iGrid.Rows.Count, IGrid.Columns.Count];
                for (int i = 0; i < alllogentry.Rows.Count; i++)
                {
                    for (int j = 0; j < alllogentry.Columns.Count; j++)
                    {
                        if (alllogentry.Rows[i].Cells[j].Value != null)
                        {
                            Values[i, j] = alllogentry.Rows[i].Cells[j].Value.ToString();
                        }
                        else
                        {
                            Values[i, j] = " ";
                        }
                    }
                }
    

    This way number stays number and string stays string.

    Also pass the information via bulk insert to excel not cell by cell. That was my code.

    // Bulk Transfer
    String MaxRow = (alllogentry.Rows.Count+6).ToString();
     String MaxColumn = ((String)(Convert.ToChar(alllogentry.Columns.Count / 26 + 64).ToString() + Convert.ToChar(alllogentry.Columns.Count % 26 + 64))).Replace('@', ' ').Trim();
    String MaxCell = MaxColumn + MaxRow;
    
    //Format
    worksheet.get_Range("A1", MaxColumn + "1").Font.Bold = true;
    worksheet.get_Range("A1", MaxColumn + "1").VerticalAlignment = XlVAlign.xlVAlignCenter;
    
    // Insert Statement
        worksheet.get_Range("A7", MaxCell).Value2 = Values;
    
    0 讨论(0)
  • 2021-01-13 09:42

    I use this macro. It inserts an apostrophe before each value that is numeric in each cell.

    Sub Macro1()
        Dim rwIndex As Integer
        Dim colIndex As Integer
        For rwIndex = 1 To ActiveSheet.UsedRange.Rows.Count
                For colIndex = 1 To ActiveSheet.UsedRange.Columns.Count
                    If IsNumeric(Cells(rwIndex, colIndex).Value) Then _
                        Cells(rwIndex, colIndex).Value = "'" _
                         & Cells(rwIndex, colIndex).Value
                Next colIndex
        Next rwIndex
    End Sub
    
    0 讨论(0)
  • 2021-01-13 09:49

    Add a single apostrophe ' before the number. Excel will then treat the number as a string.

    0 讨论(0)
提交回复
热议问题