导出的相关方法

该博客介绍了如何使用Apache POI库在Java中导出Word文档。通过添加依赖,设置模板,填充数据并调整字体样式,实现了从模板生成包含特定内容的Word文件。主要涉及的技术包括XWPFTemplate、XWPFDocument和XWPFParagraph等。
摘要由CSDN通过智能技术生成

导出成word文档

准备好jar包

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.3.1</version>
</dependency>
public class WordUtil {

    private Logger logger = LoggerFactory.getLogger(WordUtil.class);

    public static String exportToWordUtil(HttpServletResponse response, String fileName, String templateName, Map<String, Object> dataMap) {
        OutputStream out = null;
        XWPFTemplate template = null;
        try {
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            //这里的ExcelUtil.class应该只是提供一个class对象吧?然后才能调用getResourceAsStream方法
            InputStream is = Object.class.getResourceAsStream("/template/" + templateName);
            //这一步应该是把准备好的map内容,转成XWPFTemplate对象
            template = XWPFTemplate.compile(is).render(dataMap);
            XWPFDocument xwpfDocument = template.getXWPFDocument();
            List<XWPFTable> tables = xwpfDocument.getTables();
            //设置字体和大小
            List<XWPFParagraph> paragraphs = getTableParagraphs(xwpfDocument, tables.get(0));
            //1.取得某一个表格的全部段落
            for (XWPFParagraph paragraph : paragraphs) {
                //获取XWPFRun
                XWPFRun xwpfRun = getOrAddParagraphFirstRun(paragraph, false, false);
                //设置字体大小内容
                String font = "仿宋";
                String fontSize = "22";
                setParagraphRunFontInfo(paragraph, xwpfRun, null, font, fontSize);
            }
            out = response.getOutputStream();
            template.write(out);
        } catch (Exception e) {

        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {

            }
            try {
                if (template != null) {
                    template.close();
                }
            } catch (IOException e) {

            }
        }
        return null;
    }


    //取得某一个表格的全部段落
    public static List<XWPFParagraph> getTableParagraphs(XWPFDocument document, XWPFTable table) {
        List<XWPFParagraph> paragraphs = new ArrayList<>();
        //列表内段落
        List<XWPFTableRow> rows = table.getRows();
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> cells = row.getTableCells();
            for (XWPFTableCell cell : cells) {
                paragraphs.addAll(cell.getParagraphs());
            }
        }
        return paragraphs;
    }

    private static XWPFRun getOrAddParagraphFirstRun(XWPFParagraph p, boolean isInsert, boolean isNewLine) {
        XWPFRun pRun = null;
        if (isInsert) {
            pRun = p.createRun();
        } else {
            if (p.getRuns() != null && p.getRuns().size() > 0) {
                pRun = p.getRuns().get(0);
            }
        }
        if (isNewLine) {
            pRun.addBreak();
        }
        return pRun;
    }

    private static void setParagraphRunFontInfo(XWPFParagraph p, XWPFRun pRun, String content, String fontFamily, String fontSize) {
        CTRPr pRpr = getRunCTRPr(p, pRun);
        if (StringUtils.isNoneBlank(content)) {
            pRun.setText(content);
        }
        //设置字体
        CTFonts fonts = pRpr.isSetRFonts() ? (CTFonts) pRpr.getRFonts() : pRpr.addNewRFonts();
        fonts.setAscii(fontFamily);
        fonts.setEastAsia(fontFamily);
        fonts.setHAnsi(fontFamily);

        //设置字体大小
        CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz();
        sz.setVal(new BigInteger(fontSize));

        CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr.addNewSzCs();
        szCs.setVal(new BigInteger(fontSize));
    }

    private static CTRPr getRunCTRPr(XWPFParagraph p, XWPFRun pRun) {
        CTRPr pRpr = null;
        if (pRun.getCTR() != null) {
            pRpr = pRun.getCTR().getRPr();
            if (pRpr == null) {
                pRpr = pRun.getCTR().addNewRPr();
            }
        } else {
            pRpr = p.getCTP().addNewR().addNewRPr();
        }
        return pRpr;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值