【发布时间】:2019-03-08 05:36:26
【问题描述】:
我想以编程方式在 Word 文档中添加多个表格。我尝试了以下代码来添加表格(在下面的示例代码中我没有使用循环)
Microsoft.Office.Interop.Word.Table imageTable1 = wordDoc.Tables.Add(initialRange, 1, 2, ref oMissing, ref oMissing);
imageTable1.Rows.SetHeight(40, WdRowHeightRule.wdRowHeightExactly);
imageTable1.AllowAutoFit = true;
var text = "ABC";
// Add feature name in bold in table
if (!string.IsNullOrEmpty(text))
{
Cell cell1 = imageTable1.Cell(1, 1);
cell1.Range.Bold = 1;
cell1.Range.Underline = WdUnderline.wdUnderlineSingle;
cell1.Range.Font.Size = 18;
cell1.Range.Font.AllCaps = 1;
cell1.Range.Font.Name = "Times New Roman";
cell1.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell1.Range.Text = "ABC";
}
initialRange.Collapse(WdCollapseDirection.wdCollapseEnd);
initialRange.InsertParagraphAfter();
initialRange.Collapse(WdCollapseDirection.wdCollapseEnd);
Microsoft.Office.Interop.Word.Table imageTable2 = wordDoc.Tables.Add(initialRange, 1, 2, ref oMissing, ref oMissing);
imageTable2.Rows.SetHeight(40, WdRowHeightRule.wdRowHeightExactly);
imageTable2.AllowAutoFit = true;
text = "DEF"
// Add feature name in bold in table
if (!string.IsNullOrEmpty(text))
{
Cell cell1 = imageTable2.Cell(1, 1);
//cell1.Range.InsertAfter(feature.Name + Environment.NewLine);
cell1.Range.Bold = 1;
cell1.Range.Underline = WdUnderline.wdUnderlineSingle;
cell1.Range.Font.Size = 18;
cell1.Range.Font.AllCaps = 1;
cell1.Range.Font.Name = "Times New Roman";
cell1.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell1.Range.Text = "DEF";
}
在上面的代码中,initialRange 变量是我文档中的选择范围。使用上面的代码,我得到重叠的表格,打开文档时只有最后一个表格可见。代码正确创建了所有表,但所有表都放置在同一位置,因此最后创建的表仅可见。我无法弄清楚上述代码中需要进行哪些更改才能一个接一个地显示表格。
另外,我想在表格之间添加一些文本行。如何插入文本,以便在我的文档中有表格,然后是相关文本。
谢谢
【问题讨论】:
标签: c# ms-word office-interop