gpt4 book ai didi

powershell - 使用 PowerShell 在 MS Word 中编写表格的内容。订单不正确

转载 作者:行者123 更新时间:2023-12-02 23:15:38 24 4
gpt4 key购买 nike

下面是代码:

$objWord = New-Object -Com Word.Application

$filename = 'C:\Chicago_NewUser.doc'
$objDocument = $objWord.Documents.Open($filename)

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


Write-output "Starting to write... "

for($r=0; $r -le $LETableRows; $r++) {
for($c=0; $c -le $LETableCols; $c++) {
Write-host $r "x" $c
$content = $LETable.Cell($r,$c).Range.Text
Write-host $content
}
}
$objDocument.Close()
$objWord.Quit()

该文档是 2003 Word 文档,带有一个表格 - 2 列和 3 行。但是打印的输出以第 3 行和第 1 列中的文本开始。

Word 文档可在此处获得:

https://docs.google.com/document/d/1W0usG4ASsvd3PqQ20l7P8rq0hWBsYlrI0iM1S5ZcumU/edit

最佳答案

只需从 1 开始您的索引就可以了。我还在末尾添加了一行以正确关闭 winword。

$objWord = New-Object -Com Word.Application

$filename = 'c:\silogix\SILOGIX.doc'
$objDocument = $objWord.Documents.Open($filename)

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


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)

关于powershell - 使用 PowerShell 在 MS Word 中编写表格的内容。订单不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11355066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com