Tamas
Tamas

Reputation: 11

How can i change the legend in powerpoint with apache POI?

I am using java 21 and apache POI 5.4.0. The task is to generate a pptx file from a powerpoint file which works as template. The template has keys like {{ model.info.title }}. The template has graphs too. The graphs data is the following:

|  | {{ model.chartData[0].title }} | {{ model.chartData[1].title }} |
| Category 1 | {{ model.chartData[0].value }} | {{ model.chartData[1].value }} |

I can change the data behind the graph without a problem. The problem occurs when I open the generated pptx file and the graph has the values and have changed the graph. But the legend shows the keys nonetheless. If i choose the option "Change data" on the graph, it changes these keys instantly. But i can't achieve it from the java code.

The code is the following:

XSSFSheet sheet = frame.getChart().getWorkbook().getSheetAt(0);
       for (int i = 0; i < sheet.getLastRowNum(); i++) {
                XSSFRow row = sheet.getRow(i);

                for (int j = 0; j < row.getLastCellNum(); j++) {
                    XSSFCell cell = row.getCell(j);

                    if (cell.getStringCellValue().contains("{{ ")) {

                        String key = cell.getStringCellValue().substring(3, cell.getStringCellValue().length() - 3);

                        try {

                            Object value = findKey(key);

                            if (value instanceof Double valueDouble) {
                                valueDouble = valueDouble / 100;
                                cell.setCellValue(valueDouble);
                            } else {
                                cell.setCellValue(String.valueOf(value));
                            }

                        } catch (Exception ex) {
                            log.debug("Error.", ex);
                        }
                    }
                }
            }

            frame.getChart().replaceReferences(sheet);

It is not pretty. But basically I am trying to get the key from the cell. Get rid of the brackets. Do some magic to find the value I need, convert it to the necessary format and replace the references with the modified data. The frame is a XSLFGraphicFrame object.

Any idea why it doesn't refresh the legend values? It should be the first row of the table.

Upvotes: 1

Views: 39

Answers (0)

Related Questions