springboot使用POI创建world实现下载


```java
         ---------------------------------后端获取response-------------------------------------
         ServletRequestAttributes servletRequestAttributes =
            (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        response.setContentType("application/msword;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".docx");
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        ---------------------------------创建文件,设置标题段落start-------------------------------------
        //方式三:
        XWPFDocument document = new XWPFDocument();
        //添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText("标题文字");
        titleRun.setFontSize(18);
        titleRun.setBold(true);
       -----------段落start--------------------
        XWPFParagraph firstParagraph = document.createParagraph();
        XWPFRun run = firstParagraph.createRun();
        run.setText("   我是第一段文字");
        run.addCarriageReturn();//文件折行
        run.setFontSize(16);//字体
        run.setColor("000000");
        //设置段落背景颜色
        CTShd cd = run2.getCTR().addNewRPr().addNewShd();
        cd.setVal(STShd.CLEAR);
		 -----------表格start--------------------
        XWPFTable table = document.createTable(3, 2);
        WordExportUtil.setTableStyle(table);
        //表格第一行
        XWPFTableRow headRow = table.getRow(0);
        WordExportUtil.setRowProperty(headRow);
        XWPFTableCell headCell0 = headRow.getCell(0);//第一个单元格
        XWPFTableCell headCell1 = headRow.getCell(1);//第二个单元格
        List<XWPFParagraph> list = rowThree0.getParagraphs();
        XWPFParagraph pIO = list.get(0);
        XWPFRun rIO = pIO.createRun();
        rIO.setFontFamily("宋体");
        rIO.setFontSize(16);
        rIO.setText("第一行第一个单元格文字");
    
        XWPFParagraph pIO = list.get(1);
        XWPFRun rIO = pIO.createRun();
        rIO.setFontFamily("宋体");
        rIO.setFontSize(16);
        rIO.setText("第一行第二个单元格文字");
 //-----------设置页眉--------------------
        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
        //添加页眉
        CTP ctpHeader = CTP.Factory.newInstance();
        CTR ctrHeader = ctpHeader.addNewR();
        CTText ctHeader = ctrHeader.addNewT();
        String headerText = "xxx有限公司";
        ctHeader.setStringValue(headerText);
        XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
        //设置为右对齐
        headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
        XWPFParagraph[] parsHeader = new XWPFParagraph[1];
        parsHeader[0] = headerParagraph;
        policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
        //添加页脚
        CTP ctpFooter = CTP.Factory.newInstance();
        CTR ctrFooter = ctpFooter.addNewR();
        CTText ctFooter = ctrFooter.addNewT();
        ctFooter.setStringValue("xxx有限公司");
        XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
        headerParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFParagraph[] parsFooter = new XWPFParagraph[1];
        parsFooter[0] = footerParagraph;
        policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);

        //写入到response中实现下载
        OutputStream outputStream = response.getOutputStream();
        document.write(outputStream);
        outputStream.flush();
        outputStream.close();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值