将表对象导出到CSV

时间:2018-07-17 15:27:22

标签: powershell csv ms-word

我们从word doc中提取了所需的表,但是有人可以帮助...该如何将这个表对象$LETable导出为CSV,或者以表格式导出下面以CSV格式获取的值。 / p>

$objWord = New-Object -Com Word.Application
$filename = 'D:\Files\Scan1.doc'
$objDocument = $objWord.Documents.Open($filename)

$LETable = $objDocument.Tables.Item(4)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count

$obj = New-Object -TypeName PSCustomObject

Write-Output "Starting to write... "

for($r=1; $r -le $LETableRows; $r++) {
    for($c=1; $c -le $LETableCols; $c++) {
        #Write-Host $r "x" $c
        $content = $LETable.Cell($r,$c).Range.Text
        Write-Host $content
    }
}
$objDocument.Close()
$objWord.Quit()
# Stop Winword Process
$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objWord)

1 个答案:

答案 0 :(得分:0)

还没有准备好解决方案,但是我没有更多时间使用ATM。

脚本输出所有用双引号引起来并用逗号分隔的字段,并将其存储在变量$ RawCSV中,然后将其传递给ConvertFrom-Csv 我在单元格值中用cr / lf和char 7遇到了麻烦,我一无所获

## Q:\Test\2018\07\17\SO_51385204.ps1

$CsvName = '.\Test.csv'
$filename = (Get-Item ".\Test-text.docx").FullName
$tableNum = 4
$delimiter = ','

$objWord = New-Object -Com Word.Application
$objWord.Visible = $true # $false

$objDocument = $objWord.Documents.Open($filename)

$LETable = $objDocument.Tables.Item($tableNum)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count

Write-Output "Starting to write... "
# "Table rows:{0} cols:{1}" -f $LETableRows,$LETableCols

$RawCSV = for($r=1; $r -le $LETableRows; $r++) {
    $content= @()
    for($c=1; $c -le $LETableCols; $c++) {
        #Write-Host ("R:{0},C:{1}" -f $r,$c)
        $content += ("`"{0}`"" -f $LETable.Cell($r,$c).Range.Text -replace "(`r|`n|`t)|$([char]7)?")
    }
    $Content -join $delimiter
}
$Csv = $RawCSV | ConvertFrom-Csv

$objDocument.Close()
$objWord.Quit()
# Stop Winword Process
$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objWord)

$Csv
$Csv | Export-Csv $CsvName -NoTypeInformation