如何使用 C# 向 MS Word 文档中的表格添加行

如何使用 C# 向 MS Word 文档中的表格添加行

常见问题及解决方案.

无效输入错误:确保上传的文件是有效的 Word 文档且水印文本非空。

  1. 动态内容管理: - 性能问题:对于大文件,通过直接从磁盘处理文件而不是使用流来优化内存使用。.
  2. 灵活性: - 如何在 .NET 中使用 Aspose.Words 动态组装文档.
  3. 自动化: - 加载多个 Word 模板.

常见问题

  1. 批注未显示:确保批注已正确附加到文档结构中有效的段落或其他内容节点。检查 Word 视图设置中是否启用了批注显示。.
  2. 确保评论正确地附加到文档结构中有效的段落或其他内容节点上。.

将模板合并为单个文档

保存最终组装的文档

文档组装涉及将多个 Word 模板或文件动态合并为一个统一的文档。此技术可用于生成报告、提案或综合性文档。使用 Aspose.Words 为 .NET,您可以以编程方式自动化此过程。.

Install-Package Aspose.Words

前置条件:准备动态文档组装

为您的操作系统安装 .NET SDK。.

using Aspose.Words;
using Aspose.Words.Tables;

将 Aspose.Words 添加到您的项目中: dotnet add package Aspose.Words

准备要合并的 Word 模板或文件,例如 Template1.docxTemplate2.docxTemplate3.docx。.

Document MSWordDocument = new Document(@"MS Word.docx");

动态组装 Word 文档的分步指南

步骤 1:加载多个 Word 模板.

Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

说明:此代码将三个独立的 Word 文档加载到内存中。

步骤 2:将模板合并为单个文档.

Row row = new Row(MSWordDocument);

说明:此代码将 Template2.docxTemplate3.docx 的内容追加到 Template1.docx,并保留原始格式。

步骤 3:保存最终组装的文档.

using Aspose.Words;
using Aspose.Words.Tables;

// Open MS Word Document
Document MSWordDocument = new Document(@"BigDocument.docx");

// Get the Table by index
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

// Create a new Row class object
Row row = new Row(MSWordDocument);

// Add three Cells to Row's cells collection
for (int i = 0; i < 3; i++)
{
    Cell cell = new Cell(MSWordDocument);
    cell.AppendChild(new Paragraph(MSWordDocument));
    cell.FirstParagraph.Runs.Add(new Run(MSWordDocument, "Text in Cell " + i));
    row.Cells.Add(cell);
}

// Insert new Row after the first Row
tableToAddRowsTo.Rows.Insert(1, row);

// Clone an existing Row from Table
Row cloneOfRow = (Row)tableToAddRowsTo.FirstRow.Clone(true);

// Remove all content from all Cells
foreach (Cell cell in cloneOfRow)
{
    cell.RemoveAllChildren();
    cell.EnsureMinimum();
}

// Add multiple empty rows to the end of table
for (int i = 0; i < 10; i++)
{
    Row emptyRow = (Row)cloneOfRow.Clone(true);
    tableToAddRowsTo.Rows.Add(emptyRow);
}

// Save updated document
MSWordDocument.Save(@"output.docx");

说明:此代码将合并后的文档保存为 Word(.docx)和 PDF 两种格式。

文档组装的实际应用.

tableToAddRowsTo.Rows.Add(row);

提案生成:将介绍、定价和附录等多个章节合并为单个面向客户的提案。

将介绍、定价和附录等多个章节合并为一个面向客户的完整提案。.

tableToAddRowsTo.Rows.Insert(1, row);

员工入职套件:将录用信、入职表格和政策文件合并为一个文件,以用于人力资源流程。

将录用信、入职表格和政策文件合并为一个文件,以用于人力资源流程。.

MSWordDocument.Save(@"Added Rows to Table in MS Word.docx");

报告汇编:将来自多个贡献者的报告汇集成一个连贯、格式化的文档。

将来自多个贡献者的报告汇集成一个连贯、格式化的文档。:

using Aspose.Words;
using Aspose.Words.Tables;

// Open MS Word Document
Document MSWordDocument = new Document(@"BigDocument.docx");

// Get the Table by index
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

// Create a new Row class object
Row row = new Row(MSWordDocument);

// Add three Cells to Row's cells collection
for (int i = 0; i < 3; i++)
{
    Cell cell = new Cell(MSWordDocument);
    cell.AppendChild(new Paragraph(MSWordDocument));
    cell.FirstParagraph.Runs.Add(new Run(MSWordDocument, "Text in Cell " + i));
    row.Cells.Add(cell);
}

// Insert new Row after the first Row
tableToAddRowsTo.Rows.Insert(1, row);

// Clone an existing Row from Table
Row cloneOfRow = (Row)tableToAddRowsTo.FirstRow.Clone(true);

// Remove all content from all Cells
foreach (Cell cell in cloneOfRow)
{
    cell.RemoveAllChildren();
    cell.EnsureMinimum();
}

// Add multiple empty rows to the end of table
for (int i = 0; i < 10; i++)
{
    Row emptyRow = (Row)cloneOfRow.Clone(true);
    tableToAddRowsTo.Rows.Add(emptyRow);
}

// Save updated document
MSWordDocument.Save(@"output.docx");

结论

文档汇编解决方案的部署.

 中文