Hur man skapar tabell i Word-dokument med C#

Denna handledning listar två olika sätt att skapa tabeller i Word-dokument med C#. Vi kommer först att skapa tabell i Word med C# med hjälp av en Aspose DocumentBuilder och senare skapa Word-tabell med C# direkt i Word Document Object Model (DOM).

Steg för att skapa tabell i Word-dokument med C#

  1. Referera till Aspose.Words for .NET-paketet från NuGet i lösning
  2. Importera namnrymder Aspose.Words och Aspose.Words.Tables
  3. Skapa klassinstansen DocumentBuilder eller Table
  4. Använd metoden InsertCell eller Table.Rows.Cells.Add för att lägga till celler i tabellraden
  5. Använd metoden DocumentBuilder.Write eller Cell.AppendChild för att infoga stycketext
  6. Skapa flera rader i tabellen och avsluta tabellskapandet i Word-dokument
  7. Spara som Word-dokument med tabell till DOCX-format på disk

Följande Aspose DocumentBuilder-kodexempel kan användas i .NET-applikationen för att skapa C# Word-tabeller.

Kod för att skapa tabell i Word-dokument med 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");
}
}
}

I huvudsak låter den här skapa tabell Word C#-appen dig infoga en tabell i Word-dokument. Den lägger först till två celler i den första tabellraden och skriver senare textinnehåll i första cellen och sedan lägg till bild i Word C# sista cellen.

Kod för att skapa ordtabell med C# (DOM-klasser)

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

Ovanstående Aspose Word skapa tabell C#-kodexempel presenterar bara ett alternativt tillvägagångssätt för att lägga till ett tabellelement i Word-dokumentobjektmodell med hjälp av tabell-, rad-, cell- och styckeklasserna.

 Svenska