亲宝软件园·资讯

展开

Java生成word文档的示例详解

步尔斯特 人气:0

目标

依赖

<!--  poi工具类-->
    <dependency>
        <groupId>com.deepoove</groupId>
        <artifactId>poi-tl</artifactId>
        <version>1.12.0</version>
    </dependency>

模版

实体

实体类需要和模版内的动态字段对应

代码

 @GetMapping(value = "/all")
    @SneakyThrows
    public void table(All all, HttpServletResponse response) {

        /* 假数据 */
        all.setCode("2022-12-ABC");
        all.setBuyer("步尔斯特航天有限公司");
        String[][] strings = new String[100][5];
        for (int i = 0; i < 100; i++) {
            strings[i] = new String[]{i + "", "黄金", "100", "人民币", "125"};
        }
        all.setTableArray(strings);

        // 模版路径
        String wordPath = "/Users/issavior/java/java/seckill-redis/test/src/main/resources/";
        String modelName = "all.docx";

        String c = "购买方(Buyer): \n" +
                "名称(Name):" + all.getBuyer() + "\n" +
                "地址(Address):杭州市余杭区大东路100号\n" +
                "电话(Tel.):021-20855588\n" +
                "\n" +
                "销售方(Seller):\n" +
                "名称(Name):杭州火箭制造有限公司\n" +
                "地址(Address):杭州市余杭区大东路919号\n" +
                "电话(Tel.):021-20855553\n" +
                "开户行(Bank):中国农业银行杭州支行\n" +
                "账号(Account No.):033270-00873333567";

        // 手绘表格
        // 表头
        RowRenderData row0 = Rows.of(c, "", "", "", "").textBold().create();
        RowRenderData row1 = Rows.of("序号", "名称", "数量", "货币", "价格").center().create();

        int length = 0;
        if (all.getTableArray() != null) {
            length = all.getTableArray().length;
        }
        // 表格数据 加上2行表头 再加上最后一行总价
        RowRenderData[] rowRenderData = new RowRenderData[length + 3];
        rowRenderData[0] = row0;
        rowRenderData[1] = row1;
        // 计算价钱
        BigDecimal totalPrice = new BigDecimal("0");
        for (int i = 0; i < length; i++) {
            rowRenderData[i + 2] = Rows.of(all.getTableArray()[i]).center().create();
            String s = all.getTableArray()[i][4];
            BigDecimal bigDecimal = new BigDecimal(s);
            totalPrice = totalPrice.add(bigDecimal);
        }

        RowRenderData row4 = Rows.of("总价:" + totalPrice, "", "", "小写", totalPrice.toString()).center().create();
        rowRenderData[rowRenderData.length - 1] = row4;
        // 表格合并,根据坐标
        MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(0, 0), MergeCellRule.Grid.of(0, 4)).
//                map(MergeCellRule.Grid.of(0, 1), MergeCellRule.Grid.of(1, 1)).
//                map(MergeCellRule.Grid.of(0, 2), MergeCellRule.Grid.of(0, 3)).
//                map(MergeCellRule.Grid.of(0, 4), MergeCellRule.Grid.of(1, 4)).
                map(MergeCellRule.Grid.of(rowRenderData.length - 1, 0), MergeCellRule.Grid.of(rowRenderData.length - 1, 2)).
                build();

        TableRenderData table = Tables.of(rowRenderData).mergeRule(rule).create();
        // 数据封装
        all.setTableRenderData(table);
        // 传入模板模板地址+信息数据
        XWPFTemplate template = XWPFTemplate.compile(wordPath + modelName).render(all);
        // 指定下载的文件名--设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("发票.docx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        try {
            OutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            template.write(out);
            bos.flush();
            out.flush();
            template.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

加载全部内容

相关教程
猜你喜欢
用户评论