Kako stvoriti tablicu u Word dokumentu koristeći C#

Ovaj vodič navodi dva različita načina za stvaranje tablice u Word dokumentu koristeći C#. Prvo ćemo izraditi tablicu u Wordu koristeći C# uz pomoć Aspose DocumentBuilder-a, a kasnije ćemo kreirati Wordovu tablicu koristeći C# izravno u objektnom modelu Word dokumenta (DOM).

Koraci za stvaranje tablice u Word dokumentu pomoću C#

  1. Referirajte paket Aspose.Words for .NET iz NuGet-a u rješenju
  2. Uvoz imenskih prostora Aspose.Words i Aspose.Words.Tables
  3. Stvorite instancu klase DocumentBuilder ili Table
  4. Koristite metodu InsertCell ili Table.Rows.Cells.Add za dodavanje ćelija u retku tablice
  5. Koristite metodu DocumentBuilder.Write ili Cell.AppendChild za umetanje teksta odlomka
  6. Stvorite više redaka u tablici i dovršite izradu tablice u Word dokumentu
  7. Spremite kao Word dokument s tablicom u DOCX format na disk

Sljedeći primjer koda Aspose DocumentBuilder može se koristiti u .NET aplikaciji za stvaranje C# Word tablice.

Kod za stvaranje tablice u Word dokumentu pomoću C#

using Aspose.Words;
using Aspose.Words.Tables;
namespace CreateTableInWordDocumentUsingCsharp
{
class CreateTableWord
{
static void Main(string[] args)
{
// Set license before C# Word table creation
License setupToCreateTable = new License();
setupToCreateTable.SetLicense(@"license file path.lic");
// Document Builder object to create Table in Word document
DocumentBuilder toCreateTableInWord = new DocumentBuilder();
// Mark the start of Word Table
Table tableInWord = toCreateTableInWord.StartTable();
// Insert a new Row & then first Cell in Word Table
toCreateTableInWord.InsertCell();
// Write text in Table Cell
toCreateTableInWord.Write("Table Row 1 and Cell 1");
// Insert a new Cell in same Word Table Row
toCreateTableInWord.InsertCell();
// Insert an Image in Word Table Cell
toCreateTableInWord.InsertImage("insert image in word table.jpg");
// Mark the end of Table Row
toCreateTableInWord.EndRow();
// Mark end of Word Table creation
toCreateTableInWord.EndTable();
// Save Word DOCX document with Table on disk
toCreateTableInWord.Document.Save("c# word table created in.docx");
}
}
}

U osnovi, ova Word C# aplikacija za stvaranje tablice omogućuje umetanje tablice u Word dokument. Prvo dodaje dvije ćelije u prvi redak tablice, a kasnije upisuje tekstualni sadržaj u prvu ćeliju, a zatim dodaj sliku u Word C# posljednju ćeliju.

Kod za stvaranje Wordove tablice pomoću C# (DOM klase)

using Aspose.Words;
using Aspose.Words.Tables;
namespace CreateWordTable
{
class CreateTableInWord
{
static void Main(string[] args)
{
// Set license before creating Word table
License license = new License();
license.SetLicense(@"license file path.lic");
Document wordDocument = new Document(); // Create empty Word document
Table wordTable = new Table(wordDocument); // Create Table object
wordTable.Rows.Add(new Row(wordDocument)); // Create a Row inside Table
wordTable.FirstRow.Cells.Add(new Cell(wordDocument)); // Create a single Cell in Table Row
wordTable.FirstRow.FirstCell.AppendChild(new Paragraph(wordDocument)); // Add a Paragraph inside Cell
// Write text content inside Word Table Cell
wordTable.FirstRow.FirstCell.FirstParagraph.Runs.Add(new Run(wordDocument, "Text in Table Row 1 and Cell 1"));
// Insert Table at the end of Word Document
wordDocument.FirstSection.Body.InsertBefore(wordTable, wordDocument.FirstSection.Body.LastParagraph);
// Save Word document to DOCX format
wordDocument.Save("c# word table created in.docx");
}
}
}

Gornji primjer Aspose Word kreiranja tablice C# koda samo predstavlja alternativni pristup za dodavanje elementa tablice u objektni model Wordovog dokumenta pomoću klasa Table, Row, Cell i Paragraph.

 Hrvatski