【EasyPoi】SpringBoot使用EasyPoi自定义模版导出Excel
作者:小教学发布时间:2023-10-03分类:程序开发学习浏览:83
导读:EasyPoi官方文档:http://doc.wupaas.com/docs/easypoiExcel模版导出引入依赖<dependencies&g...
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>
- 程序开发学习排行
-
- 1鸿蒙HarmonyOS:Web组件网页白屏检测
- 2HTTPS协议是安全传输,为啥还要再加密?
- 3HarmonyOS鸿蒙应用开发——数据持久化Preferences
- 4记解决MaterialButton背景颜色与设置值不同
- 5鸿蒙HarmonyOS实战-ArkUI组件(RelativeContainer)
- 6鸿蒙HarmonyOS实战-ArkUI组件(Stack)
- 7鸿蒙HarmonyOS实战-ArkUI组件(GridRow/GridCol)
- 8[Android][NDK][Cmake]一文搞懂Android项目中的Cmake
- 9鸿蒙HarmonyOS实战-ArkUI组件(mediaquery)
- 最近发表
-
- WooCommerce最好的WordPress常用插件下载博客插件模块的相关产品
- 羊驼机器人最好的WordPress常用插件下载博客插件模块
- IP信息记录器最好的WordPress常用插件下载博客插件模块
- Linkly for WooCommerce最好的WordPress常用插件下载博客插件模块
- 元素聚合器Forms最好的WordPress常用插件下载博客插件模块
- Promaker Chat 最好的WordPress通用插件下载 博客插件模块
- 自动更新发布日期最好的WordPress常用插件下载博客插件模块
- WordPress官方最好的获取回复WordPress常用插件下载博客插件模块
- Img to rss最好的wordpress常用插件下载博客插件模块
- WPMozo为Elementor最好的WordPress常用插件下载博客插件模块添加精简版