Apache POI 设置 Excel 图表标题 [英] Apache POI set Excel chart title

查看:43
本文介绍了Apache POI 设置 Excel 图表标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从头开始创建 Excel 工作簿.其中一张表包含图表,我想设置图表标题.

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 在 HSSFChart 上有 setChartTitle 方法,但 XSSFChart 和格式无关的 Chart 都没有设置图表标题的方法.由于我需要创建 .xlsx 文件,这对我来说是个问题.

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.

在研究 POI 代码和 OOXML 规范后,我设法想出了这段代码,用于在新创建的图表上设置标题:

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");
    }

这似乎有效 - 我可以在 Excel 2013 中加载生成的文件,并且图表的标题正确.

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

有没有更简单的方法来做到这一点?在 Excel 创建的工作簿中更改图表标题时,我需要注意哪些问题?

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?

推荐答案

在这个问题六年未得到解答之前,答案如下:

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

您可以使用 XSSFChart.setTitleText(String title) 方法.

示例:

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();
        }
    }
}

我知道在询问此问题时 XSSFChart.setTitleText("My chart title") 不可用.

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

这篇关于Apache POI 设置 Excel 图表标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆