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


    EasyPoi

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

    Excel模版导出

    引入依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>cn.afterturngroupId>
                <artifactId>easypoi-baseartifactId>
                <version>4.4.0version>
            dependency>
            <dependency>
                <groupId>cn.afterturngroupId>
                <artifactId>easypoi-webartifactId>
                <version>4.4.0version>
            dependency>
            <dependency>
                <groupId>cn.afterturngroupId>
                <artifactId>easypoi-annotationartifactId>
                <version>4.4.0version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poiartifactId>
                <version>4.1.2version>
            dependency>
            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poi-ooxmlartifactId>
                <version>4.1.2version>
            dependency>
            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poi-ooxml-schemasartifactId>
                <version>4.1.2version>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    创建模版

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

    测试

    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(
                    "templates/student.xlsx");
            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();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

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

    问题记录

    运行时,类找不到

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

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

     <dependency>
        <groupId>org.apache.poigroupId>
        <artifactId>poiartifactId>
        <version>4.1.2version>
    dependency>
    <dependency>
        <groupId>org.apache.poigroupId>
        <artifactId>poi-ooxmlartifactId>
        <version>4.1.2version>
    dependency>
    <dependency>
        <groupId>org.apache.poigroupId>
        <artifactId>poi-ooxml-schemasartifactId>
        <version>4.1.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行报错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>**/*.ymlinclude>
                        <include>**/*.xmlinclude>
                        <include>**/*.propertiesinclude>
                        <include>**/*.htmlinclude>
                        <include>**/*.xlsxinclude>
                        <include>**/*.xlsinclude>
                    includes>
                    <filtering>truefiltering>
                resource>
            resources>
    
            <plugins>
                <plugin>
                    <artifactId>maven-resources-pluginartifactId>
                    <version>3.1.0version>
                    <configuration>
                        <encoding>utf-8encoding>
                        <resources>
                            <resource>
                                <directory>src/main/resources/directory>
                                <filtering>truefiltering>
                            resource>
                        resources>
                        <nonFilteredFileExtensions>
                            <nonFilteredFileExtension>
                                xls
                            nonFilteredFileExtension>
                            <nonFilteredFileExtension>
                                xlsx
                            nonFilteredFileExtension>
                        nonFilteredFileExtensions>
                    configuration>
                plugin>
    
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.8.1version>
                    <configuration>
                        <target>1.8target>
                        <source>1.8source>
                        <encoding>utf-8encoding>
                    configuration>
                plugin>
            plugins>
        build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    JavaSE-day01笔记
    前端基础之CSS
    window本地编译Spring源码并运行
    bean-validation----hibernate-validator校验框架整理【未完待续】
    JDK11以上版本中提取jre
    职场日常:测试人员如何快速熟悉新业务?
    万字长文,手把手教你重构
    pnpm为什么卸载卸载不干净
    数据结构 day4 链表
    几款拿来就能用的vue工具库,不来看看吗?
  • 原文地址:https://blog.csdn.net/qq_36762765/article/details/133471124