Remove charts in Excel worksheets as deleteRow only deletes the empty cell but not the chart

Hello Aspose Team,

I have a template that populates the chart if it has data. Here there is two charts in each page.The max total charts to be displayed in the template is 40. But the chart can be less than that i.e the number of data = number of charts. So, for deleting the charts which does not contains the data we are using deleteRow() function as below:

for (int i = 0; i < lastrow; i++) {

if (ws.getCells().get(i, Integer.parseInt(0)?.getValue() == “”) {

xx++

if (xx > 1) {

ws.getCells().deleteRows(i - 1, lastrow)

break

}

} else {

xx = 0

}

}

This function is just removing the content of the cell but not the charts. So, for removing the charts from the excel we worksheet.getCharts().remove function as below and then using the deleteRow() functions to delete the empty row.

for(int start = 39; start >= startRow; start–){

ws.getCharts().remove(ws.getCharts().InnerList[start])

}

Using this function is only deleting the chart in single page but not all. Is there anything else that needs to be done beside these?

We are using aspose plugin version 19.5 for Java.

Thanks,
HarishhealthRiskAssessmentReport.xlsx.zip (1.0 MB)

@HThagunna

I am moving this thread to Aspose.Cells forum where our concerned team will assist you further in this regard.

@HThagunna,

To remove all the charts in a worksheet, you may loop through charts collection (e.g. Get to know total charts via ChartCollection.getCount()) using its indexes and remove each chart using ChartCollection.removeAt(chartIndex) method in each iteration. Run loop from higher index to lower index.

If you still find any issue, kindly do share sample code (runnable) and output file, we will check it soon.

Thank you so much. Let us try your suggestion and get back to you.

Thanks,
Harish

The solution you provided worked for us. Thank you so much.

Thanks,
Harish

@HThagunna,

Good to know that the suggested solution works for your needs. In the event of further queries or comments , feel free to write us back.