Link to home
Start Free TrialLog in
Avatar of Terry Woods
Terry WoodsFlag for New Zealand

asked on

Excel corruption of .csv file data

I've become aware of the limitations of opening .csv data files into Excel 2007 (earlier versions have the same issue). Various changes are made to the data as the file is opened, including at least the following:
* Fields that appear to be numeric and have leading or trailing zeroes have the leading/trailing zeroes removed
* Values that Excel thinks match a date such as 5-10 get assigned a data type of date. Reformatting the field's data type to text gives a crazy value such as 40308 (probably the number of days from 1/1/1970 to the date 5/10 in the current year)

Saving changes to the original .csv file causes permanent data loss as the above changes are saved back to the file (What were they thinking?!).

This issue can be worked around by using the Data -> Get External Data From Text tool, but I deal with .csv files so frequently that I want an easier way of getting my data opened without corruption. I came up with the below set of actions:

When right clicking on a .csv file in Windows Explorer, a context menu appears with various options. It would be useful to have an option “Open without corruption using Excel” or similar, which does the following:

1.	Opens Excel
2.	Creates a blank spreadsheet
3.	Imports the select .csv file similar to the following commands:
        a.	Select Data -> From Text
        b.	Select the .csv file to be imported
        c.	Choose the “Delimited” option
        d.	Use comma as the delimiter character
        e.	Select all columns (hold shift and click the right-most column header)
        f.	Change the “Column data format” to Text
        g.	Click Finish
        h.	Put the data into cell A1
4.	And possibly even alter the currently open Excel file name from “Book1” (or whatever it is) to the name of the .csv file that was opened.


I have a colleague trying to learn to program with .NET that can attempt this, but the key questions are:

1. Will it be possible to perform the above actions with .NET?
2. What is a general programming plan (perhaps which modules to use, etc?) that I can pass on to my colleague for development?
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

No need to worry about VB.net corrupting a csv file.

Just use the text reader to open your csv file and iterate through the fields to make your changes then use the textwriter to save it.

PS. You will NOT be able to change a columns datatype as described when saving. Additionally, you willnot be able to change the book name, however, Excel will automatically rename the book name to the file name.
Avatar of Dave Baldwin
They were thinking that you wanted to use Excel 'xls', not 'csv'.  A plain text editor like PSPad will open and edit 'csv' files but only in a text format, not a column format.  I went looking for a 'csv' editor for another question but couldn't find one.  You'd think that after 30 or 40 years one would be available.
Avatar of Terry Woods

ASKER

I want to use Excel 2007 to edit the file, and Excel saves to .csv format ok without corrupting it but it corrupts the data when a .csv file is opened by just double clicking it (or probably with File->Open)
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the link. CSVed looks quite good, but I've got a lot of my neurons invested in using Excel quickly, so I want to pursue that avenue further if I can.
I understand.  Let us know how it works out.  A number of questions have come up where CSV is being used for something other than an Excel import and it is often a problem.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is it important that the data be saved back into the csv file?

I ask because it would be fairly simple to code a small app (in .net) that opens and reads the csv file then uses automation to fill a new excel table with the information from the csv file (so data should not get converted by excel) and as excel thinks this is a new document one can save it as an xls file.
excel thinks this is a new document one can save it as an xls file.

By that I mean if you hit the save button in excel it won't overwrite the csv file with 'corrupt' values
Andy, it is important to save data back to a csv file, though not necessarily the same one. I use Excel to find and fix errors prior to importing the data to a system which accepts csv files. It's a fairly repetitive task so the actions that I perform to open a file, edit it and save it need to be fairly streamlined - I will need a mechanism that avoids having to do a Save-As and select csv as the file type.
What sort of 'errors' in the csv file - would it be possible to drop excel totally and check/correct the csv file completely in code ?

It would be possible to code the app as I said then have it rename the csv file (backup - safety) then perform a SaveAs to save the new excel sheet as a csv file with the same name and path as the original.
Thanks for your comments Andy

The kind of errors that are fixed in the csv file differ for each file, but can be things like unusual dates and outlying data values, or values that may been recorded with an incorrect unit of measurement. Data filters and formulae etc are used to find and correct them - things that Excel excels at. I don't think it would be sensible to try to avoid using Excel.

Yes, it would be fine for an app to do as you describe, though I've added some extra detail to ensure efficiency and safety:
1. Open and read the csv file (from Windows Explorer context menu, or even just by associating the .csv file type with the new app) then use automation to fill a new excel table with the information from the csv file (so data should not get converted by excel)
2. Rename the csv file (as a backup for safety), adding a datetime to the filename to ensure it doesn't get overwritten when the same file is reopened
3. Perform a SaveAs to save the new excel sheet as a csv file with the same name and path as the original.