How to Convert CSV to XLSX Command Line (with Easy Steps)

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)

Easy Steps to Convert CSV to XLSX Command Line

Step 1: Save CSV File

  • Save your CSV file (source) in a specific folder.

Easy Steps to Convert CSV to XLSX Command Line


Step 2: Open Notepad from Windows

  • From the search menu, open the Notepad application.

Easy Steps to Convert CSV to XLSX Command Line


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

Easy Steps to Convert CSV to XLSX Command Line


Step 4: Save VBS File

  • Then, type .vbs with the file name.
  • Select All Files from the Save as type.
  • Click on Save.

Easy Steps to Convert CSV to XLSX Command Line


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.

Easy Steps to Convert CSV to XLSX Command Line


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.

Sample Data

Notes: The windows command line is located under the Open PowerShell window here option in Windows 10. It was formerly titled Open Command Prompt in this folder in prior versions.

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
  • Finally, press Enter to run the command.

Sample Data


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).

Sample Data

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

Bhubon Costa

Bhubon Costa

Myself, Bhubon Costa, an engineer, is currently employed with Exceldemy as an Excel & VBA Content Developer. I appreciate solving analytical difficulties, finding practical answers, and, for the most part, I enjoy taking on new tasks. I write articles on Exceldemy about various ways to get out of Microsoft Excel's stuck conditions. My passion is to leave my mark on the world through my work and to have an impact on the community who benefit from it.

2 Comments
  1. 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

      • You can modify the code to convert Pipe Delimited CSV files to XLSX using the Command Line.
      '======================================
      ' 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)
      
      'Code for Pipe Delimiter
      With ImportWbk1.QueryTables.Add("TEXT;" & srccsvfile, ImportWbk1.Range("$A$1"))
          .FieldNames = True
          .RowNumbers = False
          .FillAdjacentFormulas = False
          .RefreshOnFileOpen = False
          .RefreshStyle = xlInsertDeleteCells
          .SaveData = True
          .AdjustColumnWidth = True
          .RefreshPeriod = 0
          .TextFilePromptOnRefresh = False
          .TextFilePlatform = 437
          .TextFileStartRow = 1
          .TextFileParseType = xlDelimited
          .TextFileTextQualifier = xlTextQualifierDoubleQuote
          .TextFileConsecutiveDelimiter = False
          .TextFileTabDelimiter = False
          .TextFileSemicolonDelimiter = False
          .TextFileCommaDelimiter = False
          .TextFileSpaceDelimiter = False
          .TextFileOtherDelimiter = "|"
          .TextFileColumnDataTypes = Array(1, 1, 1)
          .TextFileTrailingMinusNumbers = True
          .Refresh False
      End With
      
      '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
      • Then, type this on the PowerShell and hit ENTER.
      .\convert-csv-to-excel C:\Users\Admin\Desktop\source.csv C:\Users\Admin\Desktop\converted.xlsx

Leave a reply

ExcelDemy
Logo