Deze tutorial somt twee verschillende manieren op om een tabel in een Word-document te maken met C#. We zullen eerst een tabel in Word maken met C# met behulp van een Aspose DocumentBuilder en later een Word-tabel maken met C# rechtstreeks in het Word-documentobjectmodel (DOM).
Stappen om een tabel in Word-document te maken met C#
- Verwijs naar het Aspose.Words for .NET-pakket van NuGet in oplossing
- Aspose.Words en Aspose.Words.Tables naamruimten importeren
- Maak DocumentBuilder of Table klasse-instantie
- Gebruik de InsertCell- of Table.Rows.Cells.Add-methode om cellen in de tabelrij toe te voegen
- Gebruik de methode DocumentBuilder.Write of Cell.AppendChild om alineatekst in te voegen
- Maak meerdere rijen in een tabel en voltooi het maken van tabellen in Word-document
- Opslaan als Word-document met tabel naar DOCX-indeling op schijf
Het volgende Aspose DocumentBuilder-codevoorbeeld kan worden gebruikt in de .NET-toepassing voor het maken van C# Word-tabellen.
Code om tabel in Word-document te maken met 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 wezen stelt deze Word C#-app voor het maken van tabellen u in staat een tabel in een Word-document in te voegen. Het voegt eerst twee cellen toe aan de eerste tabelrij en schrijft later tekstinhoud in de eerste cel en vervolgens in afbeelding toevoegen aan Word C# laatste cel.
Code om een Word-tabel te maken met C# (DOM-klassen)
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"); | |
} | |
} | |
} |
Het bovenstaande Aspose Word-tabel C#-codevoorbeeld presenteert slechts een alternatieve benadering voor het toevoegen van een tabelelement in een Word-documentobjectmodel met behulp van tabellen, rijen, cellen en alinea’s.