Moin!
Kennt sich hier jemand mit Powershell und (idealerweise in Verbindung mit) der Verarbeitung von Excel-Dateien aus?
Ich habe eine 7-spaltige Tabelle, die ich gern nach eindeutigen Wert-Kombinationen in Spalte C & D in jeweils eine Excel-Datei aufteilen will.
Bsp:
A | B | C | D | E | F | G | |
---|---|---|---|---|---|---|---|
1 | Bli | Bla | #1 | $1 | Blub | xyz | uvw |
2 | Bli | Bla | #1 | $2 | Blub | xyz | uvw |
3 | Bli | Bla | #2 | $1 | Blub | xyz | uvw |
4 | Bli | Bla | #2 | $2 | Blub | xyz | uvw |
5 | Blo | Blu | #2 | $2 | Blub | xyz | uvw |
Nach der Aufteilung wären das also vier Dateien, benannt nach den Werten in C &D:
#1_$1.xlsx:
A | B | C | D | E | F | G | |
---|---|---|---|---|---|---|---|
1 | Bli | Bla | #1 | $1 | Blub | xyz | uvw |
#1_$2xlsx
A | B | C | D | E | F | G | |
---|---|---|---|---|---|---|---|
1 | Bli | Bla | #1 | $2 | Blub | xyz | uvw |
#2_$1.xlsx
A | B | C | D | E | F | G | |
---|---|---|---|---|---|---|---|
1 | Bli | Bla | #2 | $1 | Blub | xyz | uvw |
#2_$2.xlsx
A | B | C | D | E | F | G | |
---|---|---|---|---|---|---|---|
1 | Bli | Bla | #2 | $2 | Blub | xyz | uvw |
2 | Blo | Blu | #2 | $2 | Blub | xyz | uvw |
Mit Hilfe von diversen ähnlichen Use-Case-Scripten im Netz (die aber immer nur nach den Werten in einer Spalte aufteilen, nicht zwei) hab ich mir bisher Folgendes zusammengeschustert:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
Write-Host "Starting ..."
Write-Host "Choose Excel File"
# Show a file dialog to select the input Excel file
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
if ($openFileDialog.ShowDialog() -eq "OK") {
$excelFile = $openFileDialog.FileName
} else {
Write-Host "No File Selected. Done."
Exit
}
# Create the output subfolder if it doesn't exist
$outputFolder = Join-Path (Split-Path $excelFile) "output"
Write-Host "Create Output Folder: " $outputFolder
# If subfolder doesn't exist, create it
if (-not (Test-Path $outputFolder)) {
New-Item -ItemType Directory -Path $outputFolder | Out-Null
}
Write-Host "Load Excel File"
# Load the Excel file
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open($excelFile)
$worksheet = $workbook.Worksheets.Item(1)
# Get unique value combinations in columns 3 and 4
$uniqueValues = $worksheet.Range("C1:D" + $worksheet.UsedRange.Rows.Count) |
Select-Object -Unique |
ForEach-Object { "$($_.Item(1))_$($_.Item(2))" }
Write-Host "Working ..."
# Loop through unique value combinations and create output files
foreach ($value in $uniqueValues) {
# Filter table based on unique value combination
$filteredTable = $worksheet.Range("A1:G" + $worksheet.UsedRange.Rows.Count) |
Select-Object -Skip 1 |
Where-Object { "$($_.Item(3))_$($_.Item(4))" -eq $value }
# Create new workbook and worksheet for output
$newWorkbook = $excel.Workbooks.Add()
$newWorksheet = $newWorkbook.Worksheets.Item(1)
# Copy filtered table to output worksheet
$filteredTable | ForEach-Object { $newWorksheet.Cells.Item($_.Row, $_.Column).Value2 = $_.Value2 }
Write-Host "Saving File"
# Save output file and close workbook
$outputFilePath = Join-Path $outputFolder "$value.xlsx"
Write-Host "Create File: " $outputFilePath
$newWorkbook.SaveAs($outputFilePath)
$newWorkbook.Close()
}
Write-Host "Close"
# Close input file and quit Excel
$workbook.Close()
$excel.Quit()
Write-Host "Done"
Allerdings funktioniert das nicht, weil ich bei der Verarbeitung oder Ausgabe irgendeinen Fehler mache. Der Dateiname ist immer nur „System.__ComObject_System.__ComObject.xlsx“
Mit der $value-Variable scheint also etwas nicht so zu funktionieren, wie ich das gerne hätte.
Ich hab leider null Ahnung von Powershell und keine Ahnung, wo ich da ansetzen soll.