栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Apache POI在Excel中创建条形图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Apache POI在Excel中创建条形图

对于不了解背景的用户,ApachePOI仅支持ScatterCharts和LineCharts为什么?。原则上描述了如何进行。

就像我说的。首先进行 最简单
的条形图测试。那是一个有两个值的系列。然后,您会看到您完全忘记了代码中的轴。饼图没有轴。这就是为什么它们没有必要的原因。但是,没有轴条形图将无法正常工作。

相反,您可以肆无忌forget地忘记所有被称为“缓存”的东西。

是否需要其他东西取决于尝试和错误。也可以阅读 Office OpenXML建议。可能缺少所有具有

minOccurs

<>“ 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.openxmlformats.schemas.drawingml.x2006.chart.CTChart;import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart;import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarSer;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;import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;import org.openxmlformats.schemas.drawingml.x2006.chart.CTLegend;import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;import org.openxmlformats.schemas.drawingml.x2006.chart.STBarDir;import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;import org.openxmlformats.schemas.drawingml.x2006.chart.STLegendPos;import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;public class BarChart {    public static void main(String[] args) throws Exception {        Workbook wb = new XSSFWorkbook();        Sheet sheet = wb.createSheet("Sheet1");        Row row;        Cell cell;        row = sheet.createRow(0);        row.createCell(0);        row.createCell(1).setCellValue("HEADER 1");        row.createCell(2).setCellValue("HEADER 2");        row.createCell(3).setCellValue("HEADER 3");        for (int r = 1; r < 5; r++) { row = sheet.createRow(r); cell = row.createCell(0); cell.setCellValue("Serie " + r); cell = row.createCell(1); cell.setCellValue(new java.util.Random().nextDouble()); cell = row.createCell(2); cell.setCellValue(new java.util.Random().nextDouble()); cell = row.createCell(3); cell.setCellValue(new java.util.Random().nextDouble());        }        Drawing drawing = sheet.createDrawingPatriarch();        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 8, 20);        Chart chart = drawing.createChart(anchor);        CTChart ctChart = ((XSSFChart)chart).getCTChart();        CTPlotArea ctPlotArea = ctChart.getPlotArea();        CTBarChart ctBarChart = ctPlotArea.addNewBarChart();        CTBoolean ctBoolean = ctBarChart.addNewVaryColors();        ctBoolean.setVal(true);        ctBarChart.addNewBarDir().setVal(STBarDir.COL);        for (int r = 2; r < 6; r++) {CTBarSer ctBarSer = ctBarChart.addNewSer();CTSerTx ctSerTx = ctBarSer.addNewTx();CTStrRef ctStrRef = ctSerTx.addNewStrRef();ctStrRef.setF("Sheet1!$A$" + r);ctBarSer.addNewIdx().setVal(r-2);  CTAxDataSource cttAxDataSource = ctBarSer.addNewCat();ctStrRef = cttAxDataSource.addNewStrRef();ctStrRef.setF("Sheet1!$B$1:$D$1"); CTNumDataSource ctNumDataSource = ctBarSer.addNewVal();CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();ctNumRef.setF("Sheet1!$B$" + r + ":$D$" + r);//at least the border lines in Libreoffice Calc ;-)ctBarSer.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[] {0,0,0});        }        //telling the BarChart that it has axes and giving them Ids        ctBarChart.addNewAxId().setVal(123456);        ctBarChart.addNewAxId().setVal(123457);        //cat axis        CTCatAx ctCatAx = ctPlotArea.addNewCatAx();         ctCatAx.addNewAxId().setVal(123456); //id of the cat axis        CTScaling ctScaling = ctCatAx.addNewScaling();        ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);        ctCatAx.addNewDelete().setVal(false);        ctCatAx.addNewAxPos().setVal(STAxPos.B);        ctCatAx.addNewCrossAx().setVal(123457); //id of the val axis        ctCatAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);        //val axis        CTValAx ctValAx = ctPlotArea.addNewValAx();         ctValAx.addNewAxId().setVal(123457); //id of the val axis        ctScaling = ctValAx.addNewScaling();        ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);        ctValAx.addNewDelete().setVal(false);        ctValAx.addNewAxPos().setVal(STAxPos.L);        ctValAx.addNewCrossAx().setVal(123456); //id of the cat axis        ctValAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);        //legend        CTLegend ctLegend = ctChart.addNewLegend();        ctLegend.addNewLegendPos().setVal(STLegendPos.B);        ctLegend.addNewOverlay().setVal(false);System.out.println(ctChart);        FileOutputStream fileOut = new FileOutputStream("BarChart.xlsx");        wb.write(fileOut);        fileOut.close();    }}

本示例需要FAQ-N10025中

ooxml-schemas-1.3.jar
提到的所有模式的完整jar 。


以上代码一直有效到

apache poi 3.17

以下代码可以使用

apache poi 4.1.0
。它需要所有模式的完整jar
ooxml-schemas-1.4.jar

import java.io.FileOutputStream;import org.apache.poi.ss.usermodel.*;import org.apache.poi.ss.util.*;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.apache.poi.xssf.usermodel.XSSFChart;import org.apache.poi.xssf.usermodel.XSSFDrawing;import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart;import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarSer;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;import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;import org.openxmlformats.schemas.drawingml.x2006.chart.CTLegend;import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;import org.openxmlformats.schemas.drawingml.x2006.chart.STBarDir;import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;import org.openxmlformats.schemas.drawingml.x2006.chart.STLegendPos;import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;public class BarChart {    public static void main(String[] args) throws Exception {        Workbook wb = new XSSFWorkbook();        Sheet sheet = wb.createSheet("Sheet1");        Row row;        Cell cell;        row = sheet.createRow(0);        row.createCell(0);        row.createCell(1).setCellValue("HEADER 1");        row.createCell(2).setCellValue("HEADER 2");        row.createCell(3).setCellValue("HEADER 3");        for (int r = 1; r < 5; r++) { row = sheet.createRow(r); cell = row.createCell(0); cell.setCellValue("Serie " + r); cell = row.createCell(1); cell.setCellValue(new java.util.Random().nextDouble()); cell = row.createCell(2); cell.setCellValue(new java.util.Random().nextDouble()); cell = row.createCell(3); cell.setCellValue(new java.util.Random().nextDouble());        }        XSSFDrawing drawing = (XSSFDrawing)sheet.createDrawingPatriarch();        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 8, 20);        XSSFChart chart = drawing.createChart(anchor);        CTChart ctChart = ((XSSFChart)chart).getCTChart();        CTPlotArea ctPlotArea = ctChart.getPlotArea();        CTBarChart ctBarChart = ctPlotArea.addNewBarChart();        CTBoolean ctBoolean = ctBarChart.addNewVaryColors();        ctBoolean.setVal(true);        ctBarChart.addNewBarDir().setVal(STBarDir.COL);        for (int r = 2; r < 6; r++) {CTBarSer ctBarSer = ctBarChart.addNewSer();CTSerTx ctSerTx = ctBarSer.addNewTx();CTStrRef ctStrRef = ctSerTx.addNewStrRef();ctStrRef.setF("Sheet1!$A$" + r);ctBarSer.addNewIdx().setVal(r-2);  CTAxDataSource cttAxDataSource = ctBarSer.addNewCat();ctStrRef = cttAxDataSource.addNewStrRef();ctStrRef.setF("Sheet1!$B$1:$D$1"); CTNumDataSource ctNumDataSource = ctBarSer.addNewVal();CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();ctNumRef.setF("Sheet1!$B$" + r + ":$D$" + r);//at least the border lines in Libreoffice Calc ;-)ctBarSer.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[] {0,0,0});        }        //telling the BarChart that it has axes and giving them Ids        ctBarChart.addNewAxId().setVal(123456);        ctBarChart.addNewAxId().setVal(123457);        //cat axis        CTCatAx ctCatAx = ctPlotArea.addNewCatAx();         ctCatAx.addNewAxId().setVal(123456); //id of the cat axis        CTScaling ctScaling = ctCatAx.addNewScaling();        ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);        ctCatAx.addNewDelete().setVal(false);        ctCatAx.addNewAxPos().setVal(STAxPos.B);        ctCatAx.addNewCrossAx().setVal(123457); //id of the val axis        ctCatAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);        //val axis        CTValAx ctValAx = ctPlotArea.addNewValAx();         ctValAx.addNewAxId().setVal(123457); //id of the val axis        ctScaling = ctValAx.addNewScaling();        ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);        ctValAx.addNewDelete().setVal(false);        ctValAx.addNewAxPos().setVal(STAxPos.L);        ctValAx.addNewCrossAx().setVal(123456); //id of the cat axis        ctValAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);        //legend        CTLegend ctLegend = ctChart.addNewLegend();        ctLegend.addNewLegendPos().setVal(STLegendPos.B);        ctLegend.addNewOverlay().setVal(false);System.out.println(ctChart);        FileOutputStream fileOut = new FileOutputStream("BarChart.xlsx");        wb.write(fileOut);        fileOut.close();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483287.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号