สร้างตาราง

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

สไตล์ตารางเริ่มต้น

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

คุณสมบัติตาราง ค่าเริ่มต้นใน Aspose.Words
Border Style Single
Border Width 1/2 pt
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-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Start building the table.
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");
// Build the second cell.
builder.InsertCell();
builder.Write("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("Row 2, Cell 1 Content");
// Build the second cell.
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content.");
builder.EndRow();
// Signal that we have finished building the table.
builder.EndTable();
doc.Save(ArtifactsDir + "WorkingWithTables.CreateSimpleTable.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
builder.InsertCell();
// Table wide formatting must be applied after at least one row is present in the table.
table.LeftIndent = 20.0;
// Set height and define the height rule for the header row.
builder.RowFormat.Height = 40.0;
builder.RowFormat.HeightRule = HeightRule.AtLeast;
builder.CellFormat.Shading.BackgroundPatternColor = Color.FromArgb(198, 217, 241);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 16;
builder.Font.Name = "Arial";
builder.Font.Bold = true;
builder.CellFormat.Width = 100.0;
builder.Write("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("Header Row,\n Cell 2");
builder.InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Header Row,\n Cell 3");
builder.EndRow();
builder.CellFormat.Shading.BackgroundPatternColor = Color.White;
builder.CellFormat.Width = 100.0;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
// Reset height and define a different height rule for table body.
builder.RowFormat.Height = 30.0;
builder.RowFormat.HeightRule = HeightRule.Auto;
builder.InsertCell();
// Reset font formatting.
builder.Font.Size = 12;
builder.Font.Bold = false;
builder.Write("Row 1, Cell 1 Content");
builder.InsertCell();
builder.Write("Row 1, Cell 2 Content");
builder.InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Row 1, Cell 3 Content");
builder.EndRow();
builder.InsertCell();
builder.CellFormat.Width = 100.0;
builder.Write("Row 2, Cell 1 Content");
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content");
builder.InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Row 2, Cell 3 Content.");
builder.EndRow();
builder.EndTable();
doc.Save(ArtifactsDir + "WorkingWithTables.FormattedTable.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Cell cell = builder.InsertCell();
builder.Writeln("Outer Table Cell 1");
builder.InsertCell();
builder.Writeln("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.FirstParagraph);
// Build the inner table.
builder.InsertCell();
builder.Writeln("Inner Table Cell 1");
builder.InsertCell();
builder.Writeln("Inner Table Cell 2");
builder.EndTable();
doc.Save(ArtifactsDir + "WorkingWithTables.NestedTable.docx");
view raw nested-table.cs hosted with ❤ by GitHub

สร้างตารางผ่าน DOM (Document Object Model)

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new 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.
Table table = new Table(doc);
doc.FirstSection.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.
Row row = new Row(doc);
row.RowFormat.AllowBreakAcrossPages = true;
table.AppendChild(row);
Cell cell = new Cell(doc);
cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue;
cell.CellFormat.Width = 80;
cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, "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.LastCell.AppendChild(new Paragraph(doc));
row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 2 Text"));
// We can now apply any auto fit settings.
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
doc.Save(ArtifactsDir + "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-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Note that AutoFitSettings does not apply to tables inserted from HTML.
builder.InsertHtml("<table>" +
"<tr>" +
"<td>Row 1, Cell 1</td>" +
"<td>Row 1, Cell 2</td>" +
"</tr>" +
"<tr>" +
"<td>Row 2, Cell 2</td>" +
"<td>Row 2, Cell 2</td>" +
"</tr>" +
"</table>");
doc.Save(ArtifactsDir + "WorkingWithTables.InsertTableFromHtml.docx");

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Tables.docx");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// Clone the table and insert it into the document after the original.
Table tableClone = (Table) table.Clone(true);
table.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.ParentNode.InsertAfter(new Paragraph(doc), table);
doc.Save(ArtifactsDir + "WorkingWithTables.CloneCompleteTable.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Tables.docx");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
Row clonedRow = (Row) table.LastRow.Clone(true);
// Remove all content from the cloned row's cells. This makes the row ready for new content to be inserted into.
foreach (Cell cell in clonedRow.Cells)
cell.RemoveAllChildren();
table.AppendChild(clonedRow);
doc.Save(ArtifactsDir + "WorkingWithTables.CloneLastRow.docx");

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

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

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

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

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