สร้างตาราง

Aspose.Wordsอนุญาตให้ผู้ใช้สร้างตารางในเอกสารจากรอยขีดข่วนและมีวิธีการที่แตกต่างกันหลายอย่า บทความนี้แสดงรายละเอียดเกี่ยวกับวิธีการเพิ่มตารางที่จัดรูปแบบเอกสารของคุณโดยใช้.

รูปแบบตารางเริ่มต้น

ตารางที่สร้างขึ้นใหม่จะได้รับค่าดีฟอลต์คล้ายกับที่ใช้ในMicrosoft Word:

คุณสมบัติตาราง ค่าปริยายในAspose.Words
Border Style Single
Border Width 1/2 จุด
Border Color Black
Left and Right Padding 5.4 pts
AutoFit Mode AutoFit to Window
Allow AutoFit True

สร้างตารางด้วยDocumentBuilder

ในAspose.Wordsผู้ใช้สามารถสร้างตารางในเอกสารโดยใช้DocumentBuilder ขั้นตอนวิธีการพื้นฐานสำหรับการสร้างตารางมีดังนี้:

  1. เริ่มต้นตารางด้วยStartTable
  2. เพิ่มเซลล์ลงในตารางโดยใช้InsertCellซึ่งจะเริ่มแถวใหม่โดยอัตโนมัติ
  3. คุณสามารถเลือกใช้คุณสมบัติCellFormatเพื่อระบุการจัดรูปแบบเซลล์
  4. แทรกเนื้อหาของเซลล์โดยใช้วิธีการที่เหมาะสมDocumentBuilderเช่นWriteln,InsertImageและอื่นๆ
  5. ทำซ้ำขั้นตอน2-4 จนกว่าแถวจะเสร็จสมบูรณ์
  6. โทรEndRowเพื่อสิ้นสุดแถวปัจจุบัน
  7. คุณสามารถเลือกใช้คุณสมบัติRowFormatเพื่อระบุการจัดรูปแบบแถว
  8. ทำซ้ำขั้นตอน2-7 จนกว่าตารางจะเสร็จสมบูรณ์
  9. โทรEndTableเพื่อเสร็จสิ้นการสร้างตาราง

กระบวนการของการสร้างตารางสามารถมองเห็นได้อย่างชัดเจนในภาพต่อไปนี้:

creating-table-process

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างตารางอย่างง่ายโดยใช้DocumentBuilderด้วยการจัดรูปแบบเริ่มต้น:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// 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();
doc->Save(ArtifactsDir + u"WorkingWithTables.CreateSimpleTable.docx");

ตัวอย่างรหัสต่อไปนี้แสดงวิธีสร้างตารางที่จัดรูปแบบโดยใช้DocumentBuilder:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
// 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);
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 this cell's width 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();
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);
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();
doc->Save(ArtifactsDir + u"WorkingWithTables.FormattedTable.docx");

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกตารางที่ซ้อนกันโดยใช้DocumentBuilder:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
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 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();
doc->Save(ArtifactsDir + u"WorkingWithTables.NestedTable.docx");
view raw nested-table.h hosted with ❤ by GitHub

สร้างตารางผ่านDOM(รูปแบบวัตถุเอกสาร)

คุณสามารถแทรกตารางลงในDOMโดยตรงโดยการเพิ่มโหนดใหม่Tableในตำแหน่งที่เฉพาะเจาะจง.

โปรดทราบว่าทันทีหลังจากการสร้างโหนดตารางตารางตัวเองจะว่างเปล่าอย่างสมบูรณ์ เมื่อต้องการแทรกแถวและเซลล์ลงในตารางให้เพิ่มโหนดลูกที่เหมาะสมRowและCellลงในDOM.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างตารางใหม่จากรอยขีดข่วนโดยการเพิ่มโหนดลูกที่เ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
// We start by creating the table object. Note that we must pass the document object
// to the constructor of each node. This is because every node we create must belong
// to some document.
auto table = MakeObject<Table>(doc);
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.
// 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 algorithm.
auto row = MakeObject<Row>(doc);
row->get_RowFormat()->set_AllowBreakAcrossPages(true);
table->AppendChild(row);
// We can now apply any auto fit settings.
table->AutoFit(AutoFitBehavior::FixedColumnWidths);
auto cell = MakeObject<Cell>(doc);
cell->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightBlue());
cell->get_CellFormat()->set_Width(80);
cell->AppendChild(MakeObject<Paragraph>(doc));
cell->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, u"Row 1, Cell 1 Text"));
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(cell->Clone(false));
row->get_LastCell()->AppendChild(MakeObject<Paragraph>(doc));
row->get_LastCell()->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, u"Row 1, Cell 2 Text"));
doc->Save(ArtifactsDir + u"WorkingWithTables.InsertTableDirectly.docx");

สร้างตารางจากHTML

Aspose.Wordsรองรับการแทรกเนื้อหาลงในเอกสารจากแหล่งที่มาHTMLโดยใช้วิธีการInsertHtml การป้อนข้อมูลอาจเป็นหน้าHTMLที่สมบูรณ์หรือเพียงแค่ตัวอย่างบางส่วน.

โดยใช้วิธีการInsertHtmlผู้ใช้สามารถแทรกตารางลงในเอกสารผ่านแท็กตารางเช่น<table>, <tr>, <td>.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกตารางลงในเอกสารจากสตริงที่มีแท็กHTML:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Note that AutoFitSettings does not apply to tables inserted from HTML.
builder->InsertHtml(String(u"<table>") + u"<tr>" + u"<td>Row 1, Cell 1</td>" + u"<td>Row 1, Cell 2</td>" + u"</tr>" + u"<tr>" +
u"<td>Row 2, Cell 2</td>" + u"<td>Row 2, Cell 2</td>" + u"</tr>" + u"</table>");
doc->Save(ArtifactsDir + u"WorkingWithTables.InsertTableFromHtml.docx");

แทรกสำเนาของตารางที่มีอยู่

บ่อยครั้งที่คุณจำเป็นต้องสร้างตารางตามตารางที่มีอยู่แล้วในเอกสาร วิธีที่ง่ายที่สุดในการทำซ้ำตารางขณะที่ยังคงการจัดรูปแบบทั้งหมดคือการโคลนโหนดตารางโดยใช้วิธีการClone.

เทคนิคเดียวกันสามารถใช้ในการเพิ่มสำเนาของแถวที่มีอยู่หรือเซลล์ในตาราง.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำซ้ำตารางโดยใช้ตัวสร้างโหนด:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// Clone the table and insert it into the document after the original.
auto tableClone = System::ExplicitCast<Table>(table->Clone(true));
table->get_ParentNode()->InsertAfter(tableClone, table);
// Insert an empty paragraph between the two tables,
// or else they will be combined into one upon saving this has to do with document validation.
table->get_ParentNode()->InsertAfter(MakeObject<Paragraph>(doc), table);
doc->Save(ArtifactsDir + u"WorkingWithTables.CloneCompleteTable.docx");

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการโคลนแถวสุดท้ายของตารางและผนวกเข้ากับตาราง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
auto clonedRow = System::ExplicitCast<Row>(table->get_LastRow()->Clone(true));
// Remove all content from the cloned row's cells. This makes the row ready for new content to be inserted into.
for (const auto& cell : System::IterateOver<Cell>(clonedRow->get_Cells()))
{
cell->RemoveAllChildren();
}
table->AppendChild(clonedRow);
doc->Save(ArtifactsDir + u"WorkingWithTables.CloneLastRow.docx");

หากคุณกำลังมองที่การสร้างตารางในเอกสารที่เติบโตแบบไดนามิกกับแต่ละระเบียนจาก แต่ผลลัพธ์ที่ต้องการทำได้ง่ายขึ้นโดยใช้Mail mergeกับภูมิภาค คุณสามารถเรียนรู้เพิ่มเติมเกี่ยวกับเทคนิคนี้ใน Mail Mergeกับภูมิภาค มาตรา.

เปรียบเทียบวิธีการสร้างตาราง

Aspose.Wordsมีหลายวิธีในการสร้างตารางใหม่ในเอกสาร แต่ละวิธีมีข้อดีของตัวเองและข้อเสียดังนั้นทางเลือกของการที่จะใช้มักจะขึ้นอยู่กับสถานกา.

ลองมาดูที่วิธีการเหล่านี้ในการสร้างตารางและเปรียบเทียบข้อดีและข้อเสียของพวกเขา:

วิธีการ ข้อดี ข้อเสีย
DocumentBuilder วิธีการมาตรฐานสำหรับการแทรกตารางและเนื้อหาเอกสารอื่นๆ บางครั้งยากที่จะสร้างหลายพันธุ์ของตารางในเวลาเดียวกันกับอินสแตนซ์สร้างเดียวกัน
ผ่านDOM เหมาะกับโค้ดโดยรอบที่สร้างและแทรกโหนดลงในDOMโดยไม่ต้องใช้DocumentBuilder ตารางถูกสร้างขึ้น"ว่างเปล่า":ก่อนดำเนินการส่วนใหญ่คุณต้องโทรEnsureMinimumเพื่อสร้างโหนดเด็กที่ขาด
จากHTML สามารถสร้างตารางใหม่จากHTMLแหล่งที่มาโดยใช้แท็กเช่น<table>, <tr>, <td> ไม่สามารถใช้รูปแบบตารางMicrosoft Wordได้กับHTML
การโคลนตารางที่มีอยู่ คุณสามารถสร้างสำเนาของตารางที่มีอยู่ขณะที่ยังคงจัดรูปแบบแถวและเซลล์ทั้งหมด โหนดลูกที่เหมาะสมต้องถูกลบออกก่อนที่ตารางจะพร้อมสำหรับการใช้งาน