สร้างตารางจาก DataTable
บ่อยครั้งที่แอปพลิเคชันของคุณจะดึงข้อมูลจากฐานข้อมูลและจัดเก็บไว้ในรูปแบบ DataTable คุณสามารถแทรกข้อมูลนี้ลงในเอกสารของคุณเป็นตารางใหม่ได้อย่างง่ายดาย และใช้การจัดรูปแบบกับทั้งตารางได้อย่างรวดเร็ว
เมื่อใช้ Aspose.Words คุณสามารถดึงข้อมูลจากฐานข้อมูลและจัดเก็บเป็นตารางได้อย่างง่ายดาย:
- สร้างวัตถุ DocumentBuilder ใหม่บน Document ของคุณ
- เริ่มตารางใหม่โดยใช้ DocumentBuilder
- หากเราต้องการแทรกชื่อของแต่ละคอลัมน์จาก DataTable ของเราเป็นแถวส่วนหัว ให้วนซ้ำแต่ละคอลัมน์ข้อมูลและเขียนชื่อคอลัมน์ลงในแถวในตาราง
- วนซ้ำแต่ละ DataRow ใน DataTable:
- วนซ้ำแต่ละออบเจ็กต์ใน DataRow
- แทรกวัตถุลงในเอกสารโดยใช้ DocumentBuilder วิธีการที่ใช้ขึ้นอยู่กับประเภทของวัตถุที่ถูกแทรก เช่น DocumentBuilder.Writeln สำหรับข้อความและ DocumentBuilder.InsertImage สำหรับอาร์เรย์ไบต์ซึ่งแสดงถึงรูปภาพ
- เมื่อสิ้นสุดการประมวลผล DataRow ให้สิ้นสุดแถวที่สร้างโดย DocumentBuilder โดยใช้ DocumentBuilder.EndRow
- เมื่อประมวลผลแถวทั้งหมดจาก DataTable แล้ว ให้จบตารางโดยเรียก DocumentBuilder.EndTable
- ในที่สุด เราก็สามารถตั้งค่าสไตล์ตารางที่ต้องการได้โดยใช้คุณสมบัติตารางที่เหมาะสม เช่น Table.StyleIdentifier เพื่อใช้การจัดรูปแบบกับทั้งตารางโดยอัตโนมัติ
วิธีการ ImportTableFromDataTable ยอมรับออบเจ็กต์ DocumentBuilder ซึ่งเป็น DataTable ที่มีข้อมูลและแฟล็กที่ระบุว่าส่วนหัวของคอลัมน์จาก DataTable รวมอยู่ที่ด้านบนของตารางหรือไม่ เมธอดนี้จะสร้างตารางจากพารามิเตอร์เหล่านี้โดยใช้ตำแหน่งและการจัดรูปแบบปัจจุบันของตัวสร้าง จัดเตรียมวิธีการนำเข้าข้อมูลจาก DataTable และแทรกลงในตารางใหม่โดยใช้ DocumentBuilder
ข้อมูลต่อไปนี้ใน DataTable ของเราถูกใช้ในตัวอย่างนี้:

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีดำเนินการอัลกอริทึมด้านบนใน Aspose.Words:
| // For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
| /// <summary> | |
| /// Imports the content from the specified DataTable into a new Aspose.Words Table object. | |
| /// The table is inserted at the document builder's current position and using the current builder's formatting if any is defined. | |
| /// </summary> | |
| public Table ImportTableFromDataTable(DocumentBuilder builder, DataTable dataTable, | |
| bool importColumnHeadings) | |
| { | |
| Table table = builder.StartTable(); | |
| // Check if the columns' names from the data source are to be included in a header row. | |
| if (importColumnHeadings) | |
| { | |
| // Store the original values of these properties before changing them. | |
| bool boldValue = builder.Font.Bold; | |
| ParagraphAlignment paragraphAlignmentValue = builder.ParagraphFormat.Alignment; | |
| // Format the heading row with the appropriate properties. | |
| builder.Font.Bold = true; | |
| builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; | |
| // Create a new row and insert the name of each column into the first row of the table. | |
| foreach (DataColumn column in dataTable.Columns) | |
| { | |
| builder.InsertCell(); | |
| builder.Writeln(column.ColumnName); | |
| } | |
| builder.EndRow(); | |
| // Restore the original formatting. | |
| builder.Font.Bold = boldValue; | |
| builder.ParagraphFormat.Alignment = paragraphAlignmentValue; | |
| } | |
| foreach (DataRow dataRow in dataTable.Rows) | |
| { | |
| foreach (object item in dataRow.ItemArray) | |
| { | |
| // Insert a new cell for each object. | |
| builder.InsertCell(); | |
| switch (item.GetType().Name) | |
| { | |
| case "DateTime": | |
| // Define a custom format for dates and times. | |
| DateTime dateTime = (DateTime) item; | |
| builder.Write(dateTime.ToString("MMMM d, yyyy")); | |
| break; | |
| default: | |
| // By default any other item will be inserted as text. | |
| builder.Write(item.ToString()); | |
| break; | |
| } | |
| } | |
| // After we insert all the data from the current record, we can end the table row. | |
| builder.EndRow(); | |
| } | |
| // We have finished inserting all the data from the DataTable, we can end the table. | |
| builder.EndTable(); | |
| return table; | |
| } |
จากนั้นสามารถเรียกวิธีการนี้ได้อย่างง่ายดายโดยใช้ DocumentBuilder และข้อมูลของคุณ
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการนำเข้าข้อมูลจาก DataTable และแทรกลงในตารางใหม่ในเอกสาร:
| // For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
| Document doc = new Document(); | |
| // We can position where we want the table to be inserted and specify any extra formatting to the table. | |
| DocumentBuilder builder = new DocumentBuilder(doc); | |
| // We want to rotate the page landscape as we expect a wide table. | |
| doc.FirstSection.PageSetup.Orientation = Orientation.Landscape; | |
| DataSet ds = new DataSet(); | |
| ds.ReadXml(MyDir + "List of people.xml"); | |
| // Retrieve the data from our data source, which is stored as a DataTable. | |
| DataTable dataTable = ds.Tables[0]; | |
| // Build a table in the document from the data contained in the DataTable. | |
| Table table = ImportTableFromDataTable(builder, dataTable, true); | |
| // We can apply a table style as a very quick way to apply formatting to the entire table. | |
| table.StyleIdentifier = StyleIdentifier.MediumList2Accent1; | |
| table.StyleOptions = TableStyleOptions.FirstRow | TableStyleOptions.RowBands | TableStyleOptions.LastColumn; | |
| // For our table, we want to remove the heading for the image column. | |
| table.FirstRow.LastCell.RemoveAllChildren(); | |
| doc.Save(ArtifactsDir + "WorkingWithTables.BuildTableFromDataTable.docx"); |