این آموزش دو روش مختلف برای ایجاد جدول در سند Word با استفاده از سی شارپ را فهرست می کند. ابتدا جدولی را در ورد با استفاده از سی شارپ با کمک Aspose DocumentBuilder ایجاد می کنیم و بعداً جدول ورد را با استفاده از سی شارپ مستقیماً در مدل شی سند Word (DOM) ایجاد می کنیم.
مراحل ایجاد جدول در سند ورد با استفاده از سی شارپ
- به بسته 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 زیر را می توان در برنامه دات نت برای ایجاد جدول C# Word استفاده کرد.
کد برای ایجاد جدول در سند Word با استفاده از سی شارپ
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# جدول ایجاد جدول Aspose Word بالا فقط یک رویکرد جایگزین برای افزودن یک عنصر جدول در مدل شی سند Word با استفاده از کلاسهای Table، Row، Cell و Paragraph ارائه میکند.