2010-03-14 23 views
1

我有一点ASP.NET代码可以将数据网格中的数据导出到Excel中,但是我注意到它在导出时会弄乱某个特定的字段。Excel将我的号码变为浮动

E.g.我在数据网格的一列中有类似89234010000725515875的值,但导出时它将它变成89234 + 19。

是否有任何Excel格式,将带回我的原始数字?谢谢。

回答

2

Excel并没有真正搞乱这个领域。两件事情正在发生:

  1. Excel以科学计数法格式化大量数据。所以“89234010000725515875”变成“8.9234E + 19”或“8.9234×10^19”。
  2. 数字“89234010000725515875”的大小超出了Excel用于存储值的精度。 Excel将您的号码存储为“89234010000725500000”,因此您失去了最后五位数字。

根据您的需求,您可以做两件事之一。

您的第一个选择是将格式从“常规”更改为“0”(小数位数为零)。这会给您“89234010000725500000”,因此您将失去精确度,但是您可以执行calculcations on号码。

第二种方法是将单元格格式化为文本“@”或将行的起始位置用撇号粘贴以强制为文本。你会得到所有的数字,但你不能计算的价值。

我希望这会有所帮助。

0

以下是C#源代码与做到这一点。由于SpreadsheetGear API与Excel的API类似,您应该可以轻松地将此代码调整到Excel的API以获得相同的结果。

如果您想自己尝试,可以下载免费试用here

声明:我自己的SpreadsheetGear LLC

using System; 
using SpreadsheetGear; 

namespace Program 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Create a new workbook and get a reference to A1. 
      IWorkbook workbook = Factory.GetWorkbook(); 
      IWorksheet worksheet = workbook.Worksheets[0]; 
      IRange a1 = worksheet.Cells["A1"]; 
      // Format A1 as Text using the "@" format so that the text 
      // will not be converted to a number, and put the text in A1. 
      a1.NumberFormat = "@"; 
      a1.Value = "89234010000725515875"; 
      // Show that the formatted value is 
      Console.WriteLine("FormattedValue={0}, Raw Value={1}", a1.Text, a1.Value); 
      // Save the workbook. 
      workbook.SaveAs(@"c:\tmp\Text.xls", FileFormat.Excel8); 
      workbook.SaveAs(@"c:\tmp\Text.xlsx", FileFormat.OpenXMLWorkbook); 
     } 
    } 
} 
1

您可以添加一个空格到外地,那么当你将其导出到Excel中,它被认为是字符串:

lblTest.Text = DTInfo.Rows(0).Item("Test") & " " 

好运。