views:

936

answers:

2

NOTE: this question is almost a dupe of:

http://stackoverflow.com/questions/284092/stop-ms-excel-auto-formatting-numeric-strings-as-numbers

I say "almost" because this specific question was not asked ...

Question: Is there a way to attach a "schema" to a CSV file, prior to Excel import, that allows Excel to understand the correct formatting of the CSV file without requiring post-processing of the CSV file and without requiring end-user intervention in order to correctly format the fields by hand?

By schema, I mean any method whatsoever of generating a text-based definition that can be saved somewhere on Customer X's machine and then referenced whenever the usual CSV import has to take place, including but not limited to XSD, VBMacro, or whatever conventions Excel has for this kind of operation (if any).

Background: Excel has a habit of being "helpful" by modifying input data when opened from plain-text such as a CSV file.

For an example of what is meant by this, see any of the various following links:

    http://ask.metafilter.com/28449/Preventing-Excel-applying-time-formatting-to-imported-data
    http://excel.tips.net/Pages/T002588_Handling_Leading_Zeros_in_CSV_Files.html
    http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/aae07b39-865f-4c68-a07f-7cad2dfd6733
    http://social.msdn.microsoft.com/forums/en-US/vsto/thread/80285088-b476-418f-9613-a2c499c8da7b/

We regularly have to send CSV files to customer X and we do not have the luxury of modifying the CSV files directly in order to "morph" them into a format that Excel will render exactly as intended.

Moreover, customer X does not always have the personnel who are trained with Excel in order to do the import correctly.

+1  A: 

What I've done when facing this problem is create a macro to run after the import which "un-fixes" Excel's fixes. It's not a great solution, but it's a working workaround.

Boofus McGoofus
A: 

I'm afraid I don't completely follow. You say you send the CSV files, but you can't modify the CSV files? So you're not generating them? And you can't process them before sending them to the customer?

Though I find it difficult to imagine such a situation, I'll assume that's the case. Is there any chance you can get the customer to run a script instead of launching Excel directly? For example, could they be trained to double-click on a VBScript instead of double-clicking the CSV or using Open from Excel?

That's the closest I can think of given the constraints you've described, but I can't help but think there has to be something you can do further upstream before it leaves your hands and enters the customer's.

John Y
Yes, there's always the ability to run a script or a batch file or something else, but then there's always the additional training necessary on how to use that yet-another-one-off-kludge. How much better would it be if the customer could just be sent a single .ini file schema that Excel could use without the user having to learn and use yet-another-one-off-undocumented-script-workaround?
dreftymac
It certainly would be better but (apparently) Excel doesn't have a CSV schema facility. If I may ask, why can't you process the CSV before sending it to the customer?
John Y
because they come from a third party
dreftymac