hutool导出excel大数据_springBoot使用hutool工具类导出excel的方法
springBoot使用hutool工具类导出excel的方法
发布时间:2020-06-03 21:51:26
来源:亿速云
阅读:1124
这篇文章给大家分享的是springBoot使用hutool工具类导出excel的方法。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
pm.xml中新加支撑
cn.hutool
hutool-all
5.0.7
org.apache.poi
poi-ooxml
4.1.1
org.apache.poi
poi-ooxml-schemas
3.17
接着就是在controller层
直接贴出我的代码public class MyHelloWorldController {
@RequestMapping("/export")
public void export(HttpServletResponse response){
List 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);
}}
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使用hutool工具类导出excel的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
