Kā pievienot rādītājus tabulā MS Word dokumentā, izmantojot C#

Kā pievienot rādītājus tabulā MS Word dokumentā, izmantojot C#

Šajā apmācībā jūs uzzināsiet, kā programmatiski pievienot rindas tabulā MS Word dokumentā, izmantojot C#.

Vārdu dokumentu tabulās pievienošanas priekšrocības

  • Dinamiskā satura pārvaldība :- Vienkārši mainīt esošās tabulas, lai pielāgotu jaunus datus.

    • elastība ir:- Iestatīt tabulas struktūras programmatiski bez manuāli rediģēšanas.
    • Automašīna ir:- Efektīvi pārvaldīt tabulas datus, izmantojot automatizētus skriptus.

Priekšnoteikumi: Vides sagatavošana

  • Iegūstiet Visual Studio vai jebkuru .NET IDE.
  • Pārliecinieties, ka Aspose.Words bibliotēka ir pieejama caur NuGet.

Pakāpeniski ceļvedis, kā pievienot rādītājus tabulā Word

1. solis: Instalēt Aspose.Words bibliotēku

Instalējiet Aspose.Words paketi, izmantojot NuGet paketes vadītāju.

Install-Package Aspose.Words

2. solis: nepieciešamo nosaukuma telpu importēšana

Ievadiet Aspose.Words un Asposa.Vords.Tabeles nosaukuma telpas savā projektā.

using Aspose.Words;
using Aspose.Words.Tables;

3. solis: Atveriet vārda dokumentu

Ievietojiet esošo MS Word dokumentu.

Document MSWordDocument = new Document(@"MS Word.docx");

4. solis: Ieeja pie galda

Iegūstiet tabulu ar tā indeksu no dokumenta.

Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

5. solis: izveidot vai klonēt līniju

Izveidojiet jaunu Roku vai klonējiet esošo no tabulas.

Row row = new Row(MSWordDocument);

6. solis: pievienojiet šūnas līmenim

Pievienojiet šūnām un tekstu rādītājam.

for (int i = 0; i < 3; i++) 
{
    Cell cell = new Cell(MSWordDocument);
    cell.AppendChild(new Paragraph(MSWordDocument));
    cell.FirstParagraph.Runs.Add(new Run(MSWordDocument, "Text in Cell " + i));
    row.Cells.Add(cell);
}

7. solis: pievienojiet rādītājus beigām

Izmantojiet RowCollection.Add, lai pievienotu Rows tabulā.

tableToAddRowsTo.Rows.Add(row);

8. solis: Ievietojiet rādītājus konkrētajā indekss

Ievadiet rādītājus konkrētajā indekss, izmantojot RowCollection.Insert.

tableToAddRowsTo.Rows.Insert(1, row);

9. solis: saglabāt atjaunināto dokumentu

Atkārtojiet dokumentu ar pievienotajām rindām.

MSWordDocument.Save(@"Added Rows to Table in MS Word.docx");

Piemērots kods, lai pievienotu rētas tabulā Word

Zemāk ir pilns kods, lai pievienotu rindas tabulā:

// Open MS Word Document
Document MSWordDocument = new Document(@"input.docx");

// Get the Table by index
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

// Create a new Row class object
Row row = new Row(MSWordDocument);

// Add three Cells to Row's cells collection
for (int i = 0; i < 3; i++)
{
    Cell cell = new Cell(MSWordDocument);
    cell.AppendChild(new Paragraph(MSWordDocument));
    cell.FirstParagraph.Runs.Add(new Run(MSWordDocument, "Text in Cell " + i));
    row.Cells.Add(cell);
}

// Insert new Row after the first Row
tableToAddRowsTo.Rows.Insert(1, row);

// Clone an existing Row from Table
Row cloneOfRow = (Row)tableToAddRowsTo.FirstRow.Clone(true);

// Remove all content from all Cells
foreach (Cell cell in cloneOfRow)
{
    cell.RemoveAllChildren();
    cell.EnsureMinimum();
}

// Add multiple empty rows to the end of table
for (int i = 0; i < 10; i++)
{
    Row emptyRow = (Row)cloneOfRow.Clone(true);
    tableToAddRowsTo.Rows.Add(emptyRow);
}

// Save updated document
MSWordDocument.Save(@"output.docx");

Conclusion

Šis apmācība ir parādījusi, kā pievienot rindas tabulā MS Word dokumentā, izmantojot C#. Izmantojot šos soļus, jūs varat efektīvi pārvaldīt un modificēt tabulas Word dokumentu programmā.

 Latviski