We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding data into an existing ListObject

Scenario:
My project puts data into a template.xlsx supplied by management.
This template.xslx contains an empty, formatted table (ListObject).

I would like to add data into this table/ListObject so that I don’t have to recreate the formatting in code, and to allow management to finetune the formatting without me having to change my code.

I was hoping to be able to do something like this:

var table = sheet.ListObjects[“Table1”];
table.Data.Add(“data for column 1”, “data for column 2”, …)

Is something like this possible, or do you suggest another solution?
This message was posted using Page2Forum from Aspose.Cells for .NET - Documentation

Hi,


Well, you may insert rows (Cells.InsertRows()) and then put values into the cells accordingly in the ListObject/Table range.

Also, you may import even a datatable to paste into ListObject accordingly. See a sample code below:

Sample code:
//I open a file that contains a list object already.
Workbook workbook = new Workbook(“e:\test\MyListTable.xls”);
Worksheet worksheet = workbook.Worksheets[0];

DataSet ds = new DataSet();
System.Data.DataTable dt = new DataTable(“Table1”);
dt.Columns.Add(“Customer Identification Number”, typeof(string));
dt.Columns.Add(“Customer Name”, typeof(string));
dt.Columns.Add(“Description Text”, typeof(string));
for (int i = 1; i <= 20; i++)
{
System.Data.DataRow dr = dt.NewRow();
dr[“Customer Identification Number”] = i.ToString();
dr[“Customer Name”] = "Name " + i;
dr[“Description Text”] = "Description " + i;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);


worksheet.Cells.InsertRows(1, ds.Tables[0].Rows.Count);

bool exportFieldName = true;

if (exportFieldName)
{
//put true for the last parameter, doing so, it will first insert blank rows then paste data
worksheet.Cells.ImportDataTable(ds.Tables[0], true, 0, 0, true);

workbook.Worksheets[0].ListObjects[0].UpdateColumnName();

}

else
{

worksheet.Cells.ImportDataTable(ds.Tables[0], false, 1, 0, true);

}
workbook.Save(@“E:\test\output.xlsx”);

Thank you.

Thanks, managed to get it to work with InsertRows().

The styling was messed up at first, causing me to ask the question here, but the cause was extra hardcoded styling done on table cells in the template delivered to me.

Hi,


Good to know that you have sorted out now.

Thank you.