springBoot使用hutool工具類導(dǎo)出excel的方法

這篇文章給大家分享的是springBoot使用hutool工具類導(dǎo)出excel的方法。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比肇慶網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式肇慶網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋肇慶地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。

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());
// 通過工具類創(chuàng)建writer,默認(rèn)創(chuàng)建xls格式
ExcelWriter writer = ExcelUtil.getWriter();
//自定義標(biāo)題別名
writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("age", "年齡");
writer.addHeaderAlias("birthDay", "生日");
// 合并單元格后的標(biāo)題行,使用默認(rèn)標(biāo)題樣式
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);

    }}

運(yùn)行時(shí)報(bào)了個(gè)錯(cuò);
由于我是新建的項(xiàng)目,dataSource 沒填寫。導(dǎo)致提示:

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沒有找到合適的加載類,其實(shí)是大部分spring - datasource - url沒有加載成功,分析原因如下所示.

DataSourceAutoConfiguration會(huì)自動(dòng)加載.

沒有配置spring - datasource - url 屬性.

spring - datasource - url 配置的地址格式有問題.

配置 spring - datasource - url的文件沒有加載.

網(wǎng)上給出了這幾種解決方案.
方法1:Controller 層添加

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})

方案二 (解決原因2)

在application.properties/或者application.yml文件中沒有添加數(shù)據(jù)庫配置信息.
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

看完這篇文章,你們學(xué)會(huì)springBoot使用hutool工具類導(dǎo)出excel的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀。

網(wǎng)站名稱:springBoot使用hutool工具類導(dǎo)出excel的方法
文章網(wǎng)址:http://bm7419.com/article24/psdhce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站維護(hù)品牌網(wǎng)站建設(shè)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)