easypoi 导出word表格

pom.xml 引入:

       <!-- easypoi-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.1.0</version>
        </dependency>
        <!-- 防止控制台打印错误提示-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.6</version>
        </dependency>

Student.java

package com.ruoyi.project.demo.domain;

import cn.afterturn.easypoi.excel.annotation.Excel;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @className: Student
 * @description: 学生信息
 * @date: 2021/12/16
 * @author: cakin
 */
public class Student implements Serializable {
    private static final long serialVersionUID = 2131321500629905052L;

    @Excel(name = "学生姓名")
    private String studentName;

    @Excel(name = "学生年龄")
    private Integer age;

    @Excel(name = "学生生日", importFormat = "yyyy/MM/dd", exportFormat = "yyyy/MM/dd")
    private Date birthday;

    @Excel(name = "语文成绩")
    private BigDecimal chineseScore;

    @Excel(name = "数学成绩")
    private BigDecimal mathScore;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public BigDecimal getChineseScore() {
        return chineseScore;
    }

    public void setChineseScore(BigDecimal chineseScore) {
        this.chineseScore = chineseScore;
    }

    public BigDecimal getMathScore() {
        return mathScore;
    }

    public void setMathScore(BigDecimal mathScore) {
        this.mathScore = mathScore;
    }
}




WordTest.java

package com.ruoyi.project.demo.domain;

import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.easypoi.word.entity.params.ExcelListEntity;
import cn.afterturn.easypoi.word.parse.excel.ExcelEntityParse;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * @className: WordUtils
 * @description: 参考文档
 * https://blog.csdn.net/dingfengbo1203/article/details/106229844
 * https://blog.csdn.net/YangangwuWuyangang/article/details/115206209
 * @date: 2021/12/16
 * @author: cakin
 */
public class WordTest {
    private static final String TEMPLATE_FILE_NAME = "D:/template.docx";
    private static final String TEMPLATE_FILE_NAME_JOB = "D:/template_job.docx";

    public static void main(String[] args) {
        // 简单文档导出
        SimpleWordExport();
        // 列表导出
        ExportList();
        // 导出工作经历
        exportJob();
    }

    /**
     * 功能描述:简单文档导出
     *
     * @author cakin
     * @date 2021/12/16
     */
    public static void SimpleWordExport() {
        Map<String, Object> map = new HashMap<>();
        map.put("0", "one");
        map.put("1", "two");
        map.put("2", "three");
        map.put("3", "four");
        map.put("4", "five");
        map.put("5", "six");
        try {
            XWPFDocument doc = WordExportUtil.exportWord07( TEMPLATE_FILE_NAME, map);
            FileOutputStream fos = new FileOutputStream("D:/simpleWord.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:列表导出
     *
     * @author cakin
     * @date 2021/12/16
     */
    public static void ExportList() {
        Map<String, Object> map = new HashMap<>();
        map.put("0", "one");
        map.put("1", "two");
        map.put("2", "three");
        map.put("3", "four");
        map.put("4", "five");
        map.put("5", "six");
        try {
            XWPFDocument doc = WordExportUtil.exportWord07(TEMPLATE_FILE_NAME, map);
            List<Student> list = new ArrayList<>(2);
            addStudents(list);
            // 导出批量数据
            new ExcelEntityParse().parseNextRowAndAddRow(doc.getTableArray(1), 1, new ExcelListEntity(list, Student.class, 1));


            FileOutputStream fos = new FileOutputStream("D:/simpleList.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:增加学生到列表中
     *
     * @param list 列表
     * @author 贝医
     * @date 2021/12/16
     */
    private static void addStudents(List<Student> list) {
        Student stu = new Student();
        stu.setStudentName("wuyuan");
        stu.setAge(11);
        stu.setBirthday(new Date());
        stu.setChineseScore(new BigDecimal("88"));
        stu.setMathScore(new BigDecimal("85.5"));
        list.add(stu);
        stu = new Student();
        stu.setStudentName("lisi");
        stu.setAge(12);
        stu.setBirthday(new Date());
        stu.setChineseScore(new BigDecimal("87"));
        stu.setMathScore(new BigDecimal("89"));
        list.add(stu);
    }

    /**
     * 功能描述:导出工作经历
     *
     * @author cakin
     * @date 2021/12/16
     */
    public static void exportJob() {
        try {
            try (FileOutputStream fos = new FileOutputStream("d:/expJob.docx")) {
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("title", "个人信息");
                Map<String, String> user = new HashMap<>();
                user.put("name", "张三");
                user.put("age", "22");
                user.put("address", "重庆渝北区");
                user.put("other", "篮球");
                dataMap.put("user", user);

                List<Map<String, String>> jobs = new ArrayList<>();
                Map<String, String> job;
                for (int i = 0; i < 5; i++) {
                    job = new HashMap<>();
                    job.put("name", "公司名称-" + i);
                    job.put("address", "公司地址:" + i);
                    jobs.add(job);
                }

                dataMap.put("jobs", jobs);
                byte[] doc = exportWord(TEMPLATE_FILE_NAME_JOB, dataMap);
                fos.write(doc);
                fos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出word(2007版本docx)
     *
     * @param templateWordPath 模板路径
     * @param dataMap          导出数据
     * @return 导出数据
     * @throws Exception
     */
    public static byte[] exportWord(String templateWordPath, Map<String, Object> dataMap) throws Exception {
        File tf = new File(templateWordPath);
        if (!tf.exists() || !tf.isFile()) {
            throw new RuntimeException("File [" + templateWordPath + "] Not Found Or Not File.");
        }
        XWPFDocument document = WordExportUtil.exportWord07(templateWordPath, dataMap);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        document.write(bos);
        return bos.toByteArray();
    }
}




template.docx 模板内容:

{{0}}

{{1}}

{{2}}

{{3}}

{{4}}

{{5}}

学生姓名

学生年龄

学生生日

语文成绩

数学成绩

template_job.docx 模板内容:

名称

年龄

地址

名称2

{{user.name}}

{{user.age}}

{{user.address}}

{{user.other}}

公司名称

地址

{{$fe:jobs t.name

t.address}}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
把一个单元格跨越多行,使之成为一整列的方法叫做表格纵向合并。在Easypoi中,可以使用@ExcelEntity注解或者@ExcelCollection注解来实现表格纵向合并。具体方法如下: 1.使用@ExcelEntity注解 (1)在实体类中,定义需要合并的列,使用@ExcelEntity注解进行标注。 例如,需要将第一列(学生姓名)合并,可以这样定义实体类: ```java public class Student { @ExcelEntity(name = "学生姓名", rowspan = 2) private String name; @Excel(name = "语文") private int chinese; @Excel(name = "数学") private int math; } ``` (2)在导出Excel的时候,使用ExcelExportUtil.exportExcel()方法进行导出。 例如,导出一个学生列表,可以这样写导出代码: ```java List<Student> studentList = new ArrayList<>(); // ... 添加学生数据 ExcelExportUtil.exportExcel(new ExportParams("学生列表", "学生"), Student.class, studentList); ``` 2.使用@ExcelCollection注解 (1)在实体类中,定义需要合并的列,使用@ExcelCollection注解进行标注。 例如,需要将第一列(学生姓名)合并,可以这样定义实体类: ```java public class StudentList { @ExcelCollection(name = "学生信息", orderNum = "1") private List<Student> studentList; } ``` (2)在导出Excel的时候,使用ExcelExportUtil.exportExcel()方法进行导出。 例如,导出一个学生列表,可以这样写导出代码: ```java List<Student> studentList = new ArrayList<>(); // ... 添加学生数据 StudentList list = new StudentList(); list.setStudentList(studentList); ExcelExportUtil.exportExcel(new ExportParams("学生列表", "学生"), StudentList.class, list); ``` 以上是Easypoi实现表格纵向合并的方法。需要注意的是,使用@ExcelEntity注解时,需要将实体类作为导出Excel的参数;而使用@ExcelCollection注解时,需要将包含实体类的列表作为导出Excel的参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值