【问题标题】:Excel interop - how to stop number (stored as text) being "evaluated"Excel互操作 - 如何停止数字(存储为文本)被“评估”
【发布时间】:2010-06-28 11:37:56
【问题描述】:

我想知道是否有人遇到过以下问题并对如何解决它有任何想法:我正在通过 Interop 将数据从 C# 应用程序 (.NET 3.5) 导出到 Excel (2003)。其中一列存储了一个看起来是数字的字符串值。也就是说,它是一个以 0 开头的数字,例如000123

我需要存储完整的编号,因为它可能是序列号或类似的东西。 Excel对此并不热衷,所以我想我可以通过将目标单元格设置为通用来解决它。当我导出到 Excel 时,我发现存储的是 123 而不是 000123。

我已经在调试中运行了该应用程序,并且从我发现的监视列表中发现(对于“范围范围”:

range.NumberFormat = "General"`
this.Rows[iGridRow].Cells[iGridCol].Value = "000123" '/* (datagrid is not truncating it)*/
range.Value2 = 123.0

即使我在此之前设置了 numberformat,它似乎也被作为数字处理:

range.NumberFormat = sNumberFormat;
range = (Range)sheet.Cells[iExcelRow, iExcelCol];
range.Value2 = this.Rows[iGridRow].Cells[iGridCol].Value.ToString();

有人可以帮忙吗?

【问题讨论】:

    标签: c# excel com-interop


    【解决方案1】:

    在数字前添加一个撇号 '。然后 Excel 会将数字视为字符串。

    【讨论】:

    • 谢谢 :-) 我有点困惑,为什么我可以在文本字段中输入该值,它会完美工作,但无法通过互操作工作。
    【解决方案2】:

    我知道已经晚了,但也许将来有人需要这个。 不是真的为了性能,但你可以先将它存储在一个二维数组中。

    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] = " ";
                        }
                    }
                }
    

    这样数字保持数字,字符串保持字符串。

    还可以通过批量插入将信息传递给 Excel,而不是逐个单元格地传递。 那是我的代码。

    // 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;
    

    【讨论】:

      【解决方案3】:

      我使用这个宏。它在每个单元格中的每个数字值之前插入一个撇号。

      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
      

      【讨论】:

        【解决方案4】:

        正确的类型不是 General,因为 general 会尝试猜测正确的类型,在这种情况下是数字,但您必须将其指定为文本以不截断前导零。

        试试下面的代码:

        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";
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-26
          • 2010-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多