问题描述
我正在从头开始创建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规范之后,我设法提出了以下代码来在新创建的Chart上设置标题:
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(我的图表标题")
不可用.
In understand that XSSFChart.setTitleText("My chart title")
was not available, when this question was asked.
这篇关于Apache POI设置Excel图表标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!