OPERATING SYSTEMSOS Windows

Convert CSV to Excel (XLS or XLSX) using Command line in Windows 10


Here is a tutorial to convert CSV to XLS or XLSX using the command-line interface in Windows 10. There are multiple ways to convert a CSV file to Excel formats (XLS, XLSX). You can use Microsoft Excel, a dedicated converter freeware, or an online tool to perform the conversion. But, did you know that you can also use Windows command-line interface to convert CVS to Excel? And, you don’t even need any third-party tool for that? If not, this tutorial will be pleasantly surprising for you.

In this article, we are going to show you how you can convert a CSV file to an Excel workbook. For that, we will be using a VBScript. Let us check out the script and steps that you need for conversion.

How to Convert CSV to Excel using Command line

Here are the steps to convert CSV to Excel using Command Line Interface in Windows 10:

  1. Open Notepad.
  2. Write a Visual Basic Script (VBS).
  3. Save the VBS file.
  4. Open Command Prompt in the folder.
  5. Enter the CSV to Excel conversion command.

Let’s discuss these steps in detail!

Firstly, open the Notepad application on your Windows 10 PC. You now have to write a VBScript with related commands. Below is the full script that you need to enter in the Notepad; just copy from here and paste it into your Notepad:

'======================================
' Convert CSV to Excel
'
' arg1: source - CSV pathfile
' arg2: target - Excel pathfile
'======================================

srccsvfile = Wscript.Arguments(0) 
tgtxlsfile = Wscript.Arguments(1)

'Create Spreadsheet
'Look for an existing Excel instance.
On Error Resume Next ' Turn on the error handling flag
Set objExcel = GetObject(,"Excel.Application")
'If not found, create a new instance.
If Err.Number = 429 Then '> 0
Set objExcel = CreateObject("Excel.Application")
End If

objExcel.Visible = false
objExcel.displayalerts=false

'Import CSV into Spreadsheet
Set objWorkbook = objExcel.Workbooks.open(srccsvfile)
Set objWorksheet1 = objWorkbook.Worksheets(1)

'Adjust width of columns
Set objRange = objWorksheet1.UsedRange
objRange.EntireColumn.Autofit()
'This code could be used to AutoFit a select number of columns
'For intColumns = 1 To 17
' objExcel.Columns(intColumns).AutoFit()
'Next

'Make Headings Bold
objExcel.Rows(1).Font.Bold = TRUE

'Freeze header row
With objExcel.ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
objExcel.ActiveWindow.FreezePanes = True

'Add Data Filters to Heading Row
objExcel.Rows(1).AutoFilter

'set header row gray
objExcel.Rows(1).Interior.ColorIndex = 15
'-0.249977111117893

'Save Spreadsheet, 51 = Excel 2007-2010 
objWorksheet1.SaveAs tgtxlsfile, 51

'Release Lock on Spreadsheet
objExcel.Quit()
Set objWorksheet1 = Nothing
Set objWorkbook = Nothing
Set ObjExcel = Nothing

After entering the above script, you need to save the file with the .vbs file extension. For that, click on the File > Save As option. In the Save As dialog prompt, set Save As Type to All Files and then enter the filename followed by the .vbs file extension. After that, click on the Save button to save the VBS file.

Now, go to the folder where you have saved the above VBS script and open Command Prompt in this folder.

Next, you have to enter a command with the below syntax in CMD:

VBSFilename [Input CSV file with path] [Output Excel file with path]

Let’s say you have named the VBS file as “csv-to-excel”, then your command will look something like the below one:

csv-to-excel C:UsersKOMALDesktoptwc.csv C:UsersKOMALDesktoptwc1.xlsx

In case you want to create an Excel file with the older file extension i.e., XLS, just replace xlsx with xls. Here is an example:

csv-to-excel C:UsersKOMALDesktoptwc.csv C:UsersKOMALDesktoptwc1.xls

Do remember that you have to mention the full path of both input CSV and output Excel files.

How to Convert CSV to Excel (XLS or XLSX) using Command line in Windows 10

As you enter the above command, you will get the output Excel file.

Convert CSV to Excel using Command line

In the above VBScript, there are options to adjust column width, freeze pane, apply filter, make header bold, etc. You can easily tweak the VBScript to customize these options and then run the command to convert CSV to Excel through CMD. If you are a programmer, you can further customize the script accordingly.

So, this is how you can convert CSV to Excel using Command-Line with help of a VBScript created by Jeff.

Related reads:

Convert CSV to Excel using Command line

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *