powershell - Powershell在与文本混合的Word文档中添加多个风格化表格
问题描述
本质上,我正在制作一张发票清单作为账单,它看起来像这样(通过 powershell 使用 word,注意 word 文件不会预先存在,我创建一个新文件并开始添加):
问题是我只知道如何执行以下操作:
$word = New-Object -comobject word.application
$word.Visible = $false
$doc = $word.Documents.Add()
$Selection = $word.Selection
$Selection.Style="Title"
$Selection.Font.Bold = 1
$Selection.ParagraphFormat.Alignment = 2
$Selection.TypeText("Expected Billing")
$Selection.TypeParagraph()
$Selection.Style="No Spacing"
$Selection.Font.Bold = 0
$Selection.TypeParagraph()
$doc.SaveAs([ref]$savepath)
$doc.Close()
$word.quit()
添加文本很容易,但在添加表格时,我无法正确处理。如你看到的:
- 地址表是 2 列和 3 行但没有边框
- 发票表的数量不固定
- 第一列左对齐
- 数量和金额正确对齐(因为它们将是数字)
- 供应商和经销商为中心
实际上,编号的表格也不应该有边框,但我把它们留下了,所以你可以直观地看到。然后当然是我可以做的总计算量。但请注意它出现在表格之后,所以我想要追加表格并想知道如何在它们之后追加。有人可以帮我吗?我不知道这个的编码。
解决方案
好的,经过更多研究,我想通了。我将发布代码
$Range = $Selection.Range
$Table = $Selection.Tables.add($Selection.Range,2,5)
$Table.cell(1,1).range.text = "Item Code"
$Table.cell(2,1).range.text = "Description"
$Table.cell(1,2).range.text = "Quantity"
$Table.cell(1,3).range.text = "Amount"
$Table.cell(1,4).range.text = "SUP"
$Table.cell(1,5).range.text = "Dealer Code"
$Selection.EndKey(6) | Out-Null
您必须从添加开始并确保以 End Key(6) 结束
要编辑宽度或字体样式:
$Table.cell(1,1).Width = 222.2
$Table.cell(1,1).range.Font.bold = 1
推荐阅读
- scala - Filter async with akka-stream
- outlook-addin - 如果通过 displayDialogAsync API 打开对话框,microsoft office onSend 功能不允许发送邮件
- sql - SQL FOR JSON - Add additional array bracket
- android - Delayed execution - Best Practices
- ajax - 从 Flask 应用程序强制重新加载模板?
- css - 让 Flexbox Tiles 像对齐的文本一样流动?
- r - r - 用来自另一个数据帧的值填充时间序列中的数据
- kotlin - Set and Get the class field(s) using Delegate in Kotlin
- python - 如何使用 python 模块 gmpy2 将数字的精度设置为 10 位小数
- mysql - 如果在 where 条件中满足条件,则设置一个值