首页 > 解决方案 > Apache POI Excel 图表在 Mac 上消失,但在 Windows/Linux 上没有

问题描述

我使用Openxml.CTChartApache POI 绘制了一个 Excel 条形图。该图表在 Linux(libreOffice) 和 Windows(MSOffice) 下运行良好。但是当文件在 MacOs/IOS 上打开时图表消失了,所有其他表格仍然可以正常工作。

代码很长,我放一部分。如果您需要更多信息,请告诉我。

XSSFDrawing drawing = ((XSSFSheet) sheet).createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, anchorRow, 15, anchorRow + 6 + 2 * dataSource.getTableNumber());
        XSSFChart chart = drawing.createChart(anchor);
        chart.getCTChartSpace().addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte) 0xff,
                (byte) 0xff, (byte) 0xff});
        // chart
        CTChart ctChart = chart.getCTChart();
 // plotArea
        CTPlotArea ctPlotArea = ctChart.getPlotArea();
        ctPlotArea.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte) 0xb3,
                (byte) 0xb3, (byte) 0xb3});
        // barChart
        CTBarChart ctBarChart = ctPlotArea.addNewBarChart();
        // barChart parameters
        ctBarChart.addNewGrouping().setVal(STBarGrouping.STACKED);
        ctBarChart.addNewBarDir().setVal(STBarDir.BAR);
        ctBarChart.addNewVaryColors().setVal(false);

        // ser (for each different component of total price)
        for (int serIndex = 0; serIndex < 4; serIndex++) {
            int columnIndex = serIndex + 1;
            CTBarSer ctBarSer = ctBarChart.addNewSer();
            // ser parameters
            ctBarSer.addNewIdx().setVal(serIndex);
            ctBarSer.addNewOrder().setVal(serIndex);
            // tx
            CTSerTx ctSerTx = ctBarSer.addNewTx();
            CTStrRef ctStrRef = ctSerTx.addNewStrRef();
        }
//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);
        ctCatAx.addNewMajorTickMark().setVal(OUT);
        ctCatAx.addNewMinorTickMark().setVal(NONE);
        ctCatAx.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte) 0xb3,
                (byte) 0xb3, (byte) 0xb3});

        //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);

        ctValAx.addNewMajorGridlines().addNewSpPr()
           .addNewLn().addNewSolidFill().addNewSrgbClr()
           .setVal(new byte[]{(byte) 0xb3,(byte) 0xb3, (byte) 0xb3});
        ctValAx.addNewMajorTickMark().setVal(OUT);
        ctValAx.addNewMinorTickMark().setVal(NONE);
        ctValAx.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte) 0xb3,
                (byte) 0xb3, (byte) 0xb3});


        //legend
        CTLegend ctLegend = ctChart.addNewLegend();
        ctLegend.addNewLegendPos().setVal(STLegendPos.R);
        ctLegend.addNewOverlay().setVal(false);
}



public byte[] generateExcel() {
        ByteArrayOutputStream bos = null;
        try {
            bos = new ByteArrayOutputStream();
            workbook.write(bos);
            bos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(bos);
        }
        return bos.toByteArray();
    }

控制器:

@GetMapping("/generateXlsxReport")
    public ResponseEntity<byte[]> generateReport() {

        byte[] excel = svc.generateExcel();
        HttpHeaders header = new HttpHeaders();
        header.setContentType(new MediaType("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
        header.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=my_file.xlsx");
        header.setContentLength(excel.length);

        return ResponseEntity.ok().headers(header).body(excel);
    }

我应该怎么做才能使图表在 Mac 上兼容?

标签: javaexcelspringapache-poi

解决方案


推荐阅读