How to export a CSV to Excel using Powershell

前端 未结 8 1583
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 05:17

I\'m trying to export a complete CSV to Excel by using Powershell. I stuck at a point where static column names are used. But this doesn\'t work if my CSV has generic unknow

相关标签:
8条回答
  • 2020-11-27 05:54

    Why would you bother? Load your CSV into Excel like this:

    $csv = Join-Path $env:TEMP "process.csv"
    $xls = Join-Path $env:TEMP "process.xlsx"
    
    $xl = New-Object -COM "Excel.Application"
    $xl.Visible = $true
    
    $wb = $xl.Workbooks.OpenText($csv)
    
    $wb.SaveAs($xls, 51)
    

    You just need to make sure that the CSV export uses the delimiter defined in your regional settings. Override with -Delimiter if need be.


    Edit: A more general solution that should preserve the values from the CSV as plain text. Code for iterating over the CSV columns taken from here.

    $csv = Join-Path $env:TEMP "input.csv"
    $xls = Join-Path $env:TEMP "output.xlsx"
    
    $xl = New-Object -COM "Excel.Application"
    $xl.Visible = $true
    
    $wb = $xl.Workbooks.Add()
    $ws = $wb.Sheets.Item(1)
    
    $ws.Cells.NumberFormat = "@"
    
    $i = 1
    Import-Csv $csv | ForEach-Object {
      $j = 1
      foreach ($prop in $_.PSObject.Properties) {
        if ($i -eq 1) {
          $ws.Cells.Item($i, $j++).Value = $prop.Name
        } else {
          $ws.Cells.Item($i, $j++).Value = $prop.Value
        }
      }
      $i++
    }
    
    $wb.SaveAs($xls, 51)
    $wb.Close()
    
    $xl.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
    

    Obviously this second approach won't perform too well, because it's processing each cell individually.

    0 讨论(0)
  • 2020-11-27 06:03

    This is a slight variation that worked better for me.

    $csv = Join-Path $env:TEMP "input.csv"
    $xls = Join-Path $env:TEMP "output.xlsx"
    
    $xl = new-object -comobject excel.application
    $xl.visible = $false
    $Workbook = $xl.workbooks.open($CSV)
    $Worksheets = $Workbooks.worksheets
    
    $Workbook.SaveAs($XLS,1)
    $Workbook.Saved = $True
    
    $xl.Quit()
    
    0 讨论(0)
  • 2020-11-27 06:04

    If you want to convert CSV to Excel without Excel being installed, you can use the great .NET library EPPlus (under LGPL license) to create and modify Excel Sheets and also convert CSV to Excel really fast!

    Preparation

    1. Download the latest stable EPPlus version
    2. Extract EPPlus to your preferred location (e.g. to $HOME\Documents\WindowsPowerShell\Modules\EPPlus)
    3. Right Click EPPlus.dll, select Properties and at the bottom of the General Tab click "Unblock" to allow loading of this dll. If you don't have the rights to do this, try [Reflection.Assembly]::UnsafeLoadFrom($DLLPath) | Out-Null

    Detailed Powershell Commands to import CSV to Excel

    # Create temporary CSV and Excel file names
    $FileNameCSV = "$HOME\Downloads\test.csv"
    $FileNameExcel = "$HOME\Downloads\test.xlsx"
    
    # Create CSV File (with first line containing type information and empty last line)
    Get-Process | Export-Csv -Delimiter ';' -Encoding UTF8 -Path $FileNameCSV
    
    # Load EPPlus
    $DLLPath = "$HOME\Documents\WindowsPowerShell\Modules\EPPlus\EPPlus.dll"
    [Reflection.Assembly]::LoadFile($DLLPath) | Out-Null
    
    # Set CSV Format
    $Format = New-object -TypeName OfficeOpenXml.ExcelTextFormat
    $Format.Delimiter = ";"
    # use Text Qualifier if your CSV entries are quoted, e.g. "Cell1","Cell2"
    $Format.TextQualifier = '"'
    $Format.Encoding = [System.Text.Encoding]::UTF8
    $Format.SkipLinesBeginning = '1'
    $Format.SkipLinesEnd = '1'
    
    # Set Preferred Table Style
    $TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium1
    
    # Create Excel File
    $ExcelPackage = New-Object OfficeOpenXml.ExcelPackage 
    $Worksheet = $ExcelPackage.Workbook.Worksheets.Add("FromCSV")
    
    # Load CSV File with first row as heads using a table style
    $null=$Worksheet.Cells.LoadFromText((Get-Item $FileNameCSV),$Format,$TableStyle,$true) 
    
    # Load CSV File without table style
    #$null=$Worksheet.Cells.LoadFromText($file,$format) 
    
    # Fit Column Size to Size of Content
    $Worksheet.Cells[$Worksheet.Dimension.Address].AutoFitColumns()
    
    # Save Excel File
    $ExcelPackage.SaveAs($FileNameExcel) 
    
    Write-Host "CSV File $FileNameCSV converted to Excel file $FileNameExcel"
    
    0 讨论(0)
  • 2020-11-27 06:05

    I had some problem getting the other examples to work.

    EPPlus and other libraries produces OpenDocument Xml format, which is not the same as you get when you save from Excel as xlsx.

    macks example with open CSV and just re-saving didn't work, I never managed to get the ',' delimiter to be used correctly.

    Ansgar Wiechers example has some slight error which I found the answer for in the commencts.

    Anyway, this is a complete working example. Save this in a File CsvToExcel.ps1

    param (
    [Parameter(Mandatory=$true)][string]$inputfile,
    [Parameter(Mandatory=$true)][string]$outputfile
    )
    
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $false
    
    $wb = $excel.Workbooks.Add()
    $ws = $wb.Sheets.Item(1)
    
    $ws.Cells.NumberFormat = "@"
    
    write-output "Opening $inputfile"
    
    $i = 1
    Import-Csv $inputfile | Foreach-Object { 
        $j = 1
        foreach ($prop in $_.PSObject.Properties)
        {
            if ($i -eq 1) {
                $ws.Cells.Item($i, $j) = $prop.Name
            } else {
                $ws.Cells.Item($i, $j) = $prop.Value
            }
            $j++
        }
        $i++
    }
    
    $wb.SaveAs($outputfile,51)
    $wb.Close()
    $excel.Quit()
    write-output "Success"
    

    Execute with:

    .\CsvToExcel.ps1 -inputfile "C:\Temp\X\data.csv" -outputfile "C:\Temp\X\data.xlsx"
    
    0 讨论(0)
  • 2020-11-27 06:06

    This topic really helped me, so I'd like to share my improvements. All credits go to the nixda, this is based on his answer.

    For those who need to convert multiple csv's in a folder, just modify the directory. Outputfilenames will be identical to input, just with another extension.

    Take care of the cleanup in the end, if you like to keep the original csv's you might not want to remove these.

    Can be easily modifed to save the xlsx in another directory.

    $workingdir = "C:\data\*.csv"
    $csv = dir -path $workingdir
    foreach($inputCSV in $csv){
    $outputXLSX = $inputCSV.DirectoryName + "\" + $inputCSV.Basename + ".xlsx"
    ### Create a new Excel Workbook with one empty sheet
    $excel = New-Object -ComObject excel.application 
    $excel.DisplayAlerts = $False
    $workbook = $excel.Workbooks.Add(1)
    $worksheet = $workbook.worksheets.Item(1)
    
    ### Build the QueryTables.Add command
    ### QueryTables does the same as when clicking "Data » From Text" in Excel
    $TxtConnector = ("TEXT;" + $inputCSV)
    $Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
    $query = $worksheet.QueryTables.item($Connector.name)
    
    ### Set the delimiter (, or ;) according to your regional settings
    ### $Excel.Application.International(3) = ,
    ### $Excel.Application.International(5) = ;
    $query.TextFileOtherDelimiter = $Excel.Application.International(5)
    
    ### Set the format to delimited and text for every column
    ### A trick to create an array of 2s is used with the preceding comma
    $query.TextFileParseType  = 1
    $query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
    $query.AdjustColumnWidth = 1
    
    ### Execute & delete the import query
    $query.Refresh()
    $query.Delete()
    
    ### Save & close the Workbook as XLSX. Change the output extension for Excel 2003
    $Workbook.SaveAs($outputXLSX,51)
    $excel.Quit()
    }
    ## To exclude an item, use the '-exclude' parameter (wildcards if needed)
    remove-item -path $workingdir -exclude *Crab4dq.csv
    
    0 讨论(0)
  • 2020-11-27 06:09

    I am using excelcnv.exe to convert csv into xlsx and that seemed to work properly. You will have to change the directory to where your excelcnv is. If 32 bit, it goes to Program Files (x86)

    Start-Process -FilePath 'C:\Program Files\Microsoft Office\root\Office16\excelcnv.exe' -ArgumentList "-nme -oice ""$xlsFilePath"" ""$xlsToxlsxPath"""
    
    0 讨论(0)
提交回复
热议问题