locked
Using Powershell to convert .CSV to .XLSX on PC without Excel installed RRS feed

  • Question

  • On PC with Excel installed I wrote a short script to convert .CSV to XLSX files. I want to run this script on PC without Excel. My question - what is the module name for the command: 

    $xl = New-Object -COM "Excel.Application"

    Where I can find this file? How can I execute this command in case I will find a file name?

    Here is my script:

    Param(
      [string]$CSV_IN ) 
    
    Write-Host "--- Start ----"
    
    $XLS_out =  $CSV_IN.ToUpper().Replace(".CSV",".XLSX")
    
    $FileExists = Test-Path $XLS_out
    
    If ($FileExists -eq $True) 
    { 
        Remove-Item $XLS_out
    }
     
    $xl = New-Object -COM "Excel.Application"
    
    $xl.Visible = $false 
    
    $Wb = $xl.workbooks.Open($CSV_IN) 
    
    $ws = $wb.Worksheets.Item(1)
    
    $ws.Columns.item(1).columnWidth = 40
    $ws.Columns.item(2).columnWidth = 15
    $ws.Columns.item(2).Numberformat="0.00"
    $ws.Columns.item(3).columnWidth = 15 
    $ws.Columns.item(4).columnWidth = 15  
      
    # Save & close the Workbook as XLSX. 
    $Wb.SaveAs($XLS_out, 51) 
    
    $Wb.Close() 
    
    $xl.Quit()
       
    Write-Host "--- End ----"

    Thanks,

    zb

    Thursday, February 28, 2019 10:37 PM

All replies

  • If you don't have Excel installed that will not work. You could use the Powershell module ImportExcel. Do you know that Excel can open CSV files without  problems?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by jrv Thursday, February 28, 2019 11:50 PM
    Thursday, February 28, 2019 11:23 PM