早上刚起来看订阅号时,偶然间看到使用hutool工具类导出excel。以前用过POI、easyexcel等工具的导入导出功能,总感觉太麻烦了,一顿乱七八糟的配置支撑条件。 闲来无事,看着文档实际动手撸一遍。 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> ####################################################################
接着就是在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);
}
}
####################################################################
运行时报了个错;
由于我是新建的项目,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=false username: root password: root driver-class-name: com.mysql.jdbc.Driver
方案三: //正确示例 spring.datasource.url = jdbc:mysql://47.168.0.116:1504/f_me?setUnicode=true&characterEncoding=utf8