บทช่วยสอนนี้แสดงสองวิธีในการสร้างตารางในเอกสาร Word โดยใช้ C# ก่อนอื่นเราจะสร้างตารางใน Word โดยใช้ C# ด้วยความช่วยเหลือของ Aspose DocumentBuilder และต่อมาสร้างตาราง Word โดยใช้ C# โดยตรงใน Word document object model (DOM)
ขั้นตอนในการสร้างตารางในเอกสาร Word โดยใช้ C#
- อ้างอิงแพ็คเกจ Aspose.Words for .NET จาก NuGet ในโซลูชัน
- นำเข้า Aspose.Words และ Aspose.Words.Tables เนมสเปซ
- สร้างอินสแตนซ์คลาส DocumentBuilder หรือ Table
- ใช้เมธอด InsertCell หรือ Table.Rows.Cells.Add เพื่อเพิ่มเซลล์ในแถวตาราง
- ใช้เมธอด DocumentBuilder.Write หรือ Cell.AppendChild เพื่อแทรกข้อความย่อหน้า
- สร้างหลายแถวในตารางและสร้างตารางให้เสร็จในเอกสาร Word
- บันทึกเป็นเอกสาร Word พร้อมตารางในรูปแบบ DOCX บนดิสก์
ตัวอย่างโค้ด Aspose DocumentBuilder ต่อไปนี้สามารถใช้ได้ในแอปพลิเคชัน .NET สำหรับการสร้างตาราง C# Word
รหัสเพื่อสร้างตารางในเอกสาร Word โดยใช้ C
using Aspose.Words; | |
using Aspose.Words.Tables; | |
namespace CreateTableInWordDocumentUsingCsharp | |
{ | |
class CreateTableWord | |
{ | |
static void Main(string[] args) | |
{ | |
// Set license before C# Word table creation | |
License setupToCreateTable = new License(); | |
setupToCreateTable.SetLicense(@"license file path.lic"); | |
// Document Builder object to create Table in Word document | |
DocumentBuilder toCreateTableInWord = new DocumentBuilder(); | |
// Mark the start of Word Table | |
Table tableInWord = toCreateTableInWord.StartTable(); | |
// Insert a new Row & then first Cell in Word Table | |
toCreateTableInWord.InsertCell(); | |
// Write text in Table Cell | |
toCreateTableInWord.Write("Table Row 1 and Cell 1"); | |
// Insert a new Cell in same Word Table Row | |
toCreateTableInWord.InsertCell(); | |
// Insert an Image in Word Table Cell | |
toCreateTableInWord.InsertImage("insert image in word table.jpg"); | |
// Mark the end of Table Row | |
toCreateTableInWord.EndRow(); | |
// Mark end of Word Table creation | |
toCreateTableInWord.EndTable(); | |
// Save Word DOCX document with Table on disk | |
toCreateTableInWord.Document.Save("c# word table created in.docx"); | |
} | |
} | |
} |
โดยพื้นฐานแล้ว แอปสร้างตาราง Word C# นี้จะช่วยให้คุณสามารถแทรกตารางในเอกสาร Word ได้ อันดับแรกจะเพิ่มสองเซลล์ในแถวแรกของตาราง แล้วจึงเขียนเนื้อหาข้อความในเซลล์แรก จากนั้นตามด้วย เพิ่มรูปภาพใน Word C# เซลล์สุดท้าย
รหัสเพื่อสร้างตาราง Word โดยใช้ C # (คลาส DOM)
using Aspose.Words; | |
using Aspose.Words.Tables; | |
namespace CreateWordTable | |
{ | |
class CreateTableInWord | |
{ | |
static void Main(string[] args) | |
{ | |
// Set license before creating Word table | |
License license = new License(); | |
license.SetLicense(@"license file path.lic"); | |
Document wordDocument = new Document(); // Create empty Word document | |
Table wordTable = new Table(wordDocument); // Create Table object | |
wordTable.Rows.Add(new Row(wordDocument)); // Create a Row inside Table | |
wordTable.FirstRow.Cells.Add(new Cell(wordDocument)); // Create a single Cell in Table Row | |
wordTable.FirstRow.FirstCell.AppendChild(new Paragraph(wordDocument)); // Add a Paragraph inside Cell | |
// Write text content inside Word Table Cell | |
wordTable.FirstRow.FirstCell.FirstParagraph.Runs.Add(new Run(wordDocument, "Text in Table Row 1 and Cell 1")); | |
// Insert Table at the end of Word Document | |
wordDocument.FirstSection.Body.InsertBefore(wordTable, wordDocument.FirstSection.Body.LastParagraph); | |
// Save Word document to DOCX format | |
wordDocument.Save("c# word table created in.docx"); | |
} | |
} | |
} |
ตัวอย่าง Aspose Word สร้างตารางโค้ด C# เป็นเพียงการนำเสนอวิธีอื่นสำหรับการเพิ่มองค์ประกอบตารางในรูปแบบวัตถุเอกสาร Word โดยใช้คลาสตาราง แถว เซลล์ และย่อหน้า