How to Convert CSV to XLSX – 8 Steps

In the following image, there is a CSV file. To convert it into an Excel file (.xlsx)

Easy Steps to Convert CSV to XLSX Command Line

Step 1 – Save the CSV File

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

Easy Steps to Convert CSV to XLSX Command Line


Step 2 – Open the Windows Notepad

  • In the search menu, open the Notepad application.

Easy Steps to Convert CSV to XLSX Command Line


Step 3 – Enter a VBS Code from the 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, add color to Headers, among other in 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 the VBS File

  • Enter .vbs with the file name.
  • Select All Files in Save as type.
  • Click 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.

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 Open PowerShell window here to open the Windows command line.

Sample Data

Notes: The windows command line is located in Open PowerShell window here option in Windows 10.

Step 7 – Enter a Command Line to Convert the CSV file to XLSX

  • Enter the following command.
.\convert-csv-to-excel C:\Users\Admin\Desktop\source.csv C:\Users\Admin\Desktop\converted.xlsx
  • Press Enter to run the command.

Sample Data


Step 8 – Final Result

  • Open the Excel file (converted). You will see that the data in the CSV (source) file is converted to Excel (.xlsx).

Sample Data

Read More: How to Convert CSV to XLSX


Download Practice Workbook

Download the practice workbook.


Related Articles


<< Go Back to How to Convert CSV to Excel | Import CSV to Excel | Importing Data in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Bhubon Costa
Bhubon Costa

Bhubon Costa, B.Sc. in Naval Architecture & Marine Engineering from Bangladesh University of Engineering & Technology, has worked with the ExcelDemy since 2021. Currently, he has been working as a reviewer. Notably, he has written over 90 articles and led several VBA content development teams. He has a great passion for the fields of data analytics and data science. His areas of expertise include Excel VBA, Power Query, Pivot Table, Power BI, MySQL, PostgreSQL, machine learning, and Python... Read Full Bio

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

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo