- 添加maven依賴
- 編寫要導(dǎo)出的類
- 去數(shù)據(jù)庫(kù)查詢要導(dǎo)出的數(shù)據(jù)
- 調(diào)用接口導(dǎo)出數(shù)據(jù)
添加maven依賴
<!-- 導(dǎo)出excel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.5</version>
</dependency>
編寫導(dǎo)出的excel的表的實(shí)體類
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode
public class AddMoney {
@ExcelProperty("用戶")
private String username;
@ExcelProperty("充值時(shí)間")
private String date;
@ExcelProperty("充值金額")
private int usermoney;
}
編寫使用mybatis-plus到數(shù)據(jù)庫(kù)中查詢表
private List<AddMoney> getAddMoney() {
List<Changemoney> AddMoney = changemoneyService.exportexcelChangeMoney();
List<AddMoney> list = ListUtils.newArrayList();
for (int i = 0; i < AddMoney.size(); i++) {
AddMoney data = new AddMoney();
data.setUsername(AddMoney.get(i).getUsername());
data.setDate(AddMoney.get(i).getCreatedate());
data.setUsermoney(AddMoney.get(i).getAddmoney());
list.add(data);
}
return list;
}
編寫接口并調(diào)用
@GetMapping("exportExcelAddMoney")
public void exportExccelAddMoney(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("充值情況", "UTF-8").replaceAll("\\+", "%20");
List<AddMoney> list = getAddMoney();
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(),
AddMoney.class).sheet("充值情況").doWrite(list);
}
實(shí)操測(cè)試 點(diǎn)擊鏈接會(huì)實(shí)現(xiàn)導(dǎo)出excel的功能
http://162.14.107.118:8086/PC/exportExcelAddMoney文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-691413.html
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-691413.html
到了這里,關(guān)于springboot實(shí)現(xiàn)導(dǎo)出excel的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!