Այս ձեռնարկը թվարկում է աղյուսակ ստեղծելու երկու տարբեր եղանակներ Word փաստաթղթում C#-ի միջոցով: Մենք նախ կստեղծենք աղյուսակ Word-ում C#-ի միջոցով Aspose DocumentBuilder-ի օգնությամբ, իսկ ավելի ուշ կստեղծենք Word աղյուսակը C#-ի միջոցով անմիջապես Word փաստաթղթի օբյեկտի մոդելում (DOM):
Word փաստաթղթում աղյուսակ ստեղծելու քայլեր՝ օգտագործելով C#
- Հղեք NuGet-ի Aspose.Words for .NET փաթեթին լուծույթում
- Ներմուծեք Aspose.Words և Aspose.Words.Tables անվանումների տարածքները
- Ստեղծեք DocumentBuilder կամ Table դասի օրինակ
- Աղյուսակային տողում բջիջներ ավելացնելու համար օգտագործեք InsertCell կամ Table.Rows.Cells.Add մեթոդը
- Պարբերության տեքստը տեղադրելու համար օգտագործեք DocumentBuilder.Write կամ Cell.AppendChild մեթոդը
- Ստեղծեք մի քանի տող աղյուսակում և ավարտեք աղյուսակի ստեղծումը Word փաստաթղթում
- Պահպանեք որպես Word փաստաթուղթ սկավառակի վրա աղյուսակով DOCX ձևաչափով
Aspose DocumentBuilder կոդի հետևյալ օրինակը կարող է օգտագործվել .NET հավելվածում՝ C# Word աղյուսակ ստեղծելու համար:
Կոդ՝ Word փաստաթղթում աղյուսակ ստեղծելու համար՝ օգտագործելով 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"); | |
} | |
} | |
} |
Ըստ էության, աղյուսակ ստեղծելու այս Word C# հավելվածը հնարավորություն է տալիս աղյուսակ տեղադրել Word փաստաթղթում: Այն նախ ավելացնում է երկու բջիջ աղյուսակի առաջին տողում, իսկ հետո գրում է տեքստի բովանդակությունը առաջին բջիջում, իսկ հետո՝ ավելացնել պատկեր Word C#-ին վերջին բջիջում:
Բառի աղյուսակ ստեղծելու կոդ՝ օգտագործելով C# (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"); | |
} | |
} | |
} |
Վերոհիշյալ Aspose Word-ի ստեղծման աղյուսակի C# կոդի օրինակը պարզապես ներկայացնում է այլընտրանքային մոտեցում Word փաստաթղթի օբյեկտի մոդելում աղյուսակի տարր ավելացնելու համար՝ օգտագործելով Table, Row, Cell և Paragraph դասերը: