这篇文章给大家分享的是springBoot使用hutool工具类导出excel的方法。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
pm.xml中新加支撑
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
(猜你喜欢:使用Hutool 工具库导出Excel表格的方法)
接着就是在controller层
直接贴出我的代码
public class MyHelloWorldController {
@RequestMapping("/export")
public void export(HttpServletResponse response){
List<User> list = new ArrayList<>();
User obj = new User();
obj.setName("卡卡罗特");
obj.setAge("25");
obj.setBirthDay("0903");
list.add(obj);
list.add(new User());
// 通过工具类创建writer,默认创建xls格式
ExcelWriter writer = ExcelUtil.getWriter();
//自定义标题别名
writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("age", "年龄");
writer.addHeaderAlias("birthDay", "生日");
// 合并单元格后的标题行,使用默认标题样式
writer.merge(2, "申请人员信息");
writer.write(list, true);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String name = "test";
response.setHeader("Content-Disposition","attachment;filename="+name+".xls");
ServletOutputStream out= null;
try {
out = response.getOutputStream();
writer.flush(out, true);
} catch (IOException e) {
e.printStackTrace();
}finally {
writer.close();
}
IoUtil.close(out);
}}
(猜你喜欢:使用hutool工具导出Excel标题自定义顺序)
运行时报了个错;
由于我是新建的项目,dataSource 没填写。导致提示:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
问题原因: Mybatis没有找到合适的加载类,其实是大部分spring - datasource - url没有加载成功,分析原因如下所示.
DataSourceAutoConfiguration会自动加载.
没有配置spring - datasource - url 属性.
spring - datasource - url 配置的地址格式有问题.
配置 spring - datasource - url的文件没有加载.
网上给出了这几种解决方案.
方法1:Controller 层添加
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
方案二 (解决原因2)
在application.properties/或者application.yml文件中没有添加数据库配置信息.
spring:datasource:url: jdbc:mysql://localhost:3306/read_data? useUnicode=true&characterEncoding=UTF-8&useSSL=falseusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driver
方案三:
//正确示例
spring.datasource.url = jdbc:mysql://47.168.0.116:1504/f_me?setUnicode=true&characterEncoding=utf8
更多相关资讯:
SpringBoot使用JeecgBoot中的Autopoi功能如何实现导出Excel
看完这篇文章,你们学会springBoot使用hutool工具类导出excel的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。