Questa esercitazione elenca due modi diversi per creare tabelle nel documento di Word usando C#. Creeremo prima una tabella in Word usando C# con l’aiuto di un Aspose DocumentBuilder e successivamente creeremo una tabella di Word usando C# direttamente nel modello a oggetti del documento di Word (DOM).
Passaggi per creare una tabella nel documento di Word utilizzando C#
- Fare riferimento al pacchetto Aspose.Words for .NET di NuGet nella soluzione
- Importa gli spazi dei nomi Aspose.Words e Aspose.Words.Tables
- Crea un’istanza di classe DocumentBuilder o Table
- Utilizza il metodo InsertCell o Table.Rows.Cells.Add per aggiungere celle nella riga della tabella
- Utilizzare il metodo DocumentBuilder.Write o Cell.AppendChild per inserire il testo del paragrafo
- Crea più righe nella tabella e completa la creazione della tabella nel documento di Word
- Salva come documento Word con tabella in formato DOCX su disco
L’esempio di codice Aspose DocumentBuilder seguente può essere usato nell’applicazione .NET per la creazione di tabelle Word in C#.
Codice per creare una tabella nel documento di Word usando 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"); | |
} | |
} | |
} |
In sostanza, questa app Word C# per la creazione di tabelle consente di inserire una tabella nel documento di Word. Prima aggiunge due celle nella prima riga della tabella e successivamente scrive il contenuto del testo nella prima cella e poi aggiungi immagine a Word C# nell’ultima cella.
Codice per creare tabelle di Word utilizzando C# (classi DOM)
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"); | |
} | |
} | |
} |
L’esempio di codice C# Aspose Word sopra riportato presenta solo un approccio alternativo per l’aggiunta di un elemento tabella nel modello a oggetti del documento di Word usando le classi Tabella, Riga, Cella e Paragrafo.