Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI set Excel chart title

I'm creating an Excel workbook from scratch. One of the sheets contains a chart, and I want to set the chart title.

Apache POI has a setChartTitle method on HSSFChart, but neither XSSFChart nor the format-agnostic Chart have methods to set the chart title. Since I need to create .xlsx files this is a problem for me.

After much poking around in POI code and OOXML specifications I managed to come up with this code for setting the title on a newly created Chart:

    if (chart instanceof XSSFChart) {
        XSSFChart xchart = (XSSFChart) chart;
        CTChart ctChart = xchart.getCTChart();
        CTTitle title = ctChart.addNewTitle();
        CTTx tx = title.addNewTx();
        CTTextBody rich = tx.addNewRich();
        rich.addNewBodyPr();  // body properties must exist, but can be empty
        CTTextParagraph para = rich.addNewP();
        CTRegularTextRun r = para.addNewR();
        r.setT("My chart title");
    }

This seems to work - I can load the resulting file in Excel 2013 and the chart is there with the correct title.

Is there an easier way to do this? What gotchas would I need to look out for when changing a chart title in a workbook created in Excel?

like image 755
jk2607674 Avatar asked Jul 04 '26 11:07

jk2607674


1 Answers

Before this question remains unanswered for six years, here is the answer:

You can use the XSSFChart.setTitleText(String title) method.

Example:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFDrawing;

public class Chart {

    public static void main(String[] args) throws Exception {
        try (Workbook wb = new XSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream("chart.xlsx");) {
            Sheet sheet = wb.createSheet("Sheet1");

            XSSFDrawing drawing = (XSSFDrawing)sheet.createDrawingPatriarch();
            XSSFChart chart = drawing.createChart(drawing.createAnchor(0, 0, 0, 0, 0, 0, 12, 12));
    
            
            // --------------------->     Here we set the title
            chart.setTitleText("My chart title");
            // <---------------------

            
            XDDFDataSource<String> cat = XDDFDataSourcesFactory.fromArray(new String[]{"A","B","C","D"});
            XDDFNumericalDataSource<Double> val = XDDFDataSourcesFactory.fromArray(new Double[]{1d, 2d, 3d, 4d});

            XDDFChartData data = chart.createData(ChartTypes.PIE, null, null);
            data.setVaryColors(true);
            XDDFChartData.Series series = data.addSeries(cat, val);
            series.setTitle("Series", null);
            chart.plot(data);

            wb.write(fileOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I understand that XSSFChart.setTitleText("My chart title") was not available, when this question was asked.

like image 66
Mario Varchmin Avatar answered Jul 07 '26 01:07

Mario Varchmin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!