【EasyPoi】SpringBoot使用EasyPoi自定义模版导出Excel

news/2024/7/21 4:42:18 标签: spring boot, excel

EasyPoi

官方文档:http://doc.wupaas.com/docs/easypoi

Excel模版导出

引入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>

创建模版

在这里插入图片描述
在这里插入图片描述

测试

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;
import pers.kw.esaypoi.EasyPoiApp;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest(classes = EasyPoiApp.class)
@RunWith(SpringRunner.class)
public class PoiTest {

    @Test
    public void test() throws Exception {
        ClassPathResource classPathResource = new ClassPathResource("templates/student.xlsx");
        File file = classPathResource.getFile();
        TemplateExportParams params = new TemplateExportParams(
                file.getPath());
        Map<String, Object> map = new HashMap<>();
        map.put("date", "2014年09月01日");
        map.put("sex", "男");
        map.put("stuNo", "12138");
        map.put("addr", "五道口");
        map.put("tel", "123456");
        //excel如何遍历列表
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        FileOutputStream fos = new FileOutputStream("/Users/kw/Downloads/student.xlsx");
        workbook.write(fos);
        fos.close();
    }
}

导出效果
在这里插入图片描述

问题记录

运行时,类找不到

Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.WorkbookFactory$CreateWorkbook0

这种运行类找不到问题,一般就是依赖冲突了。
使用maven helper查看到poi-ooxml等相关依赖,版本冲突了。
解决:
引入依赖

 <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.1.2</version>
</dependency>

运行报错No valid entries or contents found, this is not a valid OOXML (Office Open XML) file

原因是maven插件打包配置了过滤文件类型
排除xlsx xls类型即可

<build>
        <resources>
            <resource>
                <directory>src/main/resources/</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.html</include>
                    <include>**/*.xlsx</include>
                    <include>**/*.xls</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <encoding>utf-8</encoding>
                    <resources>
                        <resource>
                            <directory>src/main/resources/</directory>
                            <filtering>true</filtering>
                        </resource>
                    </resources>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>
                            xls
                        </nonFilteredFileExtension>
                        <nonFilteredFileExtension>
                            xlsx
                        </nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <target>1.8</target>
                    <source>1.8</source>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

http://www.niftyadmin.cn/n/5062845.html

相关文章

春招秋招,在线测评应用得越来越普及

这年代提到测评&#xff0c;很多人都比较熟悉&#xff0c;它有一种根据所选的问题给予合适答案方面的作用。因为不同的测评带来的影响不一样&#xff0c;所以很多人都会关注在线测评的内容有哪些。在校园招聘上面&#xff0c;在线测评也频繁出现了&#xff0c;这让很多人好奇它…

【LeetCode刷题笔记】一维数组

1.两数之和 解题思路&#xff1a; 哈希 &#xff0c;每次循环将 元素值 和对应 下标 放入 map 中&#xff0c;每次更新 map 之前先判断一下&#xff0c;如果 map 中已经包含 target - nums[i] 的 key &#xff0c;则找到答案&#xff0c;返回 当前下标 和之前的 key 对应的 下…

js生成随机16进制数

在JavaScript中&#xff0c;可以使用以下的代码来生成一个100位的随机十六进制数&#xff1a; function generateRandomHex(length) {var result ;var characters 0123456789abcdef;for (var i 0; i < length; i) {result characters.charAt(Math.floor(Math.random() …

链表的基本操作(数据结构)

单链表 #include <stdlib.h> #include <iostream> #include <stdio.h> typedef struct LNode{int data;struct LNode *next; }LNode,*LinkList;打印链表 void PrintList(LNode *p) {LNode *temp;temp p->next;printf("链表的顺序:");while(t…

Springboot+vue的在线试题题库管理系统(有报告),Javaee项目,springboot vue前后端分离项目。

演示视频&#xff1a; Springbootvue的在线试题题库管理系统&#xff08;有报告&#xff09;&#xff0c;Javaee项目&#xff0c;springboot vue前后端分离项目。 项目介绍&#xff1a; 本文设计了一个基于Springbootvue的前后端分离的在线试题题库管理系统&#xff0c;采用M&…

批量将文件名称符合要求的文件自动复制到新文件夹:Python实现

本文介绍基于Python语言&#xff0c;读取一个文件夹&#xff0c;并将其中每一个子文件夹内符合名称要求的文件加以筛选&#xff0c;并将筛选得到的文件复制到另一个目标文件夹中的方法。 本文的需求是&#xff1a;现在有一个大的文件夹&#xff0c;其中含有多个子文件夹&#x…

【已解决】opencv 交叉编译 ffmpeg选项始终为NO

一、opencv 交叉编译没有 ffmpeg &#xff0c;会导致视频打不开 在交叉编译时候&#xff0c;发现在 pc 端能用 opencv 打开的视频&#xff0c;但是在 rv1126 上打不开。在网上查了很久&#xff0c;原因可能是 交叉编译过程 ffmpeg 造成的。之前 ffmpeg 是直接用 apt 安装的&am…

品牌数字人IP如何从虚拟到现实快速出圈?

在数智化时代&#xff0c;数字人如何赋能品牌IP&#xff0c;为品牌挖掘出更多的增长机会&#xff1f;近日&#xff0c;纯甄馋酸奶官数字人“解小馋”以一支精致的CG视频正式亮相。 *图片源于网络 品牌数字人以CG动画可以让品牌形象更具象化&#xff0c;通过数字人三维动画的形…