یک جدول ایجاد کنید
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 یک جدول در یک سند ایجاد کنند. الگوریتم اصلی برای ایجاد جدول به شرح زیر است:
- جدول را با StartTable شروع کنید
- با استفاده از InsertCell یک سلول به جدول اضافه کنید - این به طور خودکار یک ردیف جدید را شروع می کند
- در صورت تمایل، از ویژگی CellFormat برای تعیین قالب بندی سلول استفاده کنید
- محتوای سلول را با استفاده از روش های DocumentBuilder مناسب مانند Writeln، InsertImage و غیره وارد کنید
- مراحل 2-4 را تکرار کنید تا ردیف کامل شود
- برای پایان دادن به ردیف فعلی با EndRow تماس بگیرید
- در صورت تمایل، از ویژگی RowFormat برای تعیین قالب بندی ردیف استفاده کنید
- مراحل 2-7 را تکرار کنید تا جدول کامل شود
- برای تکمیل ساخت جدول با EndTable تماس بگیرید
جزئیات مهم:
- StartTable را می توان در داخل یک سلول نیز فراخوانی کرد، در این صورت شروع به ایجاد یک جدول تودرتو در داخل سلول می کند.
- پس از فراخوانی InsertCell، یک سلول جدید ایجاد می شود و هر محتوایی که با استفاده از روش های دیگر کلاس DocumentBuilder اضافه کنید، به سلول فعلی اضافه می شود. برای ایجاد یک سلول جدید در همان ردیف، دوباره InsertCell را فراخوانی کنید.
- اگر InsertCell بلافاصله بعد از EndRow و انتهای یک سطر فراخوانی شود، جدول در یک ردیف جدید ادامه خواهد یافت.
- روش EndTable برای پایان دادن به جدول فقط باید یک بار پس از فراخوانی EndRow فراخوانی شود. فراخوانی EndTable مکان نما را از سلول فعلی به موقعیت بلافاصله بعد از جدول منتقل می کند.
روند ایجاد جدول به وضوح در تصویر زیر قابل مشاهده است:
مثال کد زیر نحوه ایجاد یک جدول ساده با استفاده از 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"); |
ایجاد جدول از طریق DOM (Document Object Model)
می توانید با افزودن یک گره Table جدید در یک موقعیت خاص، جداول را مستقیماً در DOM وارد کنید.
لطفاً توجه داشته باشید که بلافاصله پس از ایجاد گره جدول، خود جدول کاملاً خالی خواهد بود، یعنی هنوز دارای ردیف و سلول نیست. برای درج ردیف ها و سلول ها در جدول، گره های فرزند مناسب 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 | با کدهای اطراف که بدون استفاده از DocumentBuilder گرهها را مستقیماً در DOM ایجاد و وارد میکنند، بهتر سازگار است | جدول “خالی” ایجاد می شود: قبل از انجام بیشتر عملیات، باید با EnsureMinimum تماس بگیرید تا گره های فرزند گم شده ایجاد شوند |
از HTML | می تواند یک جدول جدید از منبع HTML با استفاده از برچسب هایی مانند <table> ، <tr> ، <td> ایجاد کند |
همه فرمت های ممکن جدول Microsoft Word را نمی توان در HTML اعمال کرد |
شبیه سازی جدول موجود | شما می توانید یک کپی از یک جدول موجود ایجاد کنید، در حالی که تمام قالب بندی ردیف ها و سلول ها را حفظ کنید | قبل از آماده شدن جدول برای استفاده، گره های فرزند مناسب باید حذف شوند |