צור טבלה
Aspose.Words מאפשר למשתמשים ליצור טבלאות במסמך מאפס ומספק מספר שיטות שונות לעשות זאת. מאמר זה מציג פרטים כיצד להוסיף טבלאות מעוצבות למסמך שלך בכל שיטה, כמו גם השוואה של כל שיטה בסוף המאמר.
ברירת מחדל שולחן סגנונות
הטבלה החדשה שנוצרה מקבלת ערכי ברירת מחדל דומים לאלה המשמשים ב Microsoft Word:
רכוש טבלה | ברירת מחדל ב Aspose.Words |
---|---|
Border Style |
Single |
Border Width |
1/2 פט |
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-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
// Start building the table. | |
builder->StartTable(); | |
builder->InsertCell(); | |
builder->Write(u"Row 1, Cell 1 Content."); | |
// Build the second cell. | |
builder->InsertCell(); | |
builder->Write(u"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(u"Row 2, Cell 1 Content"); | |
// Build the second cell. | |
builder->InsertCell(); | |
builder->Write(u"Row 2, Cell 2 Content."); | |
builder->EndRow(); | |
// Signal that we have finished building the table. | |
builder->EndTable(); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.CreateSimpleTable.docx"); |
דוגמת הקוד הבאה מראה כיצד ליצור טבלה מעוצבת באמצעות DocumentBuilder:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
SharedPtr<Table> table = builder->StartTable(); | |
builder->InsertCell(); | |
// Table wide formatting must be applied after at least one row is present in the table. | |
table->set_LeftIndent(20.0); | |
// Set height and define the height rule for the header row. | |
builder->get_RowFormat()->set_Height(40.0); | |
builder->get_RowFormat()->set_HeightRule(HeightRule::AtLeast); | |
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::FromArgb(198, 217, 241)); | |
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center); | |
builder->get_Font()->set_Size(16); | |
builder->get_Font()->set_Name(u"Arial"); | |
builder->get_Font()->set_Bold(true); | |
builder->get_CellFormat()->set_Width(100.0); | |
builder->Write(u"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(u"Header Row,\n Cell 2"); | |
builder->InsertCell(); | |
builder->get_CellFormat()->set_Width(200.0); | |
builder->Write(u"Header Row,\n Cell 3"); | |
builder->EndRow(); | |
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_White()); | |
builder->get_CellFormat()->set_Width(100.0); | |
builder->get_CellFormat()->set_VerticalAlignment(CellVerticalAlignment::Center); | |
// Reset height and define a different height rule for table body. | |
builder->get_RowFormat()->set_Height(30.0); | |
builder->get_RowFormat()->set_HeightRule(HeightRule::Auto); | |
builder->InsertCell(); | |
// Reset font formatting. | |
builder->get_Font()->set_Size(12); | |
builder->get_Font()->set_Bold(false); | |
builder->Write(u"Row 1, Cell 1 Content"); | |
builder->InsertCell(); | |
builder->Write(u"Row 1, Cell 2 Content"); | |
builder->InsertCell(); | |
builder->get_CellFormat()->set_Width(200.0); | |
builder->Write(u"Row 1, Cell 3 Content"); | |
builder->EndRow(); | |
builder->InsertCell(); | |
builder->get_CellFormat()->set_Width(100.0); | |
builder->Write(u"Row 2, Cell 1 Content"); | |
builder->InsertCell(); | |
builder->Write(u"Row 2, Cell 2 Content"); | |
builder->InsertCell(); | |
builder->get_CellFormat()->set_Width(200.0); | |
builder->Write(u"Row 2, Cell 3 Content."); | |
builder->EndRow(); | |
builder->EndTable(); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.FormattedTable.docx"); |
דוגמת הקוד הבאה מראה כיצד להוסיף טבלה מקוננת באמצעות DocumentBuilder:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
SharedPtr<Cell> cell = builder->InsertCell(); | |
builder->Writeln(u"Outer Table Cell 1"); | |
builder->InsertCell(); | |
builder->Writeln(u"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->get_FirstParagraph()); | |
// Build the inner table. | |
builder->InsertCell(); | |
builder->Writeln(u"Inner Table Cell 1"); | |
builder->InsertCell(); | |
builder->Writeln(u"Inner Table Cell 2"); | |
builder->EndTable(); | |
doc->Save(ArtifactsDir + u"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-C.git. | |
auto doc = MakeObject<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. | |
auto table = MakeObject<Table>(doc); | |
doc->get_FirstSection()->get_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. | |
auto row = MakeObject<Row>(doc); | |
row->get_RowFormat()->set_AllowBreakAcrossPages(true); | |
table->AppendChild(row); | |
// We can now apply any auto fit settings. | |
table->AutoFit(AutoFitBehavior::FixedColumnWidths); | |
auto cell = MakeObject<Cell>(doc); | |
cell->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightBlue()); | |
cell->get_CellFormat()->set_Width(80); | |
cell->AppendChild(MakeObject<Paragraph>(doc)); | |
cell->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, u"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->get_LastCell()->AppendChild(MakeObject<Paragraph>(doc)); | |
row->get_LastCell()->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, u"Row 1, Cell 2 Text")); | |
doc->Save(ArtifactsDir + u"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-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
// Note that AutoFitSettings does not apply to tables inserted from HTML. | |
builder->InsertHtml(String(u"<table>") + u"<tr>" + u"<td>Row 1, Cell 1</td>" + u"<td>Row 1, Cell 2</td>" + u"</tr>" + u"<tr>" + | |
u"<td>Row 2, Cell 2</td>" + u"<td>Row 2, Cell 2</td>" + u"</tr>" + u"</table>"); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.InsertTableFromHtml.docx"); |
הכנס עותק של טבלה קיימת
לעתים קרובות יש צורך ליצור טבלה המבוססת על טבלה שכבר קיימת במסמך. הדרך הקלה ביותר לשכפל טבלה תוך שמירה על כל העיצוב היא לשכפל את צומת הטבלה בשיטת Clone.
ניתן להשתמש באותה טכניקה כדי להוסיף עותקים של שורה או תא קיימים לטבלה.
דוגמת הקוד הבאה מראה כיצד לשכפל טבלה באמצעות בוני צומת:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(MyDir + u"Tables.docx"); | |
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true)); | |
// Clone the table and insert it into the document after the original. | |
auto tableClone = System::ExplicitCast<Table>(table->Clone(true)); | |
table->get_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->get_ParentNode()->InsertAfter(MakeObject<Paragraph>(doc), table); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.CloneCompleteTable.docx"); |
דוגמת הקוד הבאה מראה כיצד לשכפל את השורה האחרונה בטבלה ולהוסיף אותה לטבלה:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(MyDir + u"Tables.docx"); | |
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true)); | |
auto clonedRow = System::ExplicitCast<Row>(table->get_LastRow()->Clone(true)); | |
// Remove all content from the cloned row's cells. This makes the row ready for new content to be inserted into. | |
for (const auto& cell : System::IterateOver<Cell>(clonedRow->get_Cells())) | |
{ | |
cell->RemoveAllChildren(); | |
} | |
table->AppendChild(clonedRow); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.CloneLastRow.docx"); |
אם אתה מסתכל על יצירת טבלאות במסמך שצומחות באופן דינמי עם כל רשומה ממקור הנתונים שלך, השיטה לעיל אינה מומלצת. במקום זאת, הפלט הרצוי מושגת בקלות רבה יותר באמצעות Mail merge עם אזורים. אתה יכול ללמוד עוד על טכניקה זו ב Mail Merge עם אזורים סעיף.
השווה דרכים ליצירת טבלה
Aspose.Words מספק מספר שיטות ליצירת טבלאות חדשות במסמך. לכל שיטה יש יתרונות וחסרונות משלה, ולכן הבחירה שבה להשתמש לעתים קרובות תלוי במצב הספציפי.
בואו נסתכל מקרוב על דרכים אלה של יצירת טבלאות ולהשוות את היתרונות והחסרונות שלהם:
שיטה | יתרונות | חסרונות |
---|---|---|
DocumentBuilder |
השיטה הסטנדרטית להכנסת טבלאות ותוכן מסמכים אחר | לפעמים קשה ליצור סוגים רבים של שולחנות בו זמנית עם אותו מופע בונה |
דרך DOM | מתאים יותר עם קוד שמסביב שיוצר ומכניס צמתים ישירות לתוך DOM ללא שימוש DocumentBuilder | הטבלה נוצרת “ריק”: לפני ביצוע רוב הפעולות, עליך להתקשר EnsureMinimum כדי ליצור צמתים חסרים של ילדים |
מ HTML | ניתן ליצור טבלה חדשה מ HTML מקור באמצעות תגים כמו <table> , <tr> , <td> |
לא ניתן להחיל את כל פורמטי הטבלה Microsoft Word על HTML |
שיבוט טבלה קיימת | ניתן ליצור עותק של טבלה קיימת תוך שמירה על כל עיצוב השורות והתאים | יש להסיר את הצמתים המתאימים לפני שהטבלה מוכנה לשימוש |