Fri Dec 01, 2023 7:57 am
您好,
感谢您的询问。
我将我模拟的输入文档和完整的代码放在下面供您参考:
- Code: Select all
// 创建一个新的文档对象
Document doc = new Document();
// 从文件中加载文档内容
doc.LoadFromFile(@"输入文档.docx");
// 获取文档的第一个节
Spire.Doc.Section section = doc.Sections[0];
// 获取节中的第三个段落
Paragraph para1 = section.Paragraphs[2];
// 在节中添加一个表格,并重置单元格
Table table = section.AddTable(true);
table.ResetCells(1, 2);
// 在表格的第一行第一列添加文本“产品”,并设置字体、大小、颜色和加粗属性
TextRange range = table[0, 0].AddParagraph().AppendText("产品");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
range.CharacterFormat.TextColor = Color.Teal;
range.CharacterFormat.Bold = true;
// 在表格的第一行第二列添加文本“单价”,并设置字体、大小、颜色和加粗属性
range = table[0, 1].AddParagraph().AppendText("单价");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
range.CharacterFormat.TextColor = Color.Teal;
range.CharacterFormat.Bold = true;
// 自动调整表格的大小以适应窗口
table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
// 将表格添加到节的表格集合中
section.Tables.Add(table);
// 获取段落在节的子对象中的索引位置
int index = section.Body.ChildObjects.IndexOf(para1);
// 在段落之后插入表格
section.Body.ChildObjects.Insert(index + 1, table);
// 将修改后的文档保存到文件中
doc.SaveToFile(@"结果文档.docx", FileFormat.Docx);
如果问题仍然存在,请提供您的输入文档和您的期望实现文档或者截图给我们以帮助我们进行进一步调查,您可以附在此处或通过电子邮件发送给我们(
[email protected]),提前感谢。
Sincerely,
Ula
E-iceblue support team
Login to view the files attached to this post.