Kathir
Kathir

Reputation: 67

Aspose Cells Java - Insert Row to Excel Table

I'm new to aspose cells. I've a template excel file that has a simple Data Table (with a header row and one empty row) and a bar chart (with the data table configured as source). With aspose cells version 8.1.1. i'm trying to read the excel file, get the table object and insert data into it.

My goal here is to insert data into the table and have the chart refreshed automatically.

This is how i'm taking table object from excel

Workbook workbook = new Workbook(new TestReportsProcessor().getClass().getResourceAsStream("/ChartRefreshTest.xlsx"));
Worksheet worksheet = workbook.getWorksheets().get("Sheet1");
ListObjectCollection listObjectCollection = worksheet.getListObjects();
ListObject table = listObjectCollection.get(0);

But i don't see any method in the ListObject that has option to add/insert row into the table.

Option i have tried : I could get the cell position and insert data into cells manually by getting table start and end rows & column positions. But, when i insert data that way, the data outside the table's preset end position is not inserted as part of the table. i.e. The table is not expanding as i insert rows.

Could you please help?

Upvotes: 1

Views: 2616

Answers (2)

user4548650
user4548650

Reputation: 26

you can insert records into table object.when all records done, you should update your table's range. in aspose ListObject object, there is one method:
public void Resize(int startRow, int startColumn, int endRow, int endColumn, bool hasHeaders); which will udpate your table's orinal range to new one.

Upvotes: 1

Saqib
Saqib

Reputation: 91

Use the insertRows methods in Cells collection to insert rows in your worksheet. Here is a sample code.

Workbook workbook = new Workbook(new TestReportsProcessor.class.getResourceAsStream("/ChartRefreshTest.xlsx"));
Worksheet worksheet = workbook.getWorksheets().get("Sheet1");
// Insert a row at index 3
worksheet.getCells().insertRows(3);

For more information read Inserting and Deleting Rows and Columns.

Upvotes: 0

Related Questions