java poi 3.15 生成pptx并导出

因为pptx页数比较多,所以用的 XMLSlideShow 做的表格和数据填充,需要预先提供一个简易模板,直接上代码了

//业务模块中需要提供简易模板和需要填充的数据

XMLSlideShow xmlSlideShow = new PptUtil().writePPTTemplate(templatePath + templateKey, datas, pptQuery.getRepaymentSourceType(), pptQuery.getMoneyUsageType()); 

//PptUtil()中相关代码片段
InputStream is = new FileInputStream(templatePath); //获取模板文件
XMLSlideShow xmlSlideShow = new XMLSlideShow(is); //将其交给XMLSlideShow 处理成一个没有数据的ppt模板
xmlSlideShow.getSlides()//可以得到模板中的所有页,无法动态生成新的页,这里如果需要根据数据动态生成新的页可采取的方法提前在模板中内置冗余页,在最后将多余的页删除

模板1

java poi 3.15 生成pptx并导出_第1张图片

生成1
java poi 3.15 生成pptx并导出_第2张图片

1、由以上模板页生成以上带数据的pptx页

LinkedHashMap mapPrePledge = JSONObject.parseObject(datas.get("mapPrePledges").toString(), LinkedHashMap.class);
LinkedHashMap mapBorrowers = JSONObject.parseObject(datas.get("mapBorrowers").toString(), LinkedHashMap.class);
if(mapPrePledge != null && mapPrePledge.size() > 0){
    for(String key : mapPrePledge.keySet()){
        JSONObject object = mapPrePledge.get(key);
        if(object == null){
            continue;
        }
        JSONObject borrowersJson = mapBorrowers.get(key);
        slide = xmlSlideShow.getSlides().get(i);
        if(!getJSONObjectValue(borrowersJson, "SubjectProperty", "").equals("1")){
            willRemoveRows = new LinkedList(Arrays.asList(new Integer[]{2,8}));
        }
        addTableByCTTable(slide,2, key, willRemoveRows,
        getSubjectPropertyStr(getJSONObjectValue(borrowersJson, "SubjectProperty", "")),
        getJSONObjectValue(borrowersJson, "SubjectName", ""),
        getJSONObjectValue(borrowersJson, "CertificateNum", ""),
        getSubjectPropertyStr(getJSONObjectValue(object, "SubjectProperty", "")),
        getJSONObjectValue(object, "SubjectName", ""),
        getJSONObjectValue(object, "StockCode", ""),
        getJSONObjectValue(object, "StockShortName", ""),
        (object.containsKey("PrepledgedStockNum") && object.getString("PrepledgedStockNum") != null) ?
        String.valueOf(new BigDecimal(object.getString("PrepledgedStockNum")).setScale(4, RoundingMode.HALF_UP).doubleValue()) :"",
        getJSONObjectValue(object, "EmpowermentJustice", "").equals("1") ? "是" : "否",
        getJSONObjectValue(object, "Remark", ""));
        i++;
    }
}
private void addTableByCTTable(Slide slide, int shapeIndex, String rightTitle, LinkedList willRemoveRows, String ...obj){
    if(slide == null || obj.length <= 0){
        return;
    }
    if(shapeIndex == 2 && !StringUtils.isBlank(rightTitle)){
        XSLFAutoShape shape0 = (XSLFAutoShape) slide.getShapes().get(2);
        shape0.setText(rightTitle);
    }
    XSLFTable table = (XSLFTable) slide.getShapes().get(1);
    for(int i = 0; i< obj.length; i++){
        XSLFTableCell cell = table.getCell(i,1);
        if(StringUtils.isBlank(obj[i])){
            cell.setText("");
        }else{
            cell.setText(obj[i]);
        }
    }
    setTableStyle(table); //设置表格样式的方法,此方法必须放在最后,否则设置的格式无法生效
    removeRows(table.getCTTable(), willRemoveRows); //删除表格中多余行的方法,使用的是table.getCTTable(),table.getRows().remove()无法使用

}
private void setTableStyle(XSLFTable table){
    if(table == null){
        return;
    }
    for(XSLFTableRow row : table.getRows()){
        for(XSLFTableCell cell : row.getCells()){
            cell.setBorderColor(TableCell.BorderEdge.top, new Color(187, 224, 227));
            cell.setBorderColor(TableCell.BorderEdge.bottom, new Color(187, 224, 227));
            cell.setBorderColor(TableCell.BorderEdge.left, new Color(187, 224, 227));
            cell.setBorderColor(TableCell.BorderEdge.right, new Color(187, 224, 227));
            List paragraphs = cell.getTextParagraphs();
            for(XSLFTextParagraph paragraph : paragraphs){
                List textRuns = paragraph.getTextRuns();
                for(XSLFTextRun textRun : textRuns){
                    textRun.setFontSize(14d);
                    textRun.setFontFamily("楷体_GB2312 (正文)");
                }
            }
        }
    }

}
private void removeRows(CTTable ctTable, LinkedList willRemoveRows){
    if(willRemoveRows.size() <= 0){
        return;
    }
    ctTable.removeTr(willRemoveRows.get(0));
    willRemoveRows.remove(0);
    for(int i = 0; i< willRemoveRows.size(); i++){
        willRemoveRows.set(i, willRemoveRows.get(i) - 1);
    }
    removeRows(ctTable, willRemoveRows);
}

模板2
java poi 3.15 生成pptx并导出_第3张图片

生成2
java poi 3.15 生成pptx并导出_第4张图片

2、由以上模板页生成以上带数据的pptx页
 

List shareholdInfos = JSONObject.parseArray(object.get("shareholdInfo").toString(), JSONObject.class);
if(shareholdInfos == null || shareholdInfos.size() <= 0){
    willRemoves.add(i);
    continue;
}
addTable3(slide, 2, getJSONObjectValue(object,"getTime",""), shareholdInfos, "shareholderName", "shareholdingRatio","shareholdingNum");
private void addTable3(Slide slide, int shapeIndex, String rightTitle, Listdatas, String ...obj){
    if(slide == null || datas == null || datas.size() <= 0 ||obj.length <= 0){
        return;
    }
    if(shapeIndex == 2 && !StringUtils.isBlank(rightTitle)){
        XSLFAutoShape shape0 = (XSLFAutoShape) slide.getShapes().get(1);
        shape0.setText(rightTitle);
    }
    double shareholdingRatio = 0;
    long shareholdingNum = 0;
    XSLFTable table = (XSLFTable) slide.getShapes().get(shapeIndex);
    for(int i = 0; i< datas.size(); i++){
        XSLFTableRow row = table.addRow();
        for(int j = 0; j< obj.length; j++){
            XSLFTableCell cell = row.addCell();
            String value = datas.get(i).getString(obj[j]);
            if(j == 0){
                cell.setText(value);
            }
            if(j == 1){
                BigDecimal bigDecimalAmount = new BigDecimal(value).setScale(2, RoundingMode.HALF_UP);
                cell.setText(String.valueOf(bigDecimalAmount.doubleValue()));
                shareholdingRatio += Double.valueOf(datas.get(i).getString(obj[j]));
            }
            if(j == 2){
                cell.setText(value);
                shareholdingNum += Long.valueOf(datas.get(i).getString(obj[j]));
            }
        }
        if(i > 9){
            break;
        }
    }
    XSLFTableRow row = table.addRow();
    XSLFTableCell cell_1 = row.addCell();
    cell_1.setText("合计");
    XSLFTableCell cell_2 = row.addCell();
    BigDecimal bigDecimalAmount = new BigDecimal(Double.toString(shareholdingRatio)).setScale(2, RoundingMode.HALF_UP);
    cell_2.setText(String.valueOf(bigDecimalAmount.doubleValue()));
    XSLFTableCell cell_3 = row.addCell();
    cell_3.setText(String.valueOf(shareholdingNum));
    setTableStyle(table);  //此方法必须放在最后,否则设置的格式无法生效

}

模板3

java poi 3.15 生成pptx并导出_第5张图片

生成3
java poi 3.15 生成pptx并导出_第6张图片

3、由以上模板页生成以上带图形带箭头指向的pptx页

private String[][] params = {
    {"230,100,130,120,0"}, //1
    {"200,200,130,120,0",  //2
    "240,125,350,60,7",
    "530,200,130,120,0",
    "240,335,350,60,8"
    },
    {"200,100,130,120,0", //3
    "360,135,150,50,1",
    "530,100,130,120,0",
    "570,230,50,150,4",
    "530,400,130,120,0",
    "180,330,200,100,5"
    },
    {"200,100,130,120,0", //4
    "360,135,150,50,1",
    "530,100,130,120,0",
    "570,230,50,150,4",
    "530,400,130,120,0",
    "360,430,150,50,2",
    "200,400,130,120,0",
    "240,230,50,150,3"
    },
    {"100,100,130,120,0",//5
    "250,130,100,50,1",
    "370,100,130,120,0",
    "530,130,100,50,1",
    "650,100,130,120,0",
    "680,250,50,100,4",
    "650,400,130,120,0",
    "530,440,100,50,2",
    "370,400,130,120,0",
    "70,330,200,100,5"
    },
    {"100,100,130,120,0",//6
    "250,130,100,50,1",
    "370,100,130,120,0",
    "530,130,100,50,1",
    "650,100,130,120,0",
    "680,250,50,100,4",
    "650,400,130,120,0",
    "530,440,100,50,2",
    "370,400,130,120,0",
    "250,440,100,50,2",
    "100,400,130,120,0",
    "130,250,50,100,3",
    },
    {"50,100,130,120,0",//7
    "190,135,50,50,1",
    "250,100,130,120,0",
    "390,135,50,50,1",
    "450,100,130,120,0",
    "590,135,50,50,1",
    "650,100,130,120,0",
    "690,250,50,100,4",
    "650,400,130,120,0",
    "590,435,50,50,2",
    "450,400,130,120,0",
    "390,435,50,50,2",
    "250,400,130,120,0",
    "40,320,200,100,5"
    },
    {"50,100,130,120,0",//8
    "190,135,50,50,1",
    "250,100,130,120,0",
    "390,135,50,50,1",
    "450,100,130,120,0",
    "590,135,50,50,1",
    "650,100,130,120,0",
    "690,250,50,100,4",
    "650,400,130,120,0",
    "590,435,50,50,2",
    "450,400,130,120,0",
    "390,435,50,50,2",
    "250,400,130,120,0",
    "190,435,50,50,2",
    "50,400,130,120,0",
    "80,250,50,100,3",
    },
    {"50,100,130,120,0",//9
    "185,150,15,15,1",
    "205,100,130,120,0",
    "340,150,15,15,1",
    "360,100,130,120,0",
    "495,150,15,15,1",
    "515,100,130,120,0",
    "650,150,15,15,1",
    "670,100,130,120,0",
    "700,250,50,100,4",
    "670,400,130,120,0",
    "650,450,15,15,2",
    "515,400,130,120,0",
    "495,450,15,15,2",
    "360,400,130,120,0",
    "340,450,15,15,2",
    "205,400,130,120,0",
    "30,320,200,100,5"
    },
    {"50,100,130,120,0",//10
    "185,150,15,15,1",
    "205,100,130,120,0",
    "340,150,15,15,1",
    "360,100,130,120,0",
    "495,150,15,15,1",
    "515,100,130,120,0",
    "650,150,15,15,1",
    "670,100,130,120,0",
    "700,250,50,100,4",
    "670,400,130,120,0",
    "650,450,15,15,2",
    "515,400,130,120,0",
    "495,450,15,15,2",
    "360,400,130,120,0",
    "340,450,15,15,2",
    "205,400,130,120,0",
    "185,450,15,15,2",
    "50,400,130,120,0",
    "70,250,50,100,3"
    }
};  //提前定义好的ppt页面上从 1 -10 个图形每一个的大小位置和类型
LinkedHashMap>mapFundsPaths = JSONObject.parseObject(datas.get("mapFundsPaths").toString(), LinkedHashMap.class);
if(mapFundsPaths != null && mapFundsPaths.size() > 0){
    for(String key : mapFundsPaths.keySet()){
        if(mapFundsPaths.get(key) == null || mapFundsPaths.get(key).size() <= 0){
            willRemoves.add(i);
            continue;
        }
        slide = xmlSlideShow.getSlides().get(i);
        addShape(slide, 2,key, getFundsPaths(mapFundsPaths.get(key)));
        i++;
    }
}
private void addShape(Slide slide, int shapeIndex, String rightTitle, List texts){
    if(slide == null || texts == null || texts.size() > 10){
        return;
    }
    if(shapeIndex == 2 && !StringUtils.isBlank(rightTitle)){
        XSLFAutoShape shape0 = (XSLFAutoShape) slide.getShapes().get(1);
        shape0.setText(rightTitle);
    }
    String paramStr[] = params[texts.size() - 1];
    for(int index = 0; index < paramStr.length; index++){
        String[] datas = StringUtils.split(paramStr[index],",");
        XSLFAutoShape shape = (XSLFAutoShape) slide.getShapes().get(index + 2);
        if(Integer.valueOf(datas[4]) == 0){
            shape.setShapeType(ShapeType.ROUND_RECT);//矩形
        }
        if(Integer.valueOf(datas[4]) == 1){
            shape.setShapeType(ShapeType.RIGHT_ARROW);//右箭头
        }
        if(Integer.valueOf(datas[4]) == 2){
            shape.setShapeType(ShapeType.LEFT_ARROW);//左箭头
        }
        if(Integer.valueOf(datas[4]) == 3){
            shape.setShapeType(ShapeType.UP_ARROW);//向上箭头
        }
        if(Integer.valueOf(datas[4]) == 4){
            shape.setShapeType(ShapeType.DOWN_ARROW);//向下箭头
        }
        if(Integer.valueOf(datas[4]) == 5){
            shape.setRotation(-90);
            shape.setShapeType(ShapeType.BENT_ARROW);//圆角右箭头
        }
        if(Integer.valueOf(datas[4]) == 6){
            shape.setShapeType(ShapeType.LEFT_RIGHT_ARROW);//左右互指箭头
        }
        if(Integer.valueOf(datas[4]) == 7){
            shape.setShapeType(ShapeType.CURVED_DOWN_ARROW);//上弧形右箭头
        }
        if(Integer.valueOf(datas[4]) == 8){
            shape.setRotation(-180);
            shape.setShapeType(ShapeType.CURVED_DOWN_ARROW);//下弧形左箭头
        }
        shape.setAnchor(new Rectangle(Integer.valueOf(datas[0]), Integer.valueOf(datas[1]), Integer.valueOf(datas[2]), Integer.valueOf(datas[3])));
        if(index % 2 == 0){
            String[] text = StringUtils.split(texts.get(index/2),"|");
            for(int i = 0; i< text.length; i++){
                if(i == 0){
                    XSLFTextRun textRun = shape.setText(text[i]);
                    textRun.setFontSize(9D);
                    textRun.setBold(false);
                }
                if(i > 0 && i< text.length){
                    shape.appendText(text[i],true);
                }
            }
        }
        shape.setFillColor(new Color(187, 224, 227));
    }
}

你可能感兴趣的:(ppt,pptx导出,java,poi,3.15,pptx导出)