2

使用Novacode.DocX.

// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add a Table to this document.
Table t = document.AddTable(2, 3);
// Specify some properties for this Table.
t.Alignment = Alignment.center;
t.Design = TableDesign.MediumGrid1Accent2;
// Add content to this Table.
t.Rows[0].Cells[0].Paragraphs.First().Append("A");
t.Rows[0].Cells[1].Paragraphs.First().Append("B");
t.Rows[0].Cells[2].Paragraphs.First().Append("C");
t.Rows[1].Cells[0].Paragraphs.First().Append("D");
t.Rows[1].Cells[1].Paragraphs.First().Append("E");
t.Rows[1].Cells[2].Paragraphs.First().Append("F");
// Insert the Table into the document.
document.InsertTable(t);
document.Save();
}// Release this document from memory.

上面的代码将创建一个如下图所示的文档

还有,如何使用 DocX 为表格中的文本设置垂直方向?文本方向仅从右到左进行,反之亦然。

tablePlan.Rows[0].Cells[1].Paragraphs.First().Direction = Direction.LeftToRight;

以及如何从下往上放?

在此处输入图像描述

4

2 回答 2

1

您可以通过添加:

t.Rows[0].Cells[0].TextDirection = TextDirection.btLr;
t.Rows[0].Cells[1].TextDirection = TextDirection.btLr;
t.Rows[0].Cells[2].TextDirection = TextDirection.btLr;
于 2019-03-26T11:07:54.003 回答
0

此解决方案是一种变通方法,只有在您已经知道 Table 将要拥有的列数之前才有效。您首先需要创建一个包含表格的文档,其中包含您正在寻找的属性,文本方向。然后,您可以Tables根据需要从其他模板文档中获取。

// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
    Table t;
    using (DocX document2 = DocX.Load(@"Test2.docx"))
    {
        t = document2.Tables[0];
    }        
    // Specify some properties for this Table.
    t.Alignment = Alignment.center;
    t.Design = TableDesign.MediumGrid1Accent2;
    // Add content to this Table.
    t.Rows[0].Cells[0].Paragraphs.First().Append("A");
    t.Rows[0].Cells[1].Paragraphs.First().Append("B");
    t.Rows[0].Cells[2].Paragraphs.First().Append("C");
    t.Rows[1].Cells[0].Paragraphs.First().Append("D");
    t.Rows[1].Cells[1].Paragraphs.First().Append("E");
    t.Rows[1].Cells[2].Paragraphs.First().Append("F");
    // Insert the Table into the document.
    document.InsertTable(t);
    document.Save();
}// Release this document from memory.

document2包含具有您要查找的属性的表。

于 2015-08-21T18:59:33.517 回答