I den här handledningen kommer du att lära dig att programmässigt lägga till rader i tabellen i MS Word-dokument med C#. Vi kommer först att lägga till radtabell Word C# vid specificerat index i tabellens radsamling och senare kommer flera tomma C# Word-tabellrader att läggas till i slutet av tabellen.
Steg för att lägga till rader i tabell i MS Word-dokument med C#
- Installera paketet Aspose.Words for .NET i ditt projekt via NuGet
- Importera namnrymder Aspose.Words och Aspose.Words.Tables
- Öppna MS Word-dokument med C#-kod
- Hämta Table efter index som du vill lägga till rader till
- Skapa ett nytt radklassobjekt eller klona en befintlig rad från tabell
- Lägg till några celler med text Stycken till ovanstående rad
- Använd metoden RowCollection.Add för att lägga till rader i slutet av samlingen Table.Rows
- Eller använd metoden RowCollection.Insert för att infoga rader vid ett specifikt index
- Spara MS Word-dokument igen med tillagda tabellrader
Följande C#-kod för .NET-konsolapplikationen använder Aspose Words add rad till befintlig tabell i MS Word-dokument.
Kod för att lägga till rader i tabell i MS Word-dokument med C#
using Aspose.Words; | |
using Aspose.Words.Tables; | |
namespace AddRowsToTableInMsWordDocumentUsingCsharp | |
{ | |
class Aspose_Words_Table_Add_Row | |
{ | |
static void Main(string[] args) | |
{ | |
License setupToAddRowsToTable = new License(); | |
setupToAddRowsToTable.SetLicense("path to license file.lic"); | |
// Open MS Word Document | |
Document MSWordDocument = new Document(@"MS Word.docx"); | |
// Get the Table by index | |
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0]; | |
#region C# Word table insert row | |
// Create a new Row class object | |
Row row = new Row(MSWordDocument); | |
// Add three Cells to Row's cells collection | |
for (int i = 0; i < 3; i++) | |
{ | |
Cell cell = new Cell(MSWordDocument); | |
cell.AppendChild(new Paragraph(MSWordDocument)); | |
cell.FirstParagraph.Runs.Add(new Run(MSWordDocument, "Text in Cell " + i)); | |
row.Cells.Add(cell); | |
} | |
// Insert new Row after the first Row | |
tableToAddRowsTo.Rows.Insert(1, row); | |
#endregion | |
#region C# add rows to Word table | |
// Clone an existing Row from Table | |
Row cloneOfRow = (Row)tableToAddRowsTo.FirstRow.Clone(true); | |
// Remove all content from all Cells | |
foreach (Cell cell in cloneOfRow) | |
{ | |
cell.RemoveAllChildren(); | |
cell.EnsureMinimum(); | |
} | |
// Add multiple empty rows to the end of table | |
for (int i = 0; i < 10; i++) | |
{ | |
Row emptyRow = (Row)cloneOfRow.Clone(true); | |
tableToAddRowsTo.Rows.Add(emptyRow); | |
} | |
#endregion | |
MSWordDocument.Save(@"Added Rows to Table to MS Word.docx"); | |
} | |
} | |
} |
Det här C# Word-tabellexemplet måste i princip först hämta en befintlig tabell från MS Word-dokument med hjälp av dess index. Men om det inte finns någon tabell i dokumentet kan du till och med skapa tabell i Word med C# koda. Koden första C# Word-tabellen infoga rad vid det angivna indexet i radsamlingen. Sedan klonar den första tabellraden, tar bort allt innehåll från alla celler och slutligen kommer detta C#-lägg till rader i Word-tabellkodavsnittet att lägga till flera tomma rader i slutet av tabellen i MS Word-dokument.