You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.3 KiB
38 lines
1.3 KiB
param(
|
|
[string]$RawDirectory = "$(Split-Path -Parent $PSScriptRoot)\data\raw\gansu\2024",
|
|
[string]$OutputDirectory = "$(Split-Path -Parent $PSScriptRoot)\data\processed\gansu\2024\csv"
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
New-Item -ItemType Directory -Force -Path $OutputDirectory | Out-Null
|
|
|
|
$excel = New-Object -ComObject Excel.Application
|
|
$excel.Visible = $false
|
|
$excel.DisplayAlerts = $false
|
|
try {
|
|
Get-ChildItem -LiteralPath $RawDirectory -Filter '*.xls' | ForEach-Object {
|
|
$workbook = $excel.Workbooks.Open($_.FullName)
|
|
try {
|
|
$worksheet = $workbook.Worksheets.Item(1)
|
|
$usedRange = $worksheet.UsedRange
|
|
$matrix = $usedRange.Value2
|
|
$rows = for ($row = 1; $row -le $usedRange.Rows.Count; $row++) {
|
|
$record = [ordered]@{ row_number = $row }
|
|
for ($column = 1; $column -le $usedRange.Columns.Count; $column++) {
|
|
$record["col$column"] = $matrix[$row, $column]
|
|
}
|
|
[pscustomobject]$record
|
|
}
|
|
$target = Join-Path $OutputDirectory ($_.BaseName + '.csv')
|
|
$rows | Export-Csv -LiteralPath $target -NoTypeInformation -Encoding utf8
|
|
}
|
|
finally {
|
|
$workbook.Close($false)
|
|
}
|
|
}
|
|
}
|
|
finally {
|
|
$excel.Quit()
|
|
[Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
|
|
}
|
|
|
|
|