Brief overview and best practices on how to take an Excel (.xlsx) file and convert it into a comma delimited values (CSV or .csv) file. This is useful if you’re trying to upload files to a database or other systems that do not support .xlsx or other files. You can simply convert them to .csv so the system can ingest them in a compatible format.
This tutorial will cover how-to execute this and some basic best practices.
Though this is pretty basic IT knowledge, it can be used as a useful How-To for beginners and/or used as a living document you can refer people to. Lastly, even though it’s basic file conversion, there’s some tips on what to watch out for when converting from Excel to CSV.
Step 1: Start with a Saved or Working Excel File
So we can work from the same file at the outset and control for potential outliers/corruptions, I’m uploading this .xlsx file. It contains my top 10 most misused or non-existent words that one should ever fall victim to.
It’s only ten rows across two columns so it’s not a lot of data. You don’t have to use this if you don’t want to, but if you want a test file, this one will work just fine.
Regardless, the steps below will work for any standard Excel doc.
b6a4e7c8c5ab20d4b925e5606a9f4309136882dcd9b6f3bfe03bf285b497c8cc_example_file_to_convert.xlsx (8.7 KB)
Step 2: Click “File” on the Ribbon
Once you have the file above (or other Excel file) open, go to the “File” tab on the uppermost ribbon. Unless configured otherwise it is usually in the upper lefthand corner of an open Excel document.
Once you have the dropdown menu open after hovering over “File”, click “Save As” from the tab’s dropdown menu. A pop-up menu will appear once you have done so. The attached image is a version of what this would look like on a Mac. PC’s will be very similar.
Step 3: Save the Excel (.xlsx) File into a CSV (.csv)
In the form of a “File Format” option the menu will ask you what type of file you want to save your current .xlsx into. The full list of conversion options will vary by the type of operating system you have, but there’s a few common types that are pretty consistent across the board.
The highlighted option in the attached image is the most general of .csv file options. Others (e.g. MS-DOS) are more specific in their use-case. Unless you have a known reason for picking another variety, it is advised to pick the .csv option that has no explicit verbiage in the naming convention as shown in the image.
Author’s Note:
You’ll notice that the photo shows me saving this to my applications file. This is not an advisable location for storing files of this type. I am only doing this so that I’m not showing you other files. Choose the file location that makes the most sense for your use-case.
Step 4: Best Practices and Notes
Once you’ve selected your file type and location you are technically done because your file has been converted, but there’s a few “gotchas” to consider when making conversions.
- .csv does not support multiple tabs like .xslx does (basic conversion consideration)
If you have an Excel file with multiple tabs, you’ll need to convert each tab to it’s own .xlsx file and convert each to a .csv separately. Typically, you’ll get a warning of potential data loss if you’re using the “Save As” wizard, but the error message is not always explicit as to what you’re going to lose.
- The default file opener for .csv files in most systems is Excel (slightly more advanced consideration)
This is because Excel is smart enough to break files into rows and columns better than most (if not all user-friendly) programs. However, it is not the only way these types of files can be opened. In fact, if you’re having trouble importing a .csv into a database, I would advise that you open it up in your favorite text editor instead. That way, you can see what the root cause of a potential error is because Excel will often mask these errors making it look like nothing is wrong because it is smart enough to correct these errors behind the covers.
The image attached to this step shows the example file in .csv format. Wherever you put the file you converted in previous steps, right click it, go to “Open With”, and you’ll see your default program as well as a list of other options. If you open the file with a text editor (e.g. Notepad, Notepad++, TextEdit, Sublime, Atom, etc…), you’ll be able to view the file as the “computer sees it”. This will help you navigate upload errors in a database.
- File header best practice (advanced consideration)
In the file in step one, you’ll notice that the second column, column B, has spaces in its header. This is not advised if you want to upload a .csv file into another system and carry the headers over. This is because it can cause two potential problems in a database:
a) If it uploads with a header, you will have to wrap what is now your column name in double quotes every time you call it in a query. It’s not an impediment to uploading all the time, but it can be really annoying if you have to write it that way over and over again. Also, it will prevent auto-complete if your database program supports it.
b) It could cause an upload error in some systems because they’re trying to prevent the troubles in “a” above, and it could prevent you from uploading the file entirely if you do not remove or fix the header.
To prevent this, simply snake_case or CamelCase the header so there are no spaces in its name.
Whew!
I know that is a lot of text for a seemingly simple exercise, but I cannot tell you how many times I’ve either had to (a) teach someone how to simply convert a file to a CSV, or (b) had issues uploading a file into a database due to elementary issues.
There’s a lot more to this including, non UTF-8 supported characters, data types, casting, fixing .csv upload errors through vim or bash editing… All topics for another time. For now, that’s a wrap.
Hope you found this helpful!