There are a few ways to convert CSV files to Excel files (.xlsx, .xlsm). But you can do it by using the windows command line. Additionally, it has you can edit the CSV file according to the need you want to export data in an Excel file. This tutorial will show you how to convert CSV to xlsx using the windows command line.
Download Practice Workbook
Download this practice workbook to exercise while you are reading this article.
8 Easy Steps to Convert CSV to XLSX Command Line
In the following image, we have provided a data set of a CSV file. Firstly, we will type Visual Basic Script (VBS) codes in the Notepad application. Then, we will save them in .vbs format. Later on, we will use the VBS code file in the windows command line and link them with the CSV file and convert them into an Excel file (.xlsx)
Step 1: Save CSV File
- Save your CSV file (source) in a specific folder.
Step 2: Open Notepad from Windows
- From the search menu, open the Notepad application.
Step 3: Insert VBS Code for Command Line to Convert CSV to XLSX
- Paste the following (Visual Basic Script) VBS code in Notepad. There are options to alter column width, freeze pane, apply Filter, give a colorful Header, and more in the VBS codes
'======================================
' Convert CSV to xlsx Command Line
'======================================
srccsvfile = Wscript.Arguments(0)
tgtxlsfile = Wscript.Arguments(1)
'Create an Excel worksheet where you want to place the file
On Error Resume Next
'Set a variable and define
Set ObjectiveFileExcel = GetObject(,"Excel.Application")
'If condition for creating new instance if the find is not found
If Err.Number = 429 Then '> 0
'Set a command to create new instance
Set ObjectiveFileExcel = CreateObject("Excel.Application")
End If
ObjectiveFileExcel.Visible = false
ObjectiveFileExcel.displayalerts=false
'Give a command to import CSV into Excel
Set ImportWbk = ObjectiveFileExcel.Workbooks.open(srccsvfile)
Set ImportWbk1 = ImportWbk.Worksheets(1)
'Set a command to adjust column widths
Set ObjectiveRng = ImportWbk1.UsedRange
ObjectiveRng.EntireColumn.Autofit()
' ObjectiveFileExcel.Columns(intColumns).AutoFit()
'Next
'Give TRUE statement to set the column Headings Bold
ObjectiveFileExcel.Rows(1).Font.Bold = TRUE
'Give TRUE statement to freeze panes for the Header Row
With ObjectiveFileExcel.ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ObjectiveFileExcel.ActiveWindow.FreezePanes = True
'Apply AutoFilter to Heading Row
ObjectiveFileExcel.Rows(1).AutoFilter
'Make Header row Interior Cyanide
ObjectiveFileExcel.Rows(1).Interior.ColorIndex = 31
'Save the worksheet
ImportWbk1.SaveAs tgtxlsfile, 51
'Make free the Lock on worksheetsheet
ObjectiveFileExcel.Quit()
Set ImportWbk1 = Nothing
Set ImportWbk = Nothing
Set ObjectiveFileExcel = Nothing
Step 4: Save VBS File
- Then, type .vbs with the file name.
- Select All Files from the Save as type.
- Click on Save.
Step 5: Add a Destination Excel File
- Place your destination Excel file (converted) in the same folder. This is the Excel file where you export data from the CSV file.
Step 6: Open Windows Command Line Box to Convert CSV to XLSX
- Press Shift + right-click .
- Select the Open PowerShell window here option to open the windows command line.
Step 7: Insert Command Line to Convert CSV to XLSX
- In the windows command line box, paste the following command.
.\convert-csv-to-excel C:\Users\Admin\Desktop\source.csv C:\Users\Admin\Desktop\converted.xlsx
- Finally, press Enter to run the command.
Step 8: Final Result
- Therefore, open the Excel file (converted). You will see the data in CSV (source) file is now converted to Excel (.xlsx) file with the applied editing with the Visual Basic Script (VBS).
Read More: How to Convert CSV to XLSX without Opening (5 Easy Methods)
Conclusion
I hope this article has given you a tutorial about converting CSV files to the xlsx files using the windows command line. All of these procedures should be learned and applied to your dataset. Take a look at the practice workbook and put these skills to the test. We’re motivated to keep making tutorials like this because of your valuable support.
If you have any questions – Feel free to ask us. Also, feel free to leave comments in the section below.
We, the Exceldemy Team, are always responsive to your queries.
Stay with us & keep learning.
Related Articles
- How to Open CSV File in Excel with Columns Automatically (3 Methods)
- How to Open CSV File with Columns in Excel (3 Easy Ways)
- Excel VBA to Read CSV File into Array (4 Ideal Examples)
- How to Convert CSV to Excel with Columns (5 Methods)
- Excel VBA: Read a Text File Line by Line (6 Related Examples)
- How to Import CSV into Existing Sheet in Excel (5 Methods)
Hi,
I used your vbs script, thanks for your work!
Ho can I set delimiter of csv?
I use pipe | as csv delimiter and all values are wrongly in one column