使用 OleDB 导入 CSV 文件 - 字符串数据类型?
我正在编写一个 C# .net Web 应用程序,用户将上传 CSV 文件进行处理。这些 CSV 文件包含多种数据类型,我在处理其中一些数据类型时遇到了问题。有一个 20 位收据编号字段以科学记数法导入(9.1234563E+18 而不是 91234563000000000000),并且任何前导零都会被删除。另一个 4 位商店编号字段中的前导零也被删除。
我读过有关创建 Schema.ini 文件来定义数据类型的内容,但每个文件都有不同的文件名,这意味着我必须以编程方式生成新的 Schema 文件,并且由于多个用户可能同时使用该应用程序,这有点排除了这个选项。
有人有什么建议吗?
I'm writing a C# .net web application, and users will be uploading CSV files to be processed. These CSV files contain a number of data types, and I'm having trouble with a few of them. There is a 20-digit receipt number field that is getting imported in scientific notation (9.1234563E+18 instead of 91234563000000000000), and any leading zeros are getting dropped. Leading zeros are also dropped in another 4-digit store number field.
I've read about creating a Schema.ini file to define the datatypes, but each file will have a different file name, meaning I'd have to generate a new Schema file programmatically, and since multiple users may be using the application simultaneously, it kind of rules this option out.
Does anybody have any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此 CSV 文件是在 Excel 中创建的吗?如果是,请确保用户将列设置为正确的数据类型。我之前也遇到过类似的问题,但这是因为用户将日期字段设置为不同的类型 - 所以我不会返回像 20100408 这样的东西,而是会得到像 24782 :-)
否则,尝试在每个字段中直接读取文本......然后自己处理转换
Is this CSV file created in Excel? if it is, ensure the users have the columns set to the right data types. I had a similar issue before, but it was due to the users have a date field set as a different type - so instead of getting back something like 20100408, i would get like 24782 :-)
Otherwise, try reading in each field has straight text....and then handling the conversions yourself
听起来您必须编写一些自定义代码来手动读取文件并自行拆分字段。您可以轻松地一次读取一行并使用 String.Split 方法获取各个字段。当然,这是假设 CSV 文件始终采用相同的格式,因此无论文件来自何处,您都可以以相同的方式处理每个字段。这样您就可以按照您想要的方式处理每个字段。
It sounds like you will have to write some custom code that reads the files manually and split the fields yourself. You can easily read in a line at a time and use the String.Split method to get the individual fields. Of course this is assuming the CSV files will always be in the same format so you can process each field the same way no matter where the file comes from. This way you can process each field exactly the way you want to.