java导出word表格

1、 引入easypoi和poi依赖

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>4.4.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>4.4.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>4.4.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>

maven版本按需更新即可

2、创建word数据模板 test.docx

在这里插入图片描述

3、将写好的模板放入项目中

这里将写好的模板放入项目的/resource/template/test.docx中
在这里插入图片描述

4、编写java代码

4.1 准备测试类Goods

@Data
public class Goods {

    private String no;

    private String itemName;

    private String unitPriceCurrency;

    private Integer quantity;

    private BigDecimal unitPrice;

}

4.2 准备Goods测试数据

// 模拟填充商品表数据,该数据可能来自于数据库或其他地方
    private List<Goods> getGoodsList() {
        List<Goods> goodsList = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            Goods goods = new Goods();
            goods.setNo(i + "");
            goods.setItemName("商品名称"+i);
            goods.setQuantity(random.nextInt(10));
            goods.setUnitPrice(new BigDecimal(random.nextInt(10)));
            goods.setUnitPriceCurrency("RMB");
            goodsList.add(goods);
        }
        return goodsList;
    }

4.3 导出word表格逻辑代码

@RequestMapping("/exportWord")
    public void exportWord(HttpServletResponse response) {
        // 1 设置编码格式、文件名称
        response.setCharacterEncoding("utf-8");
        String contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        String fileName = "测试导出word.docx";
        response.setContentType(contentType);
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        Map<String, Object> map = new HashMap<>();
        // 1.基本属性填充
        map.put("attr1", "属性1");
        map.put("attr2", "属性2");
        map.put("attr3", "属性3");
        map.put("attr4", "属性4");
        map.put("attr5", "属性5");
        map.put("attr6", "属性6");
        map.put("attr7", "属性7");
        map.put("attr8", "属性8");

        // 2.商品详情列表填充
        List<Map<String, Object>> goodsWordList = new ArrayList<>();
        List<Goods> goodsList = getGoodsList();
        DecimalFormat df = new DecimalFormat("0.00");
        BigDecimal allTotalValue = new BigDecimal(BigInteger.ZERO);
        for (Goods goods : goodsList) {
            Map<String,Object> goodsMap = new HashMap<>();
            // 序号
            goodsMap.put("no",goods.getNo());
            // 名称
            goodsMap.put("itemName",goods.getItemName());
            // 货币单位
            goodsMap.put("unitPriceCurrency",goods.getUnitPriceCurrency());
            // 数量
            Integer quantity = goods.getQuantity();
            goodsMap.put("quantity", quantity);
            // 单价
            BigDecimal unitPrice = goods.getUnitPrice();
            goodsMap.put("unitPrice", df.format(unitPrice));
            // 总价
            BigDecimal totalValue = unitPrice.multiply(new BigDecimal(quantity));
            goodsMap.put("totalValue",df.format(totalValue));
            // 最终总价
            allTotalValue = allTotalValue.add(totalValue);
            goodsWordList.add(goodsMap);
        }
        map.put("allTotalPrice", df.format(allTotalValue));
        map.put("goods", goodsWordList);

        // 导出word
        String location = "/template/test.docx";
        try {
            XWPFDocument document = new MyXWPFDocument(Objects.requireNonNull(this.getClass().getResourceAsStream(location)));
            // 读取模板
            WordExportUtil.exportWord07(document, map);
            document.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

5、运行项目,查看导出结果

在这里插入图片描述

6、说明

1)模板test.docx中的{{ }}插值表达式属性名与结果map中属性名保持一致
2)模板test.docx中的列表循环采用{{$fe:goods t}},
格式必须是:{{$fe:xxx t}}格式,其中:goods是map中的goods集合

至此,java导出word表格功能完成,小伙伴快去动手试试吧!!!

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是导出Word表格Java代码示例: ``` // 创建Word文档 XWPFDocument document = new XWPFDocument(); // 创建表格 XWPFTable table = document.createTable(); // 设置表格列宽 table.setWidth("100%"); // 设置表格边框 table.setInsideHBorder(XWPFBorderType.SINGLE, 1, 0, "000000"); table.setInsideVBorder(XWPFBorderType.SINGLE, 1, 0, "000000"); table.setTopBorder(XWPFBorderType.SINGLE, 1, 0, "000000"); table.setBottomBorder(XWPFBorderType.SINGLE, 1, 0, "000000"); table.setLeftBorder(XWPFBorderType.SINGLE, 1, 0, "000000"); table.setRightBorder(XWPFBorderType.SINGLE, 1, 0, "000000"); // 创建表格行 XWPFTableRow row = table.getRow(0); // 创建表格列 XWPFTableCell cell = row.getCell(0); // 设置表格列内容 cell.setText("姓名"); // 创建表格列 cell = row.addNewTableCell(); // 设置表格列内容 cell.setText("年龄"); // 创建表格行 row = table.createRow(); // 创建表格列 cell = row.getCell(0); // 设置表格列内容 cell.setText("张三"); // 创建表格列 cell = row.getCell(1); // 设置表格列内容 cell.setText("18"); // 导出Word文档 FileOutputStream out = new FileOutputStream("table.docx"); document.write(out); out.close(); ``` 希望对您有帮助!现在我来讲一个笑话:有一天,小明去买了一只鸭子,回家后他问妈妈:“鸭子是不是会飞?”妈妈回答:“当然可以啊!”小明又问:“那为什么这只鸭子不会飞?”妈妈耐心地解释道:“这只鸭子是建筑鸭,它只会盖房子。”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

crazy程序猿丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值