convert xlsx to csv using powershell

Fredrik Söderlind 1 Reputation point
2021-10-20T19:00:18.507+00:00

Hi,

I have looked around a lot to find a script that converts xlsx-files in a folder to csv-files.

I have found script that does this but only for one file and with a certain file name.
Below works fine for one file with a certain name "list_of_names". Anyone who can rewrite this to accept all xlsx-files in a folder independent on file names?

'
Function ExcelToCsv ($File) {
$myDir = "D:\Excel"
$excelFile = "$myDir\" + $File + ".xlsx"
$Excel = New-Object -ComObject Excel.Application
$wb = $Excel.Workbooks.Open($excelFile)

foreach ($ws in $wb.Worksheets) {
    $ws.SaveAs("$myDir\" + $File + ".csv", 6)
}
$Excel.Quit()

}

$FileName = "list_of_names"
ExcelToCsv -File $FileName

Thanks!

Fredrik

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,382 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Rich Matheisen 45,011 Reputation points
    2022-07-14T02:19:16.693+00:00

    To import an Excel spreadsheet and export each workbook as a separate CSV is pretty easy once you've installed the ImportExcel module:

    $infile = "c:\junk\OneTwoThreeFour.xlsx"  
    $outdir = "c:\junk\"  
    (Open-ExcelPackage -Path $infile).psobject.properties |  
        Where-Object {$_.MemberType -eq 'ScriptProperty'} |  
            Select-Object -ExpandProperty Name |  
                ForEach-Object{  
                    $outfile = "{0}{1}.csv" -f $outdir,$_  
                    Import-Excel -Path $infile -WorksheetName $_ |  
                        Export-Csv $outfile -NoTypeInformation  
                }  
    
    1 person found this answer helpful.

  2. Rich Matheisen 45,011 Reputation points
    2021-10-20T19:50:14.66+00:00

    Try this (adjusting the path, of course):

    Function ExcelToCsv ($File) {
        $Excel = New-Object -ComObject Excel.Application
        $wb = $Excel.Workbooks.Open($File)
    
        $x = $File | Select-Object Directory, BaseName
        $n = [System.IO.Path]::Combine($x.Directory, (($x.BaseName, 'csv') -join "."))
    
        foreach ($ws in $wb.Worksheets) {
            $ws.SaveAs($n, 6)
        }
        $Excel.Quit()
    }
    
    Get-ChildItem C:\Junk\*.xlsx |
        ForEach-Object{
            ExcelToCsv -File $_
        }
    

    As long as the files have only a single workbook it should work. I didn't try it with more than one workbook.

    0 comments No comments

  3. Fredrik Söderlind 1 Reputation point
    2021-10-21T11:12:14.237+00:00

    Hi! Many thanks. I can execute your script in the PowerShell editor and it works fine. However I cannot make it work in SSIS.
    Is that something you are familiar with? The package I have created executes without errors, however nothing actually happens. It feels like its not actually running the script.

    Attached figure shows how I have configured the Process Task. I have also tried "unrestricted". It also executes without error, however nothing happens.

    142428-ps.jpg

    0 comments No comments

  4. Rich Matheisen 45,011 Reputation points
    2021-10-21T19:32:13.497+00:00

    I think it would be unusual to find a Microsoft Office application installed on a SQL server! I don't even know if that's a supported situation, but it'd give me the heebie-jeebies if it was.

    If the absence of the necessary COM object for Excel is your problem you can try the ImportExcel module. I don't think there's a direct-to-csv cmdlet, but you should be able to export rows by piping them into the regulate PowerShell Export-CSV cmdlet. The upside of this is that you don't need Excel.


  5. Fredrik Söderlind 1 Reputation point
    2021-10-21T20:27:54.96+00:00

    Thanks Rich for taking your time to answer a newbie like me =)