Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
|
epplus [2024/08/26 09:53] jango [Schreiben] |
epplus [2024/08/27 13:45] (aktuell) jango [Schreiben] |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| EPPlus ist die Standalone DLL für das [[coding: | EPPlus ist die Standalone DLL für das [[coding: | ||
| + | |||
| + | <code powershell> | ||
| + | $epplusDllPath = " | ||
| + | $excelFilePath = " | ||
| + | |||
| + | Add-Type -Path $epplusDllPath | ||
| + | |||
| + | $package = New-Object OfficeOpenXml.ExcelPackage | ||
| + | $package.Load([System.IO.File]:: | ||
| + | |||
| + | $ws = $package.Workbook.Worksheets[" | ||
| + | |||
| + | for ($row = 1; $row -le $ws.Dimension.End.Row; | ||
| + | $rowValues = @() | ||
| + | for ($col = 1; $col -le $ws.Dimension.End.Column; | ||
| + | $cellValue = $ws.Cells[$row, | ||
| + | $rowValues += $cellValue | ||
| + | } | ||
| + | Write-Output " | ||
| + | } | ||
| + | </ | ||
| =====Lesen===== | =====Lesen===== | ||
| <code powershell> | <code powershell> | ||
| + | # Pfad zur EPPlus-DLL und zur Excel-Datei, | ||
| $epplusDllPath = " | $epplusDllPath = " | ||
| $excelFilePath = " | $excelFilePath = " | ||
| + | # EPPlus-DLL laden | ||
| Add-Type -Path $epplusDllPath | Add-Type -Path $epplusDllPath | ||
| - | $package = New-Object OfficeOpenXml.ExcelPackage | + | # Funktion zum Laden einer Excel-Datei |
| - | $package.Load([System.IO.File]:: | + | function Load-ExcelFile { |
| + | param ( | ||
| + | [string]$filePath | ||
| + | ) | ||
| + | | ||
| + | $package.Load([System.IO.File]:: | ||
| + | return $package | ||
| + | } | ||
| - | foreach | + | # Funktion zum Auswählen eines Arbeitsblatts nach Namen |
| + | function Get-Worksheet { | ||
| + | param ( | ||
| + | [OfficeOpenXml.ExcelPackage]$package, | ||
| + | [string]$sheetName | ||
| + | ) | ||
| + | return | ||
| + | } | ||
| - | Write-Output " | + | # Funktion zum Lesen eines Werts aus einer bestimmten Zelle |
| + | function Get-CellValue { | ||
| + | param ( | ||
| + | [OfficeOpenXml.ExcelWorksheet]$worksheet, | ||
| + | [int]$row, | ||
| + | [int]$col | ||
| + | | ||
| + | return $worksheet.Cells[$row, | ||
| + | } | ||
| - | for ($row = 1; $row -le $ws.Dimension.End.Row; | + | # Funktion zum Lesen einer ganzen Zeile als Array |
| - | $rowValues = @() | + | function Get-RowValues { |
| - | for ($col = 1; $col -le $ws.Dimension.End.Column; | + | param ( |
| - | $cellValue = $ws.Cells[$row, $col].Text | + | [OfficeOpenXml.ExcelWorksheet]$worksheet, |
| - | $rowValues += $cellValue | + | [int]$row |
| - | } | + | |
| - | + | $rowValues = @() | |
| - | # | + | for ($col = 1; $col -le $worksheet.Dimension.End.Column; |
| - | Write-Output " | + | $cellValue = Get-CellValue -worksheet |
| + | $rowValues += $cellValue | ||
| } | } | ||
| + | return $rowValues | ||
| + | } | ||
| - | break | + | # Funktion zum Lesen aller Daten aus einem Arbeitsblatt |
| + | function Get-AllRows { | ||
| + | param ( | ||
| + | [OfficeOpenXml.ExcelWorksheet]$worksheet | ||
| + | ) | ||
| + | $allRows = @() | ||
| + | for ($row = 1; $row -le $worksheet.Dimension.End.Row; | ||
| + | $rowValues = Get-RowValues -worksheet $worksheet -row $row | ||
| + | $allRows += , | ||
| + | } | ||
| + | return $allRows | ||
| + | } | ||
| + | # Hauptfunktion zum Lesen und Ausgeben der Daten aus der Excel-Datei | ||
| + | function Read-ExcelFile { | ||
| + | param ( | ||
| + | [string]$filePath, | ||
| + | [string]$sheetName | ||
| + | ) | ||
| + | |||
| + | # Excel-Datei laden | ||
| + | $package = Load-ExcelFile -filePath $filePath | ||
| + | |||
| + | # Arbeitsblatt auswählen | ||
| + | $worksheet = Get-Worksheet -package $package -sheetName $sheetName | ||
| + | |||
| + | if ($null -eq $worksheet) { | ||
| + | Write-Error " | ||
| + | return | ||
| + | } | ||
| + | |||
| + | # Alle Zeilen aus dem Arbeitsblatt lesen | ||
| + | $allRows = Get-AllRows -worksheet $worksheet | ||
| + | |||
| + | # Ausgabe der Daten | ||
| + | foreach ($row in $allRows) { | ||
| + | Write-Output ($row -join ", ") | ||
| + | } | ||
| } | } | ||
| + | |||
| + | # Beispiel: Daten aus einem bestimmten Arbeitsblatt lesen und ausgeben | ||
| + | Read-ExcelFile -filePath $excelFilePath -sheetName " | ||
| </ | </ | ||
| Zeile 97: | Zeile 184: | ||
| # Datei speichern | # Datei speichern | ||
| - | $package.SaveAs([System.IO.File]:: | + | |
| + | $package.SaveAs($filePath) | ||
| # Bestätigungsausgabe | # Bestätigungsausgabe | ||