翻译|使用教程|编辑:李显亮|2020-09-22 10:32:24.903|阅读 463 次
概述:我们经常在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@cahobeh.cn