ליצור שולחן

Aspose.Words מאפשר למשתמשים ליצור טבלאות במסמך מאפס ומספק מספר שיטות שונות לעשות זאת. מאמר זה מציג פרטים על איך להוסיף טבלאות מעוצבות למסמך שלך באמצעות כל שיטה, כמו גם השוואה של כל שיטה בסוף המאמר.

Default Tables

השולחן החדש שנוצר ניתן ערכי ברירת מחדל דומים לאלה המשמשים 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

צור שולחן עם DocumentBuilder

In In In Aspose.Words, משתמשים יכולים ליצור שולחן במסמך באמצעות DocumentBuilder. האלגוריתם הבסיסי ליצירת שולחן הוא כדלקמן:

1.1 1. התחל את השולחן עם StartTable 2. הוסף תא לשולחן באמצעות InsertCell זה מתחיל באופן אוטומטי שורה חדשה 3. באופן אופציונלי, השתמש CellFormat קטגוריה: cell Format 4. הכנס את התוכן התאי באמצעות המתאים DocumentBuilder שיטות כגון Writeln, InsertImage, ואחרים 5. חזור על שלבים 2-4 עד שהשורה הושלמה 6. Call EndRow לסיום השורה הנוכחית 7. באופן אופציונלי, השתמש RowFormat רכוש כדי לציין פורמט שורות 8. חזור על שלבים 2-7 עד להשלמת השולחן 9. Call EndTable לסיים את בניית השולחן

תהליך יצירת שולחן ניתן לראות בבירור בתמונה הבאה:

creating-table-process

דוגמה לקוד הבא מראה כיצד ליצור שולחן פשוט באמצעות 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 ()Document Object Model)

ניתן להכניס טבלאות ישירות לתוך 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 שיטה.

אותה טכניקה ניתן להשתמש כדי להוסיף עותקים של שורה או תא קיים לשולחן.

הדוגמה הבאה של הקוד מראה כיצד לשכפל שולחן באמצעות בונה Node:

// 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 מספק מספר שיטות ליצירת טבלאות חדשות במסמך. לכל שיטה יש יתרונות וחסרונות משלה, ולכן הבחירה של מי להשתמש לעתים קרובות תלויה במצב הספציפי.

בואו נסתכל מקרוב על דרכים אלה ליצירת טבלאות ולהשוות את היתרונות והחסרונות שלהם:

שיטות יתרונות חסרונות
Via DocumentBuilder השיטה הסטנדרטית להוספת טבלאות ותוכן מסמך אחר לפעמים קשה ליצור סוגים רבים של טבלאות בו זמנית עם אותו בונה
Via DOM Fits in טוב יותר עם קוד מקיף שיוצר ומכניס נקודות ישירות לתוך הבלוטות ישירות לתוך DOM בלי להשתמש DocumentBuilder השולחן נוצר “ריק”: לפני ביצוע רוב הפעולות, עליך להתקשר EnsureMinimum ליצור צומת ילדים חסר
מתוך HTML יכול ליצור שולחן חדש ממקור HTML באמצעות תגים כמו <table>, <tr>, <td> לא הכל אפשרי Microsoft Word ניתן ליישם פורמטים בטבלה ב-HTML
מקליד שולחן קיים אתה יכול ליצור עותק של שולחן קיים תוך שמירה על כל שורות ותבנית תאים יש להסיר את בלוטות הילד המתאים לפני שהשולחן מוכן לשימוש