翻譯|使用教程|編輯:胡濤|2022-12-26 10:15:21.560|閱讀 216 次
概述:本文主要介紹如何使用 C++ 在 Word 文檔 (DOC/DOCX) 中插入表格,歡迎查閱
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
Aspose.Words 是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式處理,并允許將各類(lèi)文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
表格有助于組織信息和圖表。我們經(jīng)常在word文檔( DOCX / DOC )中插入表格來(lái)展示信息。在文字處理應(yīng)用程序中,您可以使用 C++ 輕松創(chuàng)建表格。您可以通過(guò)以下示例來(lái)學(xué)習(xí)在 Word 文檔中使用表格:
首先,請(qǐng)注意您將使用Aspose.Words for C++ API 在 word 文檔中插入表格。您可以通過(guò)從新版本或通過(guò)NuGet庫(kù)下載它來(lái)配置 API 。正確配置后,您可以簡(jiǎn)單地利用 API 公開(kāi)的方法、屬性和類(lèi),以便可以使用一些簡(jiǎn)單的 API 調(diào)用來(lái)創(chuàng)建、編輯或操作 Microsoft Word 文檔,如 DOCX 或 DOC 文件。
您可以通過(guò)幾個(gè)簡(jiǎn)單的步驟在 Word 文檔中插入表格。不過(guò)這里需要注意的是,必須將文檔對(duì)象傳遞給每個(gè)節(jié)點(diǎn)的構(gòu)造函數(shù),這樣所有的子節(jié)點(diǎn)都屬于同一個(gè)對(duì)象。您需要按照下面列出的步驟操作:
下面的代碼片段顯示了如何使用 C++ 在 Word 文檔 (DOCX/DOC) 中插入表格:
// The path to the documents directory. System::String outputDataDir = dataDir; System::SharedPtr<Document> doc = System::MakeObject<Document>(); // We start by creating the table object. Note how we must pass the document object // To the constructor of each node. This is because every node we create must belong // To some document. System::SharedPtr<Table> table = System::MakeObject<Table>(doc); // Add the table to the document. doc->get_FirstSection()->get_Body()->AppendChild(table); // Here we could call EnsureMinimum to create the rows and cells for us. This method is used // To ensure that the specified node is valid, in this case a valid table should have at least one // Row and one cell, therefore this method creates them for us. // Instead we will handle creating the row and table ourselves. This would be the best way to do this // If we were creating a table inside an algorthim for example. System::SharedPtr<Row> row = System::MakeObject<Row>(doc); row->get_RowFormat()->set_AllowBreakAcrossPages(true); table->AppendChild(row); // We can now apply any auto fit settings. table->AutoFit(AutoFitBehavior::FixedColumnWidths); // Create a cell and add it to the row 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); // Add a paragraph to the cell as well as a new run with some text. cell->AppendChild(System::MakeObject<Paragraph>(doc)); cell->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 1 Text")); // Add the cell to the row. row->AppendChild(cell); // We would then repeat the process for the other cells and rows in the table. // We can also speed things up by cloning existing cells and rows. 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"; // Save the document to disk. doc->Save(outputPath);
HTML 文件可能包含表格,您需要將其插入到 DOCX、DOC 等 word 文檔中。或者您可能需要從網(wǎng)站復(fù)制表格。因此,無(wú)需從頭開(kāi)始創(chuàng)建和設(shè)計(jì)表格,您可以輕松地將 HTML 標(biāo)記作為表格解析到 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>
我們使內(nèi)容保持簡(jiǎn)單,以便可以通過(guò)基本但重要的用例來(lái)演示對(duì)表格標(biāo)簽的支持。此外,這里需要注意的是,AutoFit 不能應(yīng)用于從 HTML 創(chuàng)建的表格。
讓我們按照以下步驟在 Word 文檔中插入 HTML 表格:
下面的代碼遵循這些步驟,并展示了如何使用 C++ 在 HTML 的 Word 文檔中創(chuàng)建表格:
// The path to the documents directory. System::String outputDataDir = dataDir; System::SharedPtr<Document> doc = System::MakeObject<Document>(); System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); // Insert the table from HTML. Note that AutoFitSettings does not apply to tables // Inserted from 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"; // Save the document to disk. doc->Save(outputPath);
您會(huì)注意到此方法比我們上面探討的方法要簡(jiǎn)單一些。原因是,您不需要為行、列或單元格逐個(gè)添加每個(gè)節(jié)點(diǎn),因?yàn)?HTML 字符串中的 Table 標(biāo)記包含所有信息。以下是添加到 Word 文檔中的這個(gè)簡(jiǎn)單 HTML 表格的屏幕截圖:
Aspose.Words for C++ API 的最佳之處在于它提供了多種功能,這些功能成為 API 的競(jìng)爭(zhēng)優(yōu)勢(shì),并使其在其他選項(xiàng)中脫穎而出。同樣,使用文檔生成器插入表格的功能是在 word 文檔(DOC/DOCX)中添加表格的另一種方法。因此,讓我們從三個(gè)不同的角度來(lái)探討細(xì)節(jié):
1) 使用 C++ 使用文檔生成器在 DOCX 中插入簡(jiǎn)單表格
要使用文檔生成器在 word 文檔中添加一個(gè)簡(jiǎn)單的表格,您需要按照以下步驟操作:
此外,下面的代碼片段顯示了如何使用 C++ 在 DOCX 文件中插入簡(jiǎn)單表格:
System::SharedPtr<Document> doc = System::MakeObject<Document>(); System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); // We call this method to start building the table. builder->StartTable(); builder->InsertCell(); builder->Write(u"Row 1, Cell 1 Content."); // Build the second cell builder->InsertCell(); builder->Write(u"Row 1, Cell 2 Content."); // Call the following method to end the row and start a new row. builder->EndRow(); // Build the first cell of the second row. builder->InsertCell(); builder->Write(u"Row 2, Cell 1 Content"); // Build the second cell. builder->InsertCell(); builder->Write(u"Row 2, Cell 2 Content."); builder->EndRow(); // Signal that we have finished building the table. builder->EndTable(); System::String outputPath = outputDataDir + u"InsertTableUsingDocumentBuilder.SimpleTable.doc"; // Save the document to disk. doc->Save(outputPath);
2) 使用 C++ 使用文檔生成器在 DOCX 中插入格式化表格
您可以使用以下步驟將格式化表格插入到 word 文檔中:
下面的代碼片段使用 C++ 在 DOCX 文件中創(chuàng)建格式化表格:
System::SharedPtr<Document> doc = System::MakeObject<Document>(); System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); System::SharedPtr<Table> table = builder->StartTable(); // Make the header row. builder->InsertCell(); // Set the left indent for the table. Table wide formatting must be applied after // At least one row is present in the table. table->set_LeftIndent(20.0); // Set height and define the height rule for the header row. builder->get_RowFormat()->set_Height(40.0); builder->get_RowFormat()->set_HeightRule(HeightRule::AtLeast); // Some special features for the header row. 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"); // We don't need to specify the width of this cell because it's inherited from the previous cell. 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(); // Set features for the other rows and cells. 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); // Reset height and define a different height rule for table body builder->get_RowFormat()->set_Height(30.0); builder->get_RowFormat()->set_HeightRule(HeightRule::Auto); builder->InsertCell(); // Reset font formatting. builder->get_Font()->set_Size(12); builder->get_Font()->set_Bold(false); // Build the other cells. 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"; // Save the document to disk. doc->Save(outputPath);
3) 使用 C++ 使用文檔生成器在 DOCX 中插入嵌套表
有時(shí)我們需要在現(xiàn)有表中添加另一個(gè)表。例如,表的某行或某列中的單元格可以包含子類(lèi)別或某個(gè)其他字段的子表。在這種情況下,嵌套表很有用,可以按照以下步驟添加:
以下代碼片段顯示了如何使用 C++ 在 Word 文檔中插入嵌套表格:
System::SharedPtr<Document> doc = System::MakeObject<Document>(); System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); // Build the outer table. System::SharedPtr<Cell> cell = builder->InsertCell(); builder->Writeln(u"Outer Table Cell 1"); builder->InsertCell(); builder->Writeln(u"Outer Table Cell 2"); // This call is important in order to create a nested table within the first table // Without this call the cells inserted below will be appended to the outer table. builder->EndTable(); // Move to the first cell of the outer table. builder->MoveTo(cell->get_FirstParagraph()); // Build the inner table. 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"; // Save the document to disk. doc->Save(outputPath);
以上便是如何使用 C++ 在 Word 文檔 (DOC/DOCX) 中插入表格文件詳細(xì)步驟 ,要是您還有其他關(guān)于產(chǎn)品方面的問(wèn)題,歡迎咨詢我們,或者加入我們官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn