问题
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?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/5747776/importing-csv-files-using-oledb-string-datatype