W tym samouczku wymieniono dwa różne sposoby tworzenia tabeli w dokumencie programu Word przy użyciu języka C#. Najpierw utworzymy tabelę w Wordzie przy użyciu C# z pomocą Aspose DocumentBuilder, a później utworzymy tabelę Worda przy użyciu C# bezpośrednio w modelu obiektowym dokumentu (DOM) Worda.
Kroki tworzenia tabeli w dokumencie programu Word przy użyciu języka C#
- Odwołaj się do pakietu Aspose.Words for .NET z NuGet w rozwiązaniu
- Importuj przestrzenie nazw Aspose.Words i Aspose.Words.Tables
- Utwórz instancję klasy DocumentBuilder lub Table
- Użyj metody InsertCell lub Table.Rows.Cells.Add, aby dodać komórki w wierszu tabeli
- Użyj metody DocumentBuilder.Write lub Cell.AppendChild, aby wstawić tekst akapitu
- Utwórz wiele wierszy w tabeli i zakończ tworzenie tabeli w dokumencie programu Word
- Zapisz jako dokument Word z tabelą do formatu DOCX na dysku
Poniższy przykład kodu programu Aspose DocumentBuilder może być używany w aplikacji .NET do tworzenia tabeli C# Word.
Kod do tworzenia tabeli w dokumencie programu Word przy użyciu języka 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"); | |
} | |
} | |
} |
Zasadniczo ta aplikacja Word C# do tworzenia tabel umożliwia wstawianie tabeli do dokumentu Word. Najpierw dodaje dwie komórki w pierwszym wierszu tabeli, a następnie zapisuje treść tekstową w pierwszej komórce, a następnie dodaj obraz do Worda C# w ostatniej komórce.
Kod do tworzenia tabeli Word przy użyciu C# (klasy 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"); | |
} | |
} | |
} |
Powyższy przykład kodu Aspose Word C# do tworzenia tabeli przedstawia alternatywne podejście do dodawania elementu tabeli w modelu obiektowym dokumentu programu Word przy użyciu klas Table, Row, Cell i Paragraph.