资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springBoot使用hutool工具类导出excel的方法

这篇文章给大家分享的是springBoot使用hutool工具类导出excel的方法。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

创新互联公司-专业网站定制、快速模板网站建设、高性价比肇庆网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式肇庆网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖肇庆地区。费用合理售后完善,十年实体公司更值得信赖。

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);

    }}

运行时报了个错;
由于我是新建的项目,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使用hutool工具类导出excel的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读。


网站标题:springBoot使用hutool工具类导出excel的方法
网页网址:http://cdkjz.cn/article/psdhce.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220