Excel only places quotes around certain fields, how do I force excel to save a CSV file with quotes around every column?
20 Answers
If you open the XLS file in LibreOffice or OpenOffice, then Save As....and choose Text CSV, it alows generating a CSV file that also includes quotes as delimiters. E.g.: "Smith","Pete","Canada" "Jones","Mary","England"
Just check the "Quote all text cells" box:

In order to also quote numeric fields, highlight your cell range and change the cell formatting to "text" prior to saving.
- 103
- 587
This page also has the solution which comes straight from the horse's mouth:
http://support.microsoft.com/kb/291296/en-us
If the link degrades, the topic to search for is:
"Procedure to export a text file with both comma and quote delimiters in Excel" and/or "Q291296"
tl;dr: use their macro
- 1,717
Powershell appears to dump correctly. so something like
search for powershell.exe on windows machine if you dont know powershell.
import-csv C:\Temp\Myfile.csv | export-csv C:\Temp\Myfile_New.csv -NoTypeInformation -Encoding UTF8
Hope it helps someone.
- 556
- 1
- 5
- 11
I found this easy solution:
- Highlight the cells you want to add the quotes.
- Right click and go to: Format Cells → Tab: Number → Category: Custom
- Paste the following into the Type field:
"''"@"''"(see details below) - Click “okay”
The string you are pasting is "''"@"''" which is double quote, single quote, single quote, double quote, @ symbol, double quote, single quote, single quote, double quote.
Edited for Excel 2010 from the information found here.
Highlight the cells you want to add the quotes. Go to Format –> Cells –> Custom Copy/Paste the following into the Type field: \”@\” Click “okay” Be happy you didn’t do it all by hand.
From: http://www.lenashore.com/2012/04/how-to-add-quotes-to-your-cells-in-excel-automatically/
- 181
- Highlight the cells you want to add the quotes.
- Right click and go to: Format Cells → Tab: Number → Category: Custom
- Paste the following into the Type field:
"''"@"''"(see details below) - Click “okay”
- Open the .csv file with Notepad (or equivalent)
- Replace all
' '(repeated single quotes) with"(double quote) - Replace all
; - Save revised .csv file
This was the easiest for me: I imported the spreadsheet into Access 2010 and exported it from there as a delimited text file. Gave me the quote marks around my fields. Takes less than a minute for 42k rows now that I have the steps down.
- 59
I have found another work around which doesn't involve the VBA Macro, but which does require Notepad++ or a similar macro-based text editor.
Export your file as Tab-seperated text, then open the exported file within Notepad++. Replace all instances of the 'tab' character with the text "," (ie literally double-quote, comma, double-quote) and then use another macro to prefix and suffix each line of text with a double-quote.
A bit hacky but I found it quicker than getting a VBA macro working.
- 39
If you use Notepad++ to add quotes to the beginning of each line, just open the exported csv file, put your cursor at 1st line, 1st column, then click menu Edit/Column Editor..., in field 'Text to insert', enter ", then the beginning of each line will have a quote, then you can use regular expression to search/replace all the tabs.
- 441
Another way if you have MS access (I have ver 2007) import the file then export it as a text file. Then change the .txt to .csv. Note all number fields will not have double quotes around it so if you need double quotes around the numbers too then while in Access change the field from a number field to a text field.
- 1
I have run into this issue many times. Here is the simplest solution I have come up with.
- Save the file as Excel
- Import the Excel into an Access table
- Export the table from Access to a text file.
Access will allow you to specify exporting delimiter as a comma and qualifying text fields with quotes. In the export wizard, you can specify whatever delimiter or character around strings you want.
You may need to create an Access query to arrange the fields in a particular order or hide the auto-ID field added by Access.
Also... once exported to a CSV, do NOT open it in Excel again. If you want to look at the results, open it in notepad. Excel has a tendency to mess with the data of a csv.... I think it was fields padded with leading zeros if I remeber correctly.
- 21
Exporting comma separated and quoted strings can be done with only Excel 2016 and Notepad, using a copy of the data, a formula, an export, a file properties change, a replacement in Notepad, saving the exported file and cleanup. Each is a simple step. In detail:
Copy the columns to be exported into a new intermediate sheet to preserve the original and as backup, with the new sheet to be deleted later to leave the spreadsheet as it was.
Put otherwise not occurring character(s), say '#' or ';-)' at each end of a string in the column say A with the formula
=concat("#",trim(A1),"#"), putting the formula on all the rows of another column.- Adjacent columns can be done at the same time but don't quote numbers to avoid having them read in as strings.
- The trim avoids any trailing spaces which can trigger unskillful export behavior.
- Strings should not contain a ' " ' which might perturb import.
Copy the new column(s) back over A..., using the '123' method so as to not carry the formula.
Export the sheet as a CSV file, to put in the commas between fields including numbers.
Change the file.csv properties so that it can be opened with Notepad.
Use Notepad to replace the arbitrary character(s) with ' " '.
While it would seem sensible to use " as the arbitrary character, it has to be put in a different cell, say '$A$50', and then what appears on export is ' " " " ', apparently another inconvenient trigger.
In notepad the file should be saved, becoming a *.txt file to be imported and the intermediate *.csv deleted.
With cleanup of the extra spreadsheet sheet mission accomplished.
Perhaps the Access export tools can one day be embedded in Excel. A more generic approach among those programs that use cut and paste, would be to have the paste options include a choice of ways to interpret the output structure and provide delimiters.
- 1
Here's how to use the OpenOffice app to take an Excel spreadsheet and create a text (csv) file with quotation marks around every text field.
Open the Excel file with OpenOffice, then follow these steps:
File > Save As
Provide a new name for the file you're about to create
For Filetype, choose "Test CSV (.csv)"
turn on checkbox "Edit Filter Settings"
click "Save"
choose "Keep current format"
Field delimiter should be a comma: ,
Text delimiter should a quotation mark: "
check the box "Quote all text cells"
click "OK"
This worked for me!
- 11
I made my own VBA macro to create my CSV in no time.
It just put the result in a "output" sheet that you need to create.
The result double quote all column and add ";" as a delimiter.
If you guys ask, I can add some parameters to the code to add flexibility.
Hope you guys enjoy!
Public Sub CreateCSV()
Dim i, j, nc, nr As Integer
nr = Range("A65000").End(xlUp).Row
nc = Range("ZZ1").End(xlToLeft).Column
With Sheets("output")
.Cells.Clear
For i = 1 To nr
For j = 1 To nc
If j = 1 Then
.Cells(i, 1) = Chr(34) & Cells(i, j) & Chr(34) & ";"
ElseIf j = nc Then
.Cells(i, 1) = .Cells(i, 1) & Chr(34) & Cells(i, j) & Chr(34)
Else
.Cells(i, 1) = .Cells(i, 1) & Chr(34) & Cells(i, j) & Chr(34) & ";"
End If
Next j
Next i
End With
End Sub
- 6,773
- 1
- 1
This can also be done by using Microsoft SQL Server management Studio. You basically just use the Import or Export feature to import or export your CSV file to a new CSV file with text qualifiers around each field.
- Create a database if one doesn't exist
- Right-click the database (any database)
- Click Tasks
- Click Import Data (or Export data...either should work)
- For Data source, select Flat File Source
- Browse and select your existing CSV file
- Click Next and verify that everything looks right in the preview
- Click Next again
- For Destination, choose Flat File Destination
- Browse and specify the name and location of the new file that will be created
- For Text qualifier, enter " (one double quote)
- Click Next and then Finish
Your new file should now have double quotes around each field.
- 1
The top answer (from Microsoft) is good, but it is a little hard to use since you need to provide a filename and you must preselect the cell range that you want to export. I found it easier to prepopulate the filename with <path-to-my-excel-file>/output.csv and also have the export just take everything from the current worksheet via ActiveSheet.UsedRange. Attach this VBA module to a button on your worksheet, and you have a 1-click solution to export everything as-is.
Sub QuoteCommaExport()
' Dimension all variables.
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Long
Dim RowCount As Long
' Prompt user for destination file name.
DestFile = InputBox("Enter the destination filename" _
& Chr(10) & "(with complete path):", "Quote-Comma Exporter", Application.ActiveWorkbook.Path & Application.PathSeparator & "output.csv")
' Obtain next free file handle number.
FileNum = FreeFile()
' Turn error checking off.
On Error Resume Next
' Attempt to open destination file for output.
Open DestFile For Output As #FileNum
' If an error occurs report it and end.
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
End
End If
' Turn error checking on.
On Error GoTo 0
' Loop for each row in selection.
For RowCount = 1 To ActiveSheet.UsedRange.Rows.Count
If ActiveSheet.UsedRange.Cells(RowCount, 1) <> "" Then
' Loop for each column in selection.
For ColumnCount = 1 To ActiveSheet.UsedRange.Columns.Count
' Write current cell's text to file with quotation marks.
Print #FileNum, """" & ActiveSheet.UsedRange.Cells(RowCount, _
ColumnCount).Text & """";
' Check if cell is in last column.
If ColumnCount = ActiveSheet.UsedRange.Columns.Count Then
' If so, then write a blank line.
Print #FileNum,
Else
' Otherwise, write a comma.
Print #FileNum, ",";
End If
' Start next iteration of ColumnCount loop.
Next ColumnCount
End If
' Start next iteration of RowCount loop.
Next RowCount
' Close destination file.
Close #FileNum
End Sub
- 121
- 1
For Windows users
- "Save as" Excel file as CSV
- Open saved file with Mircrosoft Works Spreadsheet
- "Save as" the spreadsheet as CSV
- All non-numeric field now have " around them
Note this is not the same keyboard quote which has a forward & backward variety.
So if using CSV to LOAD into Mysql table, cut and paste into ENCLOSED parameter otherwise you will wonder why you get message #1083 - Field separator argument is not what is expected
Michael's answer of Aug 21 2014 is really helpful. However, I had to perform an additional step or two:
- Open the .csv file with Notepad (or equivalent)
- Replace all '' (repeated single quotes) with " (double quote)
- Replace all ; (used as delimiter in German version of Excel) with ,
- Save revised .csv file
- 30,336
This is my recipe. I hate having to do this but I had no better way. In my case, I'm not getting quotes around any field. I also needed to use UTF-8 encoding and ; instead of tabs, so this is what I ended doing.
NOTE: due to the ilegibility of putting double quotes enclosed by single quotes and so on, I've used purposely "keyboard formatting" to indicate both keystrokes and literal characters and strings.
- Insert a new column before any other in your Excel spreadsheet (click on the title of the 1st column, the entire column gets selected, right click, select "insert").
- Select the entire range of empty cells from the new column (they are selected by default). On the formula field, insert whatever you want: for instance, xxx. Press CTRL+enter to fill the entire column with the same value.
- Save the file as Unicode Text (*.txt).
- Open file in Notepad++.
- Press CTRL+f to open Search/Replace.
- Go to Replace tab.
- In "Search mode" select "Extended (\n, \r, \t, \0, \x...)".
- In the "Search" field, enter xxx plus a tab keystroke.
- In the "Replace" field, enter " (a double quote).
- Press Replace All.
- In the "Search" field, put a tab keystroke.
- In the "Replace" one, enter ";" (double quote, semicolon, double quote).
- Press Replace All.
- You are almost done. For the last double quote, enter \r in the "Search" field and "\r" (double quote, backslash, double quote) in the "Replace" one. Press Replace All.
- As long as you are not using chinese character, etc., you can change encoding to UTF-8 and save the file safely.
- 143
I used the approach below to take three columns in Excel and build the string in the fourth column.
=CHAR(34)&A2&CHAR(34)&","&CHAR(34)&B2&CHAR(34)&","&CHAR(34)&C2&CHAR(34)&""
My issue with the "''"@"''" approach is the second column of my data was a number the "''"@"''" approach did nothing with the numbers. Sometimes the second column is blank but I needed to make sure it was represented in the final text file.
- 12,847