ASP.NET CSV 响应、Excel 指数格式问题
在我的 ASP.NET 应用程序中,我需要将 DataTable 作为 CSV 响应写入客户。除了有数字的列之外,一切都工作正常。
例如:7002136138603600000
但是当我在 Excel 中打开 CSV 时,它以指数格式显示。
像这样的东西:7E+18
您能否让我知道需要做什么才能将这些值显示为测试而不是指数格式?
谢谢, 马赫什
In my ASP.NET application, I need to write DataTable as CSV response to the customer. Everything is working fine except a column which has a numbers.
Ex: 7002136138603600000
But when I open the CSV in Excel, it is showing in exponential format.
Something like this: 7E+18
Could you please let me know what needs to be done in order to show these values as test instead in exponential format?
Thanks,
Mahesh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于 CSV 数字格式,请检查此处:
http://support.microsoft.com/kb/214233
我多次导出到 Excel 。只需使用 HtmlTextWriter 将 Gridview 的输出写入字符串生成器即可发送响应。
您可以找到两个执行此操作的示例:
更多搜索结果:http://www.google.com/search?q=export+gridview+to+excel
所以,我认为最简单的方法是绑定到 GridView 并将其写为HTML 与其他示例中一样,除非您还因其他原因需要 CSV。
Regarding the CSV numbers format, check here:
http://support.microsoft.com/kb/214233
I did export to Excel many times. Just write the output of the Gridview to a String Builder using an HtmlTextWriter send the response.
You can find two examples of doing this:
More search results: http://www.google.com/search?q=export+gridview+to+excel
So, I think the easiest way is to bind to GridView and write it as HTML as in the other examples, unless you need CSV for another reason also.
你不能用 CSV 来做到这一点。用 Excel 试试吧。创建一个电子表格,在单元格中包含值“7002136138603600000”,前面带有单引号 (') 以指示它是文本。 Excel 将正确显示完整数字。将其另存为 CSV。重新加载。该数字将使用指数格式显示。
或者您也可以使用 HTML。将命名空间 xmlns:x="urn:schemas-microsoft-com:office:excel" 添加到 html 根目录,并将此属性添加到 td 元素。
Excel 会将其显示为文本。对于更完整的示例,请像我提到的那样创建 Excel 文档并另存为 html。
如果您知道您的客户正在使用 Excel 2003 或更高版本,您也可以使用 XML。
You can't do it with CSV. Try it with Excel. Create a spreadsheet with the value "7002136138603600000" in a cell, preceded by a single quote (') to indicate that it's text. Excel will display the full number correctly. Save it as CSV. Reload. The number will be displayed using exponential format.
Alternatively you can use HTML. Add the namespace xmlns:x="urn:schemas-microsoft-com:office:excel" to your html root and at this attribute to your td element.
Excel will display it as text. For a more complete example create the Excel doc like I mentioned and save as html.
You can also use XML if you know your customers are using Excel 2003 or newer.
在文本编辑器中打开该文件,您可能会发现数字导出正确。对于某些长数字,Excel 只是默认采用指数格式。
Open up the file in a text editor and you will likely find that the numbers are exporting correctly. Excel simply defaults to exponential format for some long numbers.
它必须是 CSV 吗?如果是这样,那么您几乎只能坚持 Excel 的默认行为。
如果它不必是 CSV,那么请查看 Excel XML 格式,这将使您能够更好地控制显示格式(在 Excel 中可以完成的任何操作都可以在 XML 文件字符串中完成)。当然,这仅在 Office 2003 及更高版本中支持。
Does it HAVE to be CSV? If so, then you're pretty much stuck with the default behavior from Excel.
If it doesn't have to be CSV, then look into the Excel XML format, which will give you much finer control over the display formats (anything that can be done within Excel can be done in the XML file string). Of course, this is only supported in Office 2003 and later.
当数字都是数字时,它很容易添加asc char 32,使用一个函数来找出数字是否都是数字,如果是的话,然后简单地将asi 32添加到数字中(如果需要,转换为字符串)。
在 Microsoft Excel 2007 和 Windows 7 上测试了 csv 文件,它工作正常,不确定早期版本。
its easy add asc char 32 when the numbers are all digits, use a function to find out if the numbers are all digits and if true, then simple add asi 32 to the number (convert to string if required).
Tested csv file on microsoft excel 2007 and windows 7 and it works fine, not sure about earlier versions.