Apache poi 也提供poi-ooxml-schemas。这是基于XML 的办公文档的基础对象。因此,只要不提供更高级别的对象,就可以尝试直接用这些对象解决他的要求。问题是这些对象的文档。据我所知,没有。但是有http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/。
因此,从现有图表示例中的XSSFChart,我们可以得到CTChart,然后使用http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/drawingml/x2006/chart/CTChart.java#CTChart 从一个对象到另一个对象。
首先直接使用Excel 创建一个简单的*.xlsx 文件并查看其XML 内容会很有帮助。在那里我们可以得到需要什么对象的提示。为此我们可以简单地使用ZIP软件解压*.xlsx。
在此示例中,使用一个工作表和Excel 中最简单的饼图创建一个工作簿。解压*.xlsx,查看/xl/charts/chart1.xml。
饼图示例:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;
public class PieChart {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
Row row;
Cell cell;
for (int r = 0; r < 3; r++) {
row = sheet.createRow(r);
cell = row.createCell(0);
cell.setCellValue("S" + r);
cell = row.createCell(1);
cell.setCellValue(r+1);
}
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 5, 20);
Chart chart = drawing.createChart(anchor);
CTChart ctChart = ((XSSFChart)chart).getCTChart();
CTPlotArea ctPlotArea = ctChart.getPlotArea();
CTPieChart ctPieChart = ctPlotArea.addNewPieChart();
CTBoolean ctBoolean = ctPieChart.addNewVaryColors();
ctBoolean.setVal(true);
CTPieSer ctPieSer = ctPieChart.addNewSer();
ctPieSer.addNewIdx().setVal(0);
CTAxDataSource cttAxDataSource = ctPieSer.addNewCat();
CTStrRef ctStrRef = cttAxDataSource.addNewStrRef();
ctStrRef.setF("Sheet1!$A$1:$A$3");
CTNumDataSource ctNumDataSource = ctPieSer.addNewVal();
CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();
ctNumRef.setF("Sheet1!$B$1:$B$3");
System.out.println(ctChart);
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
}
}
免责声明:使用 Excel 2007 测试,不适用于 Libreoffice 和 Openoffice。
此示例需要 ooxml-schemas-1.3.jar 中提到的所有模式的完整 jar,如 FAQ-N10025。
要使用apache poi 4.1.0 使其正常工作,需要更改以下内容:
...
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
//import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
...
XSSFDrawing drawing = (XSSFDrawing)sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 5, 20);
XSSFChart chart = drawing.createChart(anchor);
...
然后它需要所有模式ooxml-schemas-1.4.jar 的完整 jar。