Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Though I try to avoid it, I occasionally have to open a CSV file in Excel. When I do, it formats columns containing numbers, which makes them useless for my purposes. As far as I can tell, the only way to prevent this from happening on import is to rename the file so the extension isn't .csv and use the import wizard to specify the format of each column individually. For files with 50-60 columns, this is impractical.

Since every answer for this oft-asked question on the internet suggests either some means of converting the formatted numbers back once the file is open (which won't work for me - I want to solve the general problem, not a few specific cases) or manually selecting the format type of each column (which I don't want to do), I'm looking for a way to set a global preference or style such that all columns of all CSV files opened are always formatted as text. I know about "armoring" the numbers with quotes, too, but the files I get don't come like that and I was hoping to avoid having to pre-process the files so Excel doesn't screw them up.

Is there a way to do specifically this: Always format all columns in opened CSV files as text, without manually selecting each column every time during import?

I'm using Excel 2003, but I'll take answers for 2007 if that's what you know.

share|improve this question

migrated from stackoverflow.com Jul 7 '11 at 6:21

This question came from our site for professional and enthusiast programmers.

1  
Hey Scripting Guy did a blog article about Importing CSV into Excel that might have some useful tidbits for you. Playing with the data object inside powershell may allow you to do what you want. Not posted as an answer as it's essentially just an offsite link, but it might have something of use to you. –  Matrix Mole Jul 5 '11 at 2:18

9 Answers 9

up vote 5 down vote accepted

Interesting that this seems to be such a mystery for The Internet. I thought this would be some sort of challenge, but really it's quite straightforward in VBA. This works:

Sub OpenCsvAsText(ByVal strFilepath As String)

    Dim intFileNo As Integer
    Dim iCol As Long
    Dim nCol As Long
    Dim strLine As String
    Dim varColumnFormat As Variant
    Dim varTemp As Variant

    '// Read first line of file to figure out how many columns there are
    intFileNo = FreeFile()
    Open strFilepath For Input As #intFileNo
    Line Input #intFileNo, strLine
    Close #intFileNo
    varTemp = Split(strLine, ",")
    nCol = UBound(varTemp) + 1

    '// Prepare description of column format
    ReDim varColumnFormat(0 To nCol - 1)
    For iCol = 1 To nCol
        varColumnFormat(iCol - 1) = Array(iCol, xlTextFormat)
        ' What's this? See VBA help for OpenText method (FieldInfo argument).
    Next iCol

    '// Open the file using the specified column formats
    Workbooks.OpenText _
            Filename:=strFilepath, _
            DataType:=xlDelimited, _
            ConsecutiveDelimiter:=False, Comma:=True, _
            FieldInfo:=varColumnFormat

End Sub

Usage:

OpenCsvAsText "C:\MyDir\MyFile.txt"

Comma-separated file is now open as Excel sheet with all columns formatted as text.

Note that @Wetmelon's wizard solution (if you can parse it out of the surrounding confusion) works just fine, but if you're opening many files then you may, like me, grow weary of, each time, scrolling to column 60 in order to Shift-Click it.

EDIT @GSerg claims in the comment below that this "doesn't work" and "eats spaces and leading zeroes". I'll just quote the comment to the question, which is more descriptive:

For reasons unknown, even if you explicitly provide formats for all columns in VBA, Excel will ignore it if the file extension is CSV. As soon as you change the extension, that same code will yield the correct results.

So the code above "works", but gets killed by this ridiculous Excel behaviour. Whichever way you cut it, you're stuck having to change the extension to something other than ".csv", sorry! After that, you're home free.

share|improve this answer
1  
Doesn't work. Eats spaces, eats leading zeroes etc. –  GSerg Jul 6 '11 at 19:19
    
Yes it does work, and no it doesn't eat anything. –  Jean-François Corbett Jul 6 '11 at 19:23
    
Source. Result. Result when renamed to not CSV. –  GSerg Jul 6 '11 at 19:31
    
@GSerg: Editing my answer following your somewhat more educational comment to the question! –  Jean-François Corbett Jul 6 '11 at 19:36
    
The used method Workbooks.OpenText has its flaws by design (See my answer below). Use QueryTables instead. –  nixda Jul 25 '13 at 23:58

How to open CSVs in Excel

Best way

  • Excel → Data → Get external data → Select all columns with Shift and choose Text

    Upside: Treats all values correctly as text without any exception

    Downside: more steps than a simple Double-click

Worst way

  • Open a CSV with Double Click or Excel's Open with dialog

    Downside: Excel's internal CSV handler misinterprets values with a leading - or = sign as a formula rather then text

    Downside: You will lose leading zeros from binary values like 0001 due to Excels auto detected column format

Best way (for VBA)

  • Use QueryTables (the VBA counterpart to Get external data) → Example code

    Upside: Treats all values correctly as text without any exception

    Downside: Slightly more code than OpenText method

Worst way (for VBA)

  • Use Workbooks.OpenText method → Example code

    Downside: This method is still using Excel's internal CSV import handler with all its flaws

    Downside: Additionally the fieldinfo parameter of OpenText is ignored if the extension is CSV. Normally this parameter lets you choose every column format, But not if the extension is CSV. You can read more about this behavior on stackoverflow

    Temporarily renaming the source extension from CSV to TXT and than back to CSV is a valid workaround if you have full control over the source file

Additional methods

  • If you have access to the source which creates your CSV, you can alter the CSV syntax.
    Enclose every value with double quotation marks and append an equal sign like ="00001". This will force Excel to treat values as text

  • Open CSV in Notepad and copy&paste all values to Excel. Then use Data - Text to Columns
    Downside: Text in Columns for changing column formats from general back to text produces inconsistent results. If a value contains a - surrounded by characters (e.g. "=E1-S1"), Excel tries to split that value up into more than one column. Values located right to that cell may get overwritten
    (This behavior was changed somewhere between Excel 2003 and 2013. Now this method doesn't change anything)


Excel Add-In to open CSVs and import all values as text

This is a Excel Plug-in (Import CSV) to simplify CSV Import actions.
The main advantage: Its a one-click solution and uses QueryTables, the same bulletproof method behind Get external data

  • It adds a new menu command to Excel, which lets you select CSV and TXT files. All values are imported to the active sheet and and the selected cell
  • Add-in works for all Office versions on Windows and MAC (Linux is a little bit tricky but possible).
  • The whole Add-In only has 35 lines of code. Check the commented sourcecode if you are curious
  • The used CSV list separator (comma or semicolon) is taken from your local system settings. Non-english Excel users will benefit from this

Installation

  1. Download the Add-In and save it to your Add-Ins folder: C:\Users\USERNAME\AppData\Roaming\Microsoft\AddIns
  2. Open Excel and activate the Add-In
    Excel 2003: Menu bar → Tools → Add-ins
    Excel 2007: Excel Options → Add-Ins → Go To
    Excel 2010-13: File tab → Options → Add-Ins → Go To
  3. Restart Excel and look at your menu bar

    enter image description here
    click for fullscreen with more details


PowerShell script to open CSVs direct from Windows Explorer

Use a Powershell script to open a CSV with a Double-Click from within the Windows Explorer and preserve all values as text in Excel. This method is also available for Windows XP if you install Powershell manually.

  1. Insert the code below to a text file and save it as OpenCSV.ps1 or download it

    $csv = Get-Item $args[0]
    
    $excel = New-Object -ComObject excel.application 
    $excel.visible = $true
    $workbook = $excel.Workbooks.Add()
    $worksheet = $workbook.worksheets.Item(1)
    $worksheet.Name = $csv.basename
    
    $arrFormats = ,2 * $worksheet.Cells.Columns.Count
    
    $TxtConnector = ("TEXT;" + $csv.fullname)
    $Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
    
    $query = $worksheet.QueryTables.item($Connector.name)
    $query.TextFileOtherDelimiter = $Excel.Application.International(5)
    $query.TextFileParseType  = 1
    $query.TextFileColumnDataTypes = $arrFormats
    $query.AdjustColumnWidth = 1
    $query.Refresh()
    $query.Delete()
    
    Remove-Item variable:arrFormats
    
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
    [System.GC]::Collect() 
    [System.GC]::WaitForPendingFinalizers() 
    
  2. Insert the code below to a new text file and save it as OpenCSV.cmd
    Modify the path to your path where you saved the Powershell script earlier.

    Powershell -Command "C:\some\folder\OpenCSV.ps1" %1
    

    Since we can't use a Powershell script as a file open handler we have to use the old-fashioned batch file as work around. Don't ask me why. Ask Microsoft.

  3. Now you should have two files: OpenCSV.cmd and OpenCSV.ps1
    Right-click any CSV → Open with → Choose default program and select your CMD file

    (or)

    Avoid steps 1-3 and the hassle to create two scripts. Download the ready-to-use executable OpenCSV.exe and assign it as your default program to open CSV files. I just converted the PowerShell script with PS2EXE to an executable.

Voila, from now on every time you double click a CSV it will be opened by Excel and all values are still formatted as text.

How does the script work?

  • First, when double-clicking a CSV we pass the CSV path to a CMD
  • The CMD then calls a Powershell script and passes on the CSV path
  • The Powershell script silently opens Excel and tells Excel to use QueryTables to open the CSV path. Thats the same method the Excel Add-In is using too.
share|improve this answer
1  
Add-In was tested with Office 2003 and 2013 –  nixda Jun 15 '13 at 12:39
1  
Great answer. Very complete. –  Jean-François Corbett Jul 27 '13 at 13:41

You can try by opening a .xlsx first, then creating a data connection and importing the .csv. Select "comma" delimited, then select the option to treat all columns as text rather than "General".

Edit: Oh, I didn't fully read the question. In import wizard, select the first column header that you want to import as text, scroll to the final column header, and Shift+Click the header. Then select the "Text" radial option.

share|improve this answer
1  
Yeah, this works nicely -- as long as the file's extension isn't ".csv". +1 –  Jean-François Corbett Jul 6 '11 at 18:41

Wetmelon's suggestion works (even with .CSV) if you do the following:

  1. Open Excel to a blank workbook or worksheet
  2. Click to Data > [Get External Data] From Text
  3. Use the "Text Import Wizard" as Wetmelon describes (Comma delimted, select the first column, SHIFT+CLICK the last column, set everything to Text).

I know it is more steps, but at least it lets me open CSVs this way without having to change the extension

share|improve this answer

If you are always importing the same data (constant record format, layout, etc...) you could write an Access macro using an import spec and then dump the data back out to Excel. Done this may times. The other way that I have done it is to use VBA and read the data into the worksheet one record at a time and parse it out as it reads. As far as I know there is no way to set a default format during import in Excel and even if you could it would cause problems with the next file type you try to parse.

share|improve this answer
    
Here's the thing - I'm not trying to do any parsing except for putting the text string that was in between the commas into columns when I import it. If I need it to be formatted a certain way, I can then do that (like make date strings dates, etc), but I just need the data left alone initially. Why should that be so hard? –  William Gunn Jul 5 '11 at 2:15

As request, submitting my comment as an answer (with a little more info added):

Hey Scripting Guy did a blog article about Importing CSV into Excel that might have some useful tidbits for you. Playing with the data object inside powershell may allow you to do what you want.

Although the article specifically just mentions importing the data into the cells which may leave the number format, it may be possible to play around with some of the Excel ComObject properties and methods to force the data to enter the cells as raw text instead (or force the formatting of the cells into text before or after the import).

share|improve this answer

For reasons unknown, even if you explicitly provide formats for all columns, Excel will ignore it if the file extension is CSV.

Some options:

  • Create a query to import the data, as Wetmelon suggests.
    Disadvantage: you may be missing CSV database drivers on a 64-bit machine.

  • Use Jean's code, but incorporate copying the file to a temporary folder and changing the copy's extension.
    Disadvantage: No link to the original file (saving will overwrite the copy); you will have to manually delete the copy afterwards. Still, you can manually Save As over the original CSV.

  • Open the CSV in Notepad, Ctrl+A, Ctrl+C, paste to Excel, then Data - Text to Columns, then it's the usual wizard where you can set all columns to Text in one go. It's a different flavour of the previous option, because it also hides the precious extension from Excel.
    Disadvantage: manual.

  • Have a very simple VBA loop that reads the whole file into memory and puts it onto the sheet, cell by cell.
    Disadvantage: slower, ugly.

share|improve this answer
    
Isn't "No link to the original [CSV] file" a disadvantage of all these methods? I like the idea of copying to a temp folder and changing the extension there... Could even overwrite the original (as CSV) as soon as the copy is open. –  Jean-François Corbett Jul 7 '11 at 7:00

If I understand the question correctly, this is easily solved using the Transpose option in Paste-Special as described here.

share|improve this answer

If you want to create a some column in .csv that will be without drop off of leading zero, the best way is to write it as formula! Example:

Id;Name
"=""002""";Foo
"=""010""";Bar
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.