开发者

Java实现导出word表格的示例详解

目录
  • 目标
  • 实现
    • 依赖
    • 模版
    • 代码

目标

多级表头、分页、动态数据

Java实现导出word表格的示例详解

Java实现导出word表格的示例详解

实现

依赖

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

模版

Java实现导出word表格的示例详解

Java实现导出word表格的示例详解

代码

TableData数据(模版对应的数据对象)

package org.example.bean;


import com.deepoove.poi.data.TableRenderData;
import lombok.Data;


@Data
public class TableData {

    /**
     * 标题
     */
    private String title;

    /**
     * 表格
     */
    private TableRenderData table;

    privatjavascripte String[][] tableList;

    /**
     * 总价
     */
    private String totalPrice;

}

核心代码

package org.example.controller;

import lombok.SneakyThrows;
import org.example.bean.TableData;
import org.springframework.web.bind.annotation.*;
import com.deepoove.poi.XwpFTemplate;
import com.deepoove.poi.data.*;

import Java.io.*;
imandroidport java.math.BigDecimal;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

/**
 * Java导出word表格
 * 根据word模版,手绘表格
 */
@RestController
@RequestMapping(value = "/word")
public class WordController {

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

        /* 假数据 */
        tableData.setTitle("附件1-报价明细表");
        String[][] strings = new String[100][5];
        for (int i = 0; i < 100; i++) {
            strings[i] = new String[]{"1", "EREWHON", "生猪", "酒鬼酒", "125"};
        }
        tableData.setTableList(strings);

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

        // 手绘表格
        // 表头
        RowRenderData row0 = Rows.of("项号", "编号", "种类", "", "价格").center().create();
        RowRenderData row1 = Rows.of("项号", "编号", "期货", "股票", "价格").center().create();

        int length = 0;
        if (tableData.getTableList() != null) {
            length = tableData.getTableList().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(tableData.getTableList()[i]).center().create();
            String s = tableData.getTableList()[i][4];
            BigDecimal bigDecimal = new BigDecimal(s);
            totalPrice = totalPrice.add(bigDecimal);
        }

        RowRenderData row4 = Rows.of("总价", "", "", "", totalPrice.toString()).center().create();
        rowRenderData[rowRenderData.length - 1] = row4;
        // 表格合并,根据坐标
        MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(0, 0), MergeCellRule.Grid.of(1, 0)).
                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)).
              python  map(MergeCellRule.Grid.of(rowRenderData.length - 1, 0), MergeCellRule.Grid.of(rowRenderData.length - 1, 3)).
                build();

        TableRenderData table = Tables.of(rowRenderData).mergeRule(rule).create();
        // 数据封编程客栈装
        tableData.setTable(table);
        // 传入模板模板地址+信息数据
        XWPFTemplate template = XWPFTemplate.compile(wordPath + modelName).render(tableData);
        // 指定下载的文件名--设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("附件1-报价明细表.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();
   开发者_JAVA         out.flush();
            template.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

到此这篇关于Java实现导出word表格的示例详解的文章就介绍到这了,更多相关Java导出word表格内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜