Bu öğretici, C# kullanarak Word belgesinde tablo oluşturmanın iki farklı yolunu listeler. Önce bir Aspose DocumentBuilder yardımıyla C# kullanarak Word’de tablo oluşturacağız ve daha sonra doğrudan Word belge nesne modelinde (DOM) C# kullanarak Word tablosu oluşturacağız.
C# Kullanarak Word Belgesinde Tablo Oluşturma Adımları
- Çözümde NuGet’ten Aspose.Words for .NET paketine başvurun
- Aspose.Words ve Aspose.Words.Tables ad alanlarını içe aktarın
- DocumentBuilder veya Table sınıf örneği oluşturun
- Tablo satırına hücre eklemek için InsertCell veya Table.Rows.Cells.Add yöntemini kullanın
- Paragraf metni eklemek için DocumentBuilder.Write veya Cell.AppendChild yöntemini kullanın
- Tabloda birden çok satır oluşturun ve Word belgesinde tablo oluşturmayı tamamlayın
- Diskte DOCX formatına tablolu Word belgesi olarak kaydedin
Aşağıdaki Aspose DocumentBuilder kod örneği, C# Word tablosu oluşturmak için .NET uygulamasında kullanılabilir.
C# ile Word Belgesinde Tablo Oluşturma Kodu
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"); | |
} | |
} | |
} |
Esasen, bu tablo oluşturma Word C# uygulaması, Word belgesine tablo eklemenizi sağlar. Önce ilk tablo satırına iki hücre ekler ve daha sonra metin içeriğini ilk hücreye ve ardından Word C#‘a resim ekleme son hücreye yazar.
C# (DOM Sınıfları) Kullanarak Kelime Tablosu Oluşturma Kodu
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"); | |
} | |
} | |
} |
Yukarıdaki Aspose Word tablo oluşturma C# kod örneği, Word belgesi nesne modeline Table, Row, Cell ve Paragraph sınıflarını kullanarak bir tablo öğesi eklemek için alternatif bir yaklaşım sunar.