Java poi导出word表格

@GetMapping(value = "/getPartyOrganCensus")
    public void  getPartyOrganCensus(HttpServletRequest request, HttpServletResponse response) throws IOException{

                List<PartyOrganCensusVO> list=commonPartyOrganMapperExt.getPartyOrganCensus();
        PartyOrganCensusVO total=new PartyOrganCensusVO();
        total.setRegionName("合计");
        total.setTotalPartyMember(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getTotalPartyMember)));
        total.setTotalPartyOrgan(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getTotalPartyOrgan)));
        total.setPartyCommittee(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getPartyCommittee)));
        total.setPartyGeneralBranch(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getPartyGeneralBranch)));
        total.setPartyBranch(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getPartyBranch)));
        list.add(total);

        String tempPath = "static/exportTemplates/党组织情况汇总表.docx";

        String outputUrl = "static/exportTemplates/test.docx";

        createWord(tempPath,outputUrl,list);
        String fileName="党组织情况汇总表.doc";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-Disposition","attachment; filename=" +fileName);
        response.setContentType("application/msword");// 定义输出类型
        response.setStatus(200);
        OutputStream out=response.getOutputStream();
        byte[] buf = new byte[2 * 1024];
        int len;
        FileInputStream in = new FileInputStream(outputUrl);
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();

    }

 public void createWord(String inputUrl, String outputUrl,List<PartyOrganCensusVO> tableList) {
        try {
            XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
            List<XWPFTable> tables = document.getTables();
            for (int i = 0; i < tables.size(); i++) {
                //只处理行数大于等于2的表格,且不循环表头
                XWPFTable table = tables.get(i);
                if(table.getRows().size()>1){
                    insertTable(table, tableList);
                }
            }
            File file = new File(outputUrl);
            FileOutputStream stream = new FileOutputStream(file);
            document.write(stream);
            stream.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void insertTable(XWPFTable table, List<PartyOrganCensusVO> tableList){
        XWPFTableRow oldrows = table.getRows().get(2);
        List<XWPFTableCell> cellList = oldrows.getTableCells();
        CTTrPr trpr=oldrows.getCtRow().getTrPr();
        //创建行,根据需要插入的数据添加新行,不处理表头
        for(int i = 0; i < tableList.size(); i++){
            XWPFTableRow newRow = table.insertNewTableRow(i+3);
            //复制行属性
            newRow.getCtRow().setTrPr(trpr);
            if (null == cellList) {
                return;
            }
            //复制列及其属性和内容
            for (XWPFTableCell sourceCell : cellList) {
                XWPFTableCell newCell=newRow.createCell();
                //列属性
                newCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
                String text="";

                int j=cellList.indexOf(sourceCell);
                if(j == 0)
                    text=tableList.get(i).getRegionName();
                else if(j==2)
                    text=tableList.get(i).getTotalPartyMember().toString();
                else if(j==3)
                    text=tableList.get(i).getTotalPartyOrgan().toString();
                else if(j==4)
                    text=tableList.get(i).getPartyCommittee().toString();
                else if(j==5)
                    text=tableList.get(i).getPartyGeneralBranch().toString();
                else if(j==6)
                    text=tableList.get(i).getPartyBranch().toString();

                //段落属性
                if(sourceCell.getParagraphs()!=null&&sourceCell.getParagraphs().size()>0){
                    newCell.getParagraphs().get(0).getCTP().setPPr(sourceCell.getParagraphs().get(0).getCTP().getPPr());
                    newCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //垂直居中
                    if(sourceCell.getParagraphs().get(0).getRuns()!=null&&sourceCell.getParagraphs().get(0).getRuns().size()>0){
                        XWPFRun cellR = newCell.getParagraphs().get(0).createRun();
                        cellR.setText(text);
                        cellR.setBold(sourceCell.getParagraphs().get(0).getRuns().get(0).isBold());
                        cellR.setSubscript(VerticalAlign.SUBSCRIPT);
                    }else{
                        newCell.setText(text);
                    }
                }else{
                    newCell.setText(text);
                }
            }
        }
        table.removeRow(2);
    }

vue调用

getPartyOrganCensus() {
                axios({
                        method: "get",
                        url: "/api/commonPartyOrgan/getPartyOrganCensus",
                        responseType: "blob"
                    })
                    .then(res => {
                        const fileName=["党组织情况汇总表","doc"];
                        const _res = res.data;
                        let blob = new Blob([_res]);
                        let downloadName =
                            decodeURIComponent(fileName[0]) + "." + fileName[1]; //下载后文件名
                        if ("msSaveOrOpenBlob" in navigator) {
                            window.navigator.msSaveOrOpenBlob(blob, downloadName);
                        } else {
                            let downloadElement = document.createElement("a");
                            let href = window.URL.createObjectURL(blob); //创建下载的链接
                            downloadElement.href = href;
                            downloadElement.download = downloadName;
                            downloadElement.click(); //点击下载
                            window.URL.revokeObjectURL(href); //释放掉blob对象
                        }
                    })
                    .catch(error => {
                        console.log(error);
                        });
            },

 

posted @ 2020-05-29 11:09  艺洁  阅读(1469)  评论(0编辑  收藏  举报