كيفية إنشاء جدول في مستند Word باستخدام C#

يسرد هذا البرنامج التعليمي طريقتين مختلفتين لإنشاء جدول في مستند Word باستخدام C#. سنقوم أولاً بإنشاء جدول في Word باستخدام C# بمساعدة Aspose DocumentBuilder ثم إنشاء جدول Word باستخدام C# مباشرةً في نموذج كائن مستند Word (DOM).

خطوات إنشاء جدول في مستند Word باستخدام C#

  1. قم بالإشارة إلى الحزمة Aspose.Words for .NET من NuGet في الحل
  2. استيراد مساحات أسماء Aspose.Words و Aspose.Words.Tables
  3. قم بإنشاء مثيل فئة DocumentBuilder أو Table
  4. استخدم طريقة InsertCell أو Table.Rows.Cells.Add لإضافة خلايا في صف الجدول
  5. استخدم DocumentBuilder.Write أو أسلوب Cell.AppendChild لإدراج نص فقرة
  6. قم بإنشاء صفوف متعددة في الجدول وقم بإنهاء إنشاء الجدول في مستند Word
  7. حفظ كمستند 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");
}
}
}

يقدم مثال كود إنشاء جدول C# أعلاه طريقة بديلة لإضافة عنصر جدول في نموذج كائن مستند Word باستخدام فئات الجدول والصف والخلية والفقرة.

 عربي