I need to extract a worksheet called Approval_Logs from a few different xlsx spreadsheets and convert them to text files.
I've done this under linux using gnumeric's ssconvert command, but I haven't found a way to do this in windows, and I'm trying to make a user-friendly powershell script to automate this task.
Thanks!
The following snippet might give you a starting point.
You will probably have to find the correct path of the Interop dll on your hardrive and then adjust the add-type
cmdlet.
$dir
is the directory from which the workbooks are opened and into which they're written.
$dir = convert-path ~/ZZZ/Excel/Export-CSV/
add-type -path 'C:\Program Files (x86)\Microsoft Office\Office16\DCF\Microsoft.Office.Interop.Excel.dll'
$xls = new-object Microsoft.Office.Interop.Excel.ApplicationClass
$xls.visible = $true
$xls.displayAlerts = $false
foreach ($wbFile in get-childItem $dir\*.xls*) {
$wb = $xls.workbooks.open($wbFile.fullName)
try {
$sh = $wb.sheets('Approval_Logs')
}
catch {
if ($_.exception.message -match 'Invalid index.') {
write-host "Expected sheet not found in $($wb.name)"
$wb.close()
continue
}
throw $_
}
$sh.select()
$csvFile = "${dir}$($wbFile.basename).csv"
$wb.saveAs($csvFile, 6, $false)
write-host "$csvFile was saved"
$wb.close()
}
$xls.quit()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments