以下代码返回正常,并使用以下数据生成CSV文件:
Borrowers Name
---------------
Conrad
Luke
Shaw
预期输出:Conrad,Luke, Shaw
(全部一行-值用逗号分隔)
$DestinationFileName = $DestinationFile
$SourceFileName = $SourceFile
$tableNum = $tableNumber
$DatabaseTable = $DatabaseTableName
$delimiter = ','
$objWord = New-Object -Com Word.Application
$objWord.Visible = $false # $false
$objDocument = $objWord.Documents.Open($SourceFileName,$false,$true)
$LETable = $objDocument.Tables.Item($tableNum)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count
$RawCSV = for($r=1; $r -le $LETableRows; $r++) {
$content= @()
for($c=1; $c -le $LETableCols; $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 $DestinationFileName -NoTypeInformation
答案 0 :(得分:0)
以下代码可以解决问题:
$csv.'Borrowers Name' -join ", "
您在这里要做的是访问所需的属性,然后使用-join
将其显示在一行中。您可以在双引号中指定定界符,以便对其进行修改(根据示例,尚不清楚是否要在逗号后留空格)。