Accessing Table from Cell and Adding Values inside it using Row and Column Offsets
Normally, you add values inside the Table or List Object using Cell.PutValue() method. But sometimes, you might need to add values inside the Table or List Object using the row and column offsets.
In order to access Table or List Object from a cell, use the Cell.GetTable() method. To add values inside it using the row and column offsets, use the ListObject.PutCellValue method.
The following screenshot shows the source Excel file used inside the code. It contains the empty table and highlights the cell D5 which lies inside the table. We will access this table from cell D5 using Cell.GetTable() method and then add the values inside it using both Cell.PutValue() and ListObject.PutCellValue methods.
Example
Screenshots comparing the source and output files
![]() |
---|
The following screenshot shows the output Excel file generated by the code. As you can see cell D5 has a value and cell F6 which is at the offset 2,2 of the table has a value.
![]() |
---|
C# code to access table from cell and to add values inside it using row and column offsets
The following sample code loads the source Excel file as shown in the above screenshot and adds values inside the table and generates the output Excel file as shown above.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create workbook from source Excel file | |
Workbook workbook = new Workbook(dataDir + "source.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Access cell D5 which lies inside the table | |
Cell cell = worksheet.Cells["D5"]; | |
// Put value inside the cell D5 | |
cell.PutValue("D5 Data"); | |
// Access the Table from this cell | |
ListObject table = cell.GetTable(); | |
// Add some value using Row and Column Offset | |
table.PutCellValue(2, 2, "Offset [2,2]"); | |
// Save the workbook | |
workbook.Save(dataDir + "output_out.xlsx"); |