إنشاء جدول
Aspose.Words يسمح للمستخدمين بإنشاء جداول في مستند من البداية ويوفر عدة طرق مختلفة للقيام بذلك. تقدم هذه المقالة تفاصيل حول كيفية إضافة جداول منسقة إلى المستند باستخدام كل طريقة، بالإضافة إلى مقارنة بين كل طريقة في نهاية المقالة.
أنماط الجدول الافتراضي
يتم إعطاء الجدول الذي تم إنشاؤه حديثا قيما افتراضية مماثلة لتلك المستخدمة في Microsoft Word:
خاصية الجدول | الافتراضي في Aspose.Words |
---|---|
Border Style |
Single |
Border Width |
1/2 pt |
لون الحدود | Black |
Left and Right Padding |
5.4 pts |
AutoFit Mode |
AutoFit to Window |
Allow AutoFit |
True |
يمكن أن يكون الجدول مضمنا إذا تم وضعه بإحكام، أو عائما إذا كان يمكن وضعه في أي مكان على الصفحة. بشكل افتراضي، يقوم Aspose.Words دائما بإنشاء جداول مضمنة.
|
إنشاء جدول مع 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-Java.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(getArtifactsDir() + "WorkingWithTables.CreateSimpleTable.docx"); |
يوضح مثال التعليمات البرمجية التالية كيفية إنشاء جدول منسق باستخدام DocumentBuilder:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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.setLeftIndent(20.0); | |
// Set height and define the height rule for the header row. | |
builder.getRowFormat().setHeight(40.0); | |
builder.getRowFormat().setHeightRule(HeightRule.AT_LEAST); | |
builder.getCellFormat().getShading().setBackgroundPatternColor(new Color((198), (217), (241))); | |
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); | |
builder.getFont().setSize(16.0); | |
builder.getFont().setName("Arial"); | |
builder.getFont().setBold(true); | |
builder.getCellFormat().setWidth(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.getCellFormat().setWidth(200.0); | |
builder.write("Header Row,\n Cell 3"); | |
builder.endRow(); | |
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE); | |
builder.getCellFormat().setWidth(100.0); | |
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); | |
// Reset height and define a different height rule for table body. | |
builder.getRowFormat().setHeight(30.0); | |
builder.getRowFormat().setHeightRule(HeightRule.AUTO); | |
builder.insertCell(); | |
// Reset font formatting. | |
builder.getFont().setSize(12.0); | |
builder.getFont().setBold(false); | |
builder.write("Row 1, Cell 1 Content"); | |
builder.insertCell(); | |
builder.write("Row 1, Cell 2 Content"); | |
builder.insertCell(); | |
builder.getCellFormat().setWidth(200.0); | |
builder.write("Row 1, Cell 3 Content"); | |
builder.endRow(); | |
builder.insertCell(); | |
builder.getCellFormat().setWidth(100.0); | |
builder.write("Row 2, Cell 1 Content"); | |
builder.insertCell(); | |
builder.write("Row 2, Cell 2 Content"); | |
builder.insertCell(); | |
builder.getCellFormat().setWidth(200.0); | |
builder.write("Row 2, Cell 3 Content."); | |
builder.endRow(); | |
builder.endTable(); | |
doc.save(getArtifactsDir() + "WorkingWithTables.FormattedTable.docx"); |
يوضح مثال التعليمات البرمجية التالية كيفية إدراج جدول متداخل باستخدام DocumentBuilder:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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.getFirstParagraph()); | |
// Build the inner table. | |
builder.insertCell(); | |
builder.writeln("Inner Table Cell 1"); | |
builder.insertCell(); | |
builder.writeln("Inner Table Cell 2"); | |
builder.endTable(); | |
doc.save(getArtifactsDir() + "WorkingWithTables.NestedTable.docx"); |
إنشاء جدول عبر DOM (نموذج كائن المستند)
يمكنك إدراج الجداول مباشرة في DOM عن طريق إضافة عقدة Table جديدة في موضع معين.
يرجى ملاحظة أنه بعد إنشاء عقدة الجدول مباشرة، سيكون الجدول نفسه فارغا تماما، أي أنه لا يحتوي بعد على صفوف وخلايا. لإدراج صفوف وخلايا في جدول، أضف العقد الفرعية Row و Cell المناسبة إلى DOM.
يوضح مثال التعليمات البرمجية التالية كيفية إنشاء جدول جديد من البداية بإضافة العقد الفرعية المناسبة إلى شجرة المستند:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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.getFirstSection().getBody().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.getRowFormat().setAllowBreakAcrossPages(true); | |
table.appendChild(row); | |
// We can now apply any auto fit settings. | |
table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS); | |
Cell cell = new Cell(doc); | |
cell.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE); | |
cell.getCellFormat().setWidth(80.0); | |
cell.appendChild(new Paragraph(doc)); | |
cell.getFirstParagraph().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.deepClone(false)); | |
row.getLastCell().appendChild(new Paragraph(doc)); | |
row.getLastCell().getFirstParagraph().appendChild(new Run(doc, "Row 1, Cell 2 Text")); | |
doc.save(getArtifactsDir() + "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-Java.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(getArtifactsDir() + "WorkingWithTables.InsertTableFromHtml.docx"); |
أدخل نسخة من جدول موجود
غالبا ما تكون هناك أوقات تحتاج فيها إلى إنشاء جدول استنادا إلى جدول موجود بالفعل في مستند. أسهل طريقة لتكرار جدول مع الاحتفاظ بجميع التنسيقات هي استنساخ عقدة الجدول باستخدام طريقة deepClone.
يمكن استخدام نفس الأسلوب لإضافة نسخ من صف أو خلية موجودة إلى جدول.
يوضح مثال الكود التالي كيفية تكرار جدول باستخدام منشئي العقدة:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "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.deepClone(true); | |
table.getParentNode().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.getParentNode().insertAfter(new Paragraph(doc), table); | |
doc.save(getArtifactsDir() + "WorkingWithTables.CloneCompleteTable.docx"); |
يوضح مثال الكود التالي كيفية استنساخ الصف الأخير من الجدول وإلحاقه بالجدول:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Tables.docx"); | |
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); | |
Row clonedRow = (Row) table.getLastRow().deepClone(true); | |
// Remove all content from the cloned row's cells. This makes the row ready for new content to be inserted into. | |
for (Cell cell : (Iterable<Cell>) clonedRow.getCells()) | |
cell.removeAllChildren(); | |
table.appendChild(clonedRow); | |
doc.save(getArtifactsDir() + "WorkingWithTables.CloneLastRow.docx"); |
إذا كنت تبحث في إنشاء جداول في مستند ينمو ديناميكيا مع كل سجل من مصدر البيانات الخاص بك، فلا ينصح بالطريقة المذكورة أعلاه. بدلا من ذلك، يتم تحقيق الإخراج المطلوب بسهولة أكبر باستخدام Mail merge مع المناطق. يمكنك معرفة المزيد عن هذه التقنية في Mail Merge مع المناطق القسم.
قارن طرق إنشاء جدول
Aspose.Words يوفر عدة طرق لإنشاء جداول جديدة في مستند. كل طريقة لها مزاياها وعيوبها، لذلك يعتمد اختيار استخدامها غالبا على الموقف المحدد.
دعونا نلقي نظرة فاحصة على هذه الطرق لإنشاء الجداول ومقارنة مزاياها وعيوبها:
الطريقة | المزايا | العيوب |
---|---|---|
عبر DocumentBuilder |
الطريقة القياسية لإدراج الجداول ومحتويات المستندات الأخرى | من الصعب في بعض الأحيان إنشاء العديد من أنواع الجداول في نفس الوقت مع نفس مثيل البناء |
عبر DOM | يتناسب بشكل أفضل مع الكود المحيط الذي ينشئ ويدرج العقد مباشرة في DOM دون استخدام DocumentBuilder | يتم إنشاء الجدول “فارغ”: قبل تنفيذ معظم العمليات، يجب استدعاء EnsureMinimum لإنشاء أي عقد فرعية مفقودة |
من HTML | يمكن إنشاء جدول جديد من HTML المصدر باستخدام علامات مثل <table> , <tr> , <td> |
لا يمكن تطبيق جميع تنسيقات الجدول Microsoft Word الممكنة على HTML |
استنساخ جدول موجود | يمكنك إنشاء نسخة من جدول موجود مع الاحتفاظ بجميع تنسيق الصف والخلية | يجب إزالة العقد الفرعية المناسبة قبل أن يصبح الجدول جاهزا للاستخدام |