![如何使用 PowerShell 加粗 Microsoft Word 表格单元格中的某些行](https://linux22.com/image/1674036/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20PowerShell%20%E5%8A%A0%E7%B2%97%20Microsoft%20Word%20%E8%A1%A8%E6%A0%BC%E5%8D%95%E5%85%83%E6%A0%BC%E4%B8%AD%E7%9A%84%E6%9F%90%E4%BA%9B%E8%A1%8C.png)
我目前正在创建一个 PowerShell 脚本来接收用户的输入并将其格式化为 Microsoft Word 文档作为输出(参考https://learn-powershell.net/2015/01/24/creating-a-table-in-word-using-powershell/)。
我的脚本能够将用户输入输出到表格的各个单元格,但我想将单元格中的某些行加粗,并对其中一些行加下划线。
我了解我可以对整个单元格实施格式化,但我想咨询如何对单元格中的某些行实施格式化?
以下代码示例:
$ContentCell = ''
$Word = New-Object -ComObject Word.Application
$Word.Visible = $True
$Document = $Word.Documents.Add()
$Selection = $Word.Selection
$Table = $Selection.Tables.add(
$Selection.Range,6,4,
[Microsoft.Office.Interop.Word.WdDefaultTableBehavior]::wdWord9TableBehavior,
[Microsoft.Office.Interop.Word.WdAutoFitBehavior]::wdAutoFitContent
)
##Header
$Table.cell(1,1).range.Bold=1
$Table.cell(1,1).range.text = 'S/N'
$Table.cell(1,2).range.Bold=1
$Table.cell(1,2).range.text = 'Content'
$Table.cell(2,1).range.text = '1'
$UserInput = Read-Host - Prompt 'Enter your header here'
$ContentCell = $UserInput
$UserInput = Read-Host - Prompt 'Enter your header here'
$ContentCell = $ContentCell + '
' + $UserInput
$Table.cell(2,2).range.text = $ContentCell
答案1
我认为你的观点非常接近
$Table.cell(1,1).range.Bold=1
$Table.cell(1,2).range.Bold=1
将 更改=1
为=$True
。
$Table.cell(1,1).range.Bold=$True
$Table.cell(1,2).range.Bold=$True
您还需要在需要下划线的地方添加行:
$Table.cell(1,1).range.Underline=$True
如果这不能完全满足您的需求;在创建出色的“ImportExcel”模块之前,我在 Excel 中做了类似的事情,并且我用来使单元格加粗的字符串是:
$Stats.Cells.Item("1","C").Font.Bold=$True
希望它会像将 更改为 一样简单=1
,=$True
但如果不是,我的 Excel 示例可能会帮助您完成剩下的工作。