表格有助於組織信息和圖表。我們經常在word文檔(DOCX/DOC)中插入表格來展示信息。在文字處理應用程序中,您可以使用 C++ 輕鬆創建表格。您可以通過以下示例來學習在 Word 文檔中使用表格:

讓我們繼續詳細探討所有這些主題:

在 Word 文檔 API 中插入表格

首先,請注意您將使用 Aspose.Words for C++ API 在 word 文檔中插入表格。您可以通過從 New ReleasesNuGet 庫下載它來配置 API。正確配置後,您可以簡單地利用 API 公開的方法、屬性和類,以便可以使用一些簡單的 API 調用來創建、編輯或操作 Microsoft Word 文檔,如 DOCX 或 DOC 文件。

使用 C++ 在 Word 文檔中插入表格

您可以通過幾個簡單的步驟在 Word 文檔中插入表格。不過這裡需要注意的是,必須將文檔對像傳遞給每個節點的構造函數,這樣所有的子節點都屬於同一個對象。您需要按照下面列出的步驟操作:

  1. 初始化 Document 類的對象
  2. 創建 Table 對象
  3. 將表添加到文檔
  4. 創建行和列
  5. 在表格單元格上應用 AutoFit
  6. 保存輸出 Word 文檔

下面的代碼片段顯示瞭如何使用 C++ 在 Word 文檔 (DOCX/DOC) 中插入表格:

// 文檔目錄的路徑。
System::String outputDataDir = dataDir;
System::SharedPtr<Document> doc = System::MakeObject<Document>();

// 我們從創建表對像開始。注意我們必須如何傳遞文檔對象
// 到每個節點的構造函數。這是因為我們創建的每個節點都必須屬於
// 到一些文件。
System::SharedPtr<Table> table = System::MakeObject<Table>(doc);

// 將表格添加到文檔中。
doc->get_FirstSection()->get_Body()->AppendChild(table);

// 在這裡我們可以調用 EnsureMinimum 來為我們創建行和單元格。這個方法是用
// 為確保指定的節點有效,在這種情況下,一個有效的表至少應該有一個
// 行和一個單元格,因此此方法為我們創建了它們。
// 相反,我們將自己處理創建行和表。這將是最好的方法
// 例如,如果我們在算法中創建一個表。
System::SharedPtr<Row> row = System::MakeObject<Row>(doc);
row->get_RowFormat()->set_AllowBreakAcrossPages(true);
table->AppendChild(row);

// 我們現在可以應用任何自動調整設置。
table->AutoFit(AutoFitBehavior::FixedColumnWidths);

// 創建一個單元格並將其添加到行中
System::SharedPtr<Cell> cell = System::MakeObject<Cell>(doc);
cell->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightBlue());
cell->get_CellFormat()->set_Width(80);

// 將一個段落添加到單元格以及一個帶有一些文本的新運行。
cell->AppendChild(System::MakeObject<Paragraph>(doc));
cell->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 1 Text"));

// 將單元格添加到行中。
row->AppendChild(cell);

// 然後我們將對錶格中的其他單元格和行重複該過程。
// 我們還可以通過克隆現有的單元格和行來加快速度。
row->AppendChild((System::StaticCast<Node>(cell))->Clone(false));
row->get_LastCell()->AppendChild(System::MakeObject<Paragraph>(doc));
row->get_LastCell()->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 2 Text"));
System::String outputPath = outputDataDir + u"InsertTableDirectly.doc";

// 將文檔保存到磁盤。
doc->Save(outputPath);

使用 C++ 在 Word 文檔中從 HTML 插入表格

HTML 文件可能包含表格,您需要將其插入到 DOCX、DOC 等 word 文檔中。或者您可能需要從網站複製表格。因此,無需從頭開始創建和設計表格,您可以輕鬆地將 HTML 標記作為表格解析到 word 文檔中。例如,您可以使用以下 HTML 字符串將表格添加到 word 文檔中:

<table><tr><td>Row 1, Cell 1</td><td>Row 1, Cell 2</td></tr><tr><td>Row 2, Cell 1</td><td>Row 2, Cell 2</td></tr></table>

我們使內容保持簡單,以便可以通過基本但重要的用例來演示對錶格標籤的支持。此外,這裡需要注意的是,AutoFit 不能應用於從 HTML 創建的表格。

讓我們按照以下步驟在 Word 文檔中插入 HTML 表格:

  1. 初始化 Document 類的一個實例
  2. 使用 InsertHtml 方法傳遞 HTML 標記
  3. 保存輸出DOCX word文件

下面的代碼遵循這些步驟,並展示瞭如何使用 C++ 在 HTML 的 Word 文檔中創建表格:

// 文檔目錄的路徑。
System::String outputDataDir = dataDir;
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);

// 從 HTML 插入表格。請注意,AutoFitSettings 不適用於表格
// 從 HTML 插入。
builder->InsertHtml(u"<table><tr><td>Row 1, Cell 1</td><td>Row 1, Cell 2</td></tr><tr><td>Row 2, Cell 1</td><td>Row 2, Cell 2</td></tr></table>");

System::String outputPath = outputDataDir + u"InsertTableFromHtml.doc";
// 將文檔保存到磁盤。
doc->Save(outputPath);

您會注意到此方法比我們上面探討的方法要簡單一些。原因是,您不需要為行、列或單元格逐一添加每個節點,因為 HTML 字符串中的 Table 標記包含所有信息。以下是添加到 Word 文檔中的這個簡單 HTML 表格的屏幕截圖:

在 Word 中插入表格

在 C++ 中使用文檔生成器插入表

Aspose.Words for C++ API 的最佳之處在於它提供了多種功能,這些功能成為 API 的競爭優勢,並使其在其他選項中脫穎而出。同樣,使用文檔生成器插入表格的功能是在 word 文檔(DOC/DOCX)中添加表格的另一種方法。因此,讓我們從三個不同的角度來探討細節:

1) 使用 C++ 使用文檔生成器在 DOCX 中插入簡單表格

要使用文檔生成器在 word 文檔中添加一個簡單的表格,您需要按照以下步驟操作:

  1. 創建 文檔 對象
  2. 調用 StartTable() 方法並插入單元格
  3. 添加行和單元格
  4. 保存輸出 DOCX 文件

此外,下面的代碼片段顯示瞭如何使用 C++ 在 DOCX 文件中插入簡單表格:

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);

// 我們調用這個方法開始建表。
builder->StartTable();
builder->InsertCell();
builder->Write(u"Row 1, Cell 1 Content.");

// 構建第二個單元格
builder->InsertCell();
builder->Write(u"Row 1, Cell 2 Content.");

// 調用以下方法結束行並開始新行。
builder->EndRow();

// 構建第二行的第一個單元格。
builder->InsertCell();
builder->Write(u"Row 2, Cell 1 Content");

// 構建第二個單元格.
builder->InsertCell();
builder->Write(u"Row 2, Cell 2 Content.");
builder->EndRow();

// 表示我們已經完成構建表。
builder->EndTable();
System::String outputPath = outputDataDir + u"InsertTableUsingDocumentBuilder.SimpleTable.doc";

// 將文檔保存到磁盤。
doc->Save(outputPath);

2) 使用 C++ 使用文檔生成器在 DOCX 中插入格式化表格

您可以使用以下步驟將格式化表格插入到 word 文檔中:

  1. 初始化 Document 類的一個實例
  2. 製作標題行
  3. 為格式設置縮進和功能
  4. 重置字體格式
  5. 保存輸出 Word DOCX 文件

下面的代碼片段使用 C++ 在 DOCX 文件中創建格式化表格:

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Table> table = builder->StartTable();

// 製作標題行。
builder->InsertCell();

// 設置表格的左縮進。必須在之後應用表範圍格式
// 表中至少有一行。
table->set_LeftIndent(20.0);

// 設置高度並定義標題行的高度規則。
builder->get_RowFormat()->set_Height(40.0);
builder->get_RowFormat()->set_HeightRule(HeightRule::AtLeast);

// 標題行的一些特殊功能。
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::FromArgb(198, 217, 241));
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
builder->get_Font()->set_Size(16);
builder->get_Font()->set_Name(u"Arial");
builder->get_Font()->set_Bold(true);
builder->get_CellFormat()->set_Width(100.0);
builder->Write(u"Header Row,\n Cell 1");

// 我們不需要指定此單元格的寬度,因為它是從前一個單元格繼承而來的。
builder->InsertCell();
builder->Write(u"Header Row,\n Cell 2");
builder->InsertCell();
builder->get_CellFormat()->set_Width(200.0);
builder->Write(u"Header Row,\n Cell 3");
builder->EndRow();

// 為其他行和單元格設置特徵。
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_White());
builder->get_CellFormat()->set_Width(100.0);
builder->get_CellFormat()->set_VerticalAlignment(CellVerticalAlignment::Center);

// 重置高度並為表體定義不同的高度規則
builder->get_RowFormat()->set_Height(30.0);
builder->get_RowFormat()->set_HeightRule(HeightRule::Auto);
builder->InsertCell();

// 重置字體格式。
builder->get_Font()->set_Size(12);
builder->get_Font()->set_Bold(false);

// 構建其他單元格。
builder->Write(u"Row 1, Cell 1 Content");
builder->InsertCell();
builder->Write(u"Row 1, Cell 2 Content");
builder->InsertCell();
builder->get_CellFormat()->set_Width(200.0);
builder->Write(u"Row 1, Cell 3 Content");
builder->EndRow();
builder->InsertCell();
builder->get_CellFormat()->set_Width(100.0);
builder->Write(u"Row 2, Cell 1 Content");
builder->InsertCell();
builder->Write(u"Row 2, Cell 2 Content");
builder->InsertCell();
builder->get_CellFormat()->set_Width(200.0);
builder->Write(u"Row 2, Cell 3 Content.");
builder->EndRow();
builder->EndTable();
System::String outputPath = outputDataDir + u"InsertTableUsingDocumentBuilder.FormattedTable.doc";

// 將文檔保存到磁盤。
doc->Save(outputPath);

3) 使用 C++ 使用文檔生成器在 DOCX 中插入嵌套表

有時我們需要在現有表中添加另一個表。例如,表的某行或某列中的單元格可以包含子類別或某個其他字段的子表。在這種情況下,嵌套表很有用,可以按照以下步驟添加:

  1. 構建外部 Table 然後調用 EndTable 方法
  2. 在外表的單元格內構建內表
  3. 保存輸出word文檔

以下代碼片段顯示瞭如何使用 C++ 在 Word 文檔中插入嵌套表格:

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);

// 構建外表。
System::SharedPtr<Cell> cell = builder->InsertCell();
builder->Writeln(u"Outer Table Cell 1");
builder->InsertCell();
builder->Writeln(u"Outer Table Cell 2");

// 此調用對於在第一個表中創建嵌套表很重要
// 如果沒有此調用,下面插入的單元格將附加到外表。
builder->EndTable();

// 移動到外表的第一個單元格。
builder->MoveTo(cell->get_FirstParagraph());

// 構建內表。
builder->InsertCell();
builder->Writeln(u"Inner Table Cell 1");
builder->InsertCell();
builder->Writeln(u"Inner Table Cell 2");
builder->EndTable();
System::String outputPath = outputDataDir + u"InsertTableUsingDocumentBuilder.NestedTable.doc";

// 將文檔保存到磁盤。
doc->Save(outputPath);

結論

綜上所述,我們已經學會瞭如何使用不同的方法在Word文檔中插入不同的表格。您可以將簡單的表格或帶有 HTML 字符串的表格以及格式化和嵌套的表格插入到 word 文檔 (DOC/DOCX) 中。但是,如果有任何疑問或疑問,您可以通過 免費支持論壇 與我們聯繫。此外,您也可以參考API ReferencesProduct Documentation作為參考。

也可以看看