Skep'n Tabel

Aspose.Words laat gebruikers toe om tabelle in’n dokument van nuuts af te skep en bied verskeie verskillende metodes om dit te doen. Hierdie artikel bied besonderhede oor hoe om geformateerde tabelle by u dokument te voeg deur elke metode te gebruik, asook’n vergelyking van elke metode aan die einde van die artikel.

Verstek Tabel Style

Die nuutgeskepte tabel kry standaardwaardes soortgelyk aan die wat in Microsoft Wordgebruik word:

Tabel Eienskap Verstek in 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

Skep’n Tabel met DocumentBuilder

In Aspose.Words kan gebruikers’n tabel in’n dokument skep met behulp van die DocumentBuilder. Die basiese algoritme vir die skep van’n tabel is soos volg:

  1. Begin die tabel met StartTable
  2. Voeg’n sel by die tabel met InsertCell - dit begin outomaties’n nuwe ry
  3. Gebruik opsioneel die CellFormat eienskap om selformatering te spesifiseer
  4. Voeg die selinhoud in met behulp van die toepaslike DocumentBuilder metodes soos Writeln, InsertImage, en ander
  5. Herhaal stappe 2-4 totdat die ry voltooi is
  6. Roep EndRow om die huidige ry te beëindig
  7. Opsioneel, gebruik die RowFormat eienskap om ry formatering spesifiseer
  8. Herhaal stappe 2-7 totdat die tabel voltooi is
  9. Bel EndTable om die tafel klaar te bou

Die proses om’n tabel te skep, kan duidelik in die volgende prentjie gesien word:

creating-table-process

Die volgende kode voorbeeld toon hoe om’n eenvoudige tabel te skep met behulp van DocumentBuilder met standaard formatering:

// 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");

Die volgende kode voorbeeld toon hoe om’n geformateerde tabel te skep met behulp van 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");

Die volgende kode voorbeeld toon hoe om’n geneste tabel te voeg met behulp van 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");
view raw nested-table.cs hosted with ❤ by GitHub

Skep’n Tabel via DOM (Dokumentobjekmodel)

Jy kan tabelle direk in die DOM invoeg deur’n nuwe Table knoop by’n spesifieke posisie by te voeg.

Let asseblief daarop dat onmiddellik na die tabelknoop skepping, die tabel self heeltemal leeg sal wees, dit wil sê dit bevat nog nie rye en selle nie. Om rye en selle in’n tabel in te voeg, voeg die toepaslike Row en Cell kind knope by die DOM.

Die volgende kode voorbeeld toon hoe om’n nuwe tabel van nuuts af te bou deur die toevoeging van die toepaslike kind nodes om die dokument boom:

// 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");

Skep’n Tabel van HTML

Aspose.Words ondersteun die invoeging van inhoud in’n dokument van’n HTML bron met behulp van die InsertHtml metode. Die insette kan’n volledige HTML bladsy of net’n gedeeltelike uittreksel wees.

Met behulp van die InsertHtml metode, kan gebruikers tabelle in die dokument invoeg via tabel etikette soos <table>, <tr>, <td>.

Die volgende kode voorbeeld toon hoe om’n tabel in’n dokument van’n string wat HTML tags:

// 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");

Voeg’n Kopie Van’n Bestaande Tabel in

Daar is dikwels tye wanneer jy’n tabel moet skep gebaseer op’n reeds bestaande tabel in’n dokument. Die maklikste manier om’n tabel te dupliseer terwyl al die formatering behou word, is om die Tabel-knooppunt te kloon met behulp van die Clone - metode.

Dieselfde tegniek kan gebruik word om kopieë van’n bestaande ry of sel by’n tabel te voeg.

Die volgende kode voorbeeld toon hoe om’n tabel te dupliseer met behulp van node konstruktors:

// 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");

Die volgende kode voorbeeld toon hoe om die laaste ry van’n tabel te kloon en voeg dit by die tabel:

// 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");

As u na tabelle in’n dokument kyk wat dinamies groei met elke rekord uit u databron, word die bogenoemde metode nie aanbeveel nie. In plaas daarvan word die gewenste uitset makliker bereik deur Mail merge met streke te gebruik. Jy kan meer leer oor hierdie tegniek in die Mail Merge Met Streke afdeling.

Vergelyk Maniere Om’n Tabel Te Skep

Aspose.Words bied verskeie metodes om nuwe tabelle in’n dokument te skep. Elke metode het sy eie voordele en nadele, so die keuse van wat om te gebruik hang dikwels af van die spesifieke situasie.

Kom ons kyk van naderby na hierdie maniere om tabelle te skep en vergelyk hul voor-en nadele:

Metode Voordele Nadele
Deur DocumentBuilder Die standaardmetode vir die invoeging van tabelle en ander dokumentinhoud Soms moeilik om baie variëteite van tabelle te skep op dieselfde tyd met dieselfde bouer geval
Deur DOM Pas in beter met omliggende kode wat nodes direk in die DOM skep en invoeg sonder om’n DocumentBuilderte gebruik Die tabel word “leeg” geskep: voordat u die meeste bewerkings uitvoer, moet u EnsureMinimum skakel om ontbrekende kindknope te skep
Van HTML Kan’n nuwe tabel van HTML bron met behulp van tags soos skep <table>, <tr>, <td> Nie alle moontlike Microsoft Word tabel formate kan toegepas word op HTML
Kloning van’n bestaande tabel U kan’n kopie van’n bestaande tabel skep terwyl u alle ry-en selformatering behou Die toepaslike kind nodes moet verwyder word voordat die tabel gereed is vir gebruik