tags:

views:

4013

answers:

5

Does anyone happen to know if there is a token I can add to my csv for this field so Excel doesn't try to convert it?

I'm trying to write a csv file from my application and one of the values happens to look enough like a date that Excel is automatically converting it from text to a date. I've tried putting all of my text fields (including the one that looks like a date) within double quotes, but that has no effect.

+5  A: 

If you put an inverted comma at the start of the field, it will be interpreted as text.

Example: 25/12/2008 becomes '25/12/2008

You are also able to select the field type when importing.

Harley
I don't want a ' at the beginning of my data.
awhite
+3  A: 

I have found that putting an '=' before the double quotes will accomplish what you want. It forces the data to be text.

eg. ="2008-10-03",="more text"

Jarod Elliott
I'm accepting this answer because 1) my csv file will only be used by Excel, and 2) this is for accounting and can't have a ' at the beginning, and 3) I don't want them to have to do an import. I just want them to open the csv.
awhite
Oh, and thank you all for your quick replies!
awhite
+2  A: 

(Assuming Excel 2003...)

When using the Text-to-Columns Wizard has, in Step 3 you can dictate the data type for each of the columns. Click on the column in the preview and change the misbehaving column from "General" to "Text."

DocMax
This is an excellent choice if I could force the user to use the import process.
awhite
A: 

An alternate method:

Convert the format of the column you want to change to 'Text'. Select all the cells you want to preserve, copy. Without deselecting those columns, click "Edit > Paste Special > As values"

Save as CSV. Note that this has to be the last thing you do to the file because when you reopen it, it will format itself as dates since cell formats cannot be saved in CSV files.

Andy Baird
A: 

You might want to check the following link.

http://gridviewguy.com/Articles/197_Extensive_Study_of_GridView_Export_to_Excel.aspx

azamsharp