powershellpowershell-v6.0

How to convert XML based XLS file to XLSX?


I have a bunch of XLS files. On opening the file I got the prompt: format and extension don't match

Later, I found out that this is an old XML based XLS file. For that reason I couldn't directly import those files into R or SAS.

I tried opening one and use Save as to save the file in any format supported by R and SAS like XLSX or CSV etc.

The problem is there are hundreds of files, not quite viable to open and save as one by one.

Any process will be great that I can incorporate into PowerShell process.


Solution

  • Try this powershell solution:

    $Excel = New-Object -Com Excel.Application
    
    foreach ($File in (gci *xls)) {
    $Workbook = $Excel.Workbooks.Open($File.FullName) 
    $Workbook.SaveAs(($File.FullName + "x"), 51) 
    $Workbook.Close($false)}
    
    $Excel.Quit() 
    

    Or if you want the files in csv:

    $Excel = New-Object -Com Excel.Application
    
    foreach ($File in (gci *xls)) {
    $Workbook = $Excel.Workbooks.Open($File.FullName) 
    $Workbook.SaveAs($File.FullName.Replace(".xls",".csv"), 6) 
    $Workbook.Close($false)}
    
    $Excel.Quit()