翻譯|使用教程|編輯:李顯亮|2020-09-22 10:32:24.903|閱讀 546 次
概述:我們經常在Word文檔(DOCX / DOC)中插入表格以顯示信息,在文本中我們將介紹如何使用Aspose.Words for C ++在Word文檔中插入表格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
表格有助于組織信息和數字。我們經常在Word文檔(DOCX / DOC)中插入表格以顯示信息。在文字處理應用程序中可以使用C ++輕松創建表。
Aspose.Words for C ++提供了幾乎所有基本的和高級的Word自動化功能,并且可以積極地滿足Qt應用程序中的Word處理要求。因此,讓我們看看如何集成和利用我們的C ++ Word庫在Qt應用程序中創建Word文檔。
在本文中,將介紹如何使用C ++在Word文檔中插入表格。包括以下內容:如果你還沒有用過C ++版Aspose.Words可以點擊這里下載最新版測試。
可以通過幾個簡單的步驟在Word文檔中插入表格。但是,在此處需要注意的重要一點是,必須將文檔對象傳遞給每個節點的構造函數,以便所有子節點都屬于同一對象。您需要按照以下步驟操作:
下面的代碼段顯示了如何使用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文檔中的表格。或者您可能需要從網站復制表格。因此,您可以輕松地將HTML標記作為表格解析為Word文檔,而不必從頭開始創建和設計表格。例如,讓我們使用以下HTML字符串將表添加到word文檔中:
我們將內容保持簡單,以便可以通過基本但重要的用例來演示對表標記的支持。此外,在此必須注意,不能將AutoFit應用于從HTML創建的表。讓我們按照以下步驟在Word文檔中插入HTML表:
下面的代碼遵循這些步驟,并顯示如何使用C ++在帶有HTML的Word文檔中創建表:
// 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);
此方法比我們上面探討的方法簡單一些。原因是,不需要為行,列或單元格一個接一個地添加每個節點,因為HTML字符串中的Table標記包含所有信息。以下是此簡單HTML表添加到Word文檔中的屏幕截圖:
Aspose.Words for C ++ API的最好之處在于,它提供了多種功能,這些功能已成為API的競爭優勢,并使其在其他選擇中脫穎而出。同樣,使用文檔構建器插入表格的功能是在Word文檔(DOC / DOCX)中添加表格的另一種方法。因此,讓我們從三個不同的角度探討細節:
要使用“文檔”構建器在Word文檔中添加簡單表格,需要執行以下步驟:
此外,下面的代碼段顯示了如何使用C ++在DOCX文件中插入簡單表:
System::SharedPtrdoc = System::MakeObject(); System::SharedPtrbuilder = System::MakeObject(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);
可以將格式化的表格插入Word文檔中。此外,請按照以下步驟實現您的要求:
下面的代碼片段使用C ++在DOCX文件中創建了格式表:
System::SharedPtrdoc = System::MakeObject(); System::SharedPtrbuilder = System::MakeObject(doc); System::SharedPtrtable = 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);
有時我們需要在現有表內添加另一個表。例如,表的某些行或列中的單元格可以包含子類別或某些其他字段的子表。在這種情況下,嵌套表很有用,可以按照以下步驟添加:
以下代碼段顯示了如何使用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);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn