从.Net导入Excel时的科学符号 [英] Scientific notation when importing from Excel in .Net

查看:108
本文介绍了从.Net导入Excel时的科学符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#/ .Net作业从Excel导入数据,然后处理它。我们的客户端删除文件,我们处理它们(重要的是因为我无法控制原始文件)。



我使用OleDb库来填充数据集(我讨厌写这个代码,严重的是,有一个.Net开发人员恐慌写的代码呢?)。该文件包含一些数字,如30829300,30071500等...这些列的数据类型为文本。



当我导入时,这些数字将转换为科学记数法数据。有没有办法防止这种情况发生?



-Chris

解决方案

OleDb库通常会在Excel电子表格中弄乱您的数据。这主要是因为它强制所有内容都是固定类型的列布局,从每列中前8个单元格中的值的每个列的类型猜测。如果它猜测错误,最后你会转换成科学符号的数字字符串。 Blech!



为了避免这种情况,你最好跳过OleDb,直接阅读表格。您可以使用Excel(也是blech!)的COM界面或第三方.NET Excel兼容的读取器来执行此操作。 SpreadsheetGear 是一个这样的图书馆,工作相当不错,并且有一个非常类似于Excel的COM界面的界面。 / p>

I have a C#/.Net job that imports data from Excel and then processes it. Our client drops off the files and we process them (important because I don't have any control over the original file).

I use the OleDb library to fill up a dataset (I hate writing that code. Seriously, is there any code that a .Net developer dreads writing more than that?). The file contains some numbers like 30829300, 30071500, etc... The data type for those columns is "Text."

Those numbers are converted to scientific notation when I import the data. Is there anyway to prevent this from happening?

-Chris

解决方案

The OleDb library will, more often than not, mess up your data in an Excel spreadsheet. This is largely because it forces everything into a fixed-type column layout, guessing at the type of each column from the values in the first 8 cells in each column. If it guesses wrong, you end up with digit strings converted to scientific-notation. Blech!

To avoid this you're better off skipping the OleDb and reading the sheet directly yourself. You can do this using the COM interface of Excel (also blech!), or a third-party .NET Excel-compatible reader. SpreadsheetGear is one such library that works reasonably well, and has an interface that's very similar to Excel's COM interface.

这篇关于从.Net导入Excel时的科学符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆