javascript
easypoi导出数值型_SpringBoot使用EasyPoi进行数据导入导出Excel(一)
在實(shí)際項(xiàng)目開(kāi)發(fā)中,對(duì)于Excel的導(dǎo)入導(dǎo)出還是很常見(jiàn)的需求,比如說(shuō)將數(shù)據(jù)根據(jù)模板批量導(dǎo)入到數(shù)據(jù)庫(kù)中,以及將數(shù)據(jù)庫(kù)中的數(shù)據(jù)批量導(dǎo)出陳Excel的形式
現(xiàn)有需求:
下載固定的導(dǎo)入Excel模板
導(dǎo)入Excel中的數(shù)據(jù)進(jìn)數(shù)據(jù)庫(kù)
將數(shù)據(jù)進(jìn)行Ecel導(dǎo)出
本篇文章,先總結(jié)excel靜態(tài)模板文件的下載
一. 準(zhǔn)備工作
準(zhǔn)備靜態(tài)文件
導(dǎo)入 EasyPOI 的依賴
1.7.9
4.1.0
cn.afterturn
easypoi-spring-boot-starter
${easypoi.version}
cn.afterturn
easypoi-base
${easypoi.version}
cn.afterturn
easypoi-web
${easypoi.version}
cn.afterturn
easypoi-annotation
${easypoi.version}
二. 使用easypoi進(jìn)行靜態(tài)模板的導(dǎo)出
excel靜態(tài)模板下載,有兩種方式:
第一種,在http頭中指定輸出文件流的類型為"application/vnd.ms-excel"類型時(shí),輸出流時(shí)就不需要添加輸出文件的后綴名;
@GetMapping("/templateDownload")
public ResponseEntity templateDownload(@PathVariable("organizationId")Long tenantId,
HttpServletResponse response ) {
try {
// 獲取資源中的模板文件
ClassPathResource resource = new ClassPathResource("static\\拉線-設(shè)備主數(shù)據(jù)導(dǎo)入模板.xlsx");
InputStream inputStream = resource.getInputStream();
Workbook wb = WorkbookFactory.create(inputStream);
String fileName="拉線-設(shè)備主數(shù)據(jù)導(dǎo)入模板";
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
wb.write(response.getOutputStream());
return Results.success();
}catch (IOException e){
return Results.error(e.getMessage());
}
第二種,指定文件流的類型為"multipart/form-data"時(shí),輸出流時(shí)需要判斷文件是.xls/.xlsx,并且加上后綴名。
@GetMapping("/templateDownload1")
public ResponseEntity templateDownload1(@PathVariable("organizationId")Long tenantId,
HttpServletResponse response ) {
try {
// 獲取資源中的模板文件
ClassPathResource resource = new ClassPathResource("static\\拉線-設(shè)備主數(shù)據(jù)導(dǎo)入模板.xlsx");
InputStream inputStream = resource.getInputStream();
// 根據(jù)不同excel創(chuàng)建不同對(duì)象,Excel2003版本-->HSSFWorkbook,Excel2007版本-->XSSFWorkbook
Workbook wb = WorkbookFactory.create(inputStream);
response.reset();
response.setContentType("multipart/form-data");
String fileName="拉線-設(shè)備主數(shù)據(jù)導(dǎo)入模板";
// 判斷excel文件類型,下載獲取到的模板并重新命名
System.out.println(wb.getClass().getSimpleName());
if (wb.getClass().getSimpleName().equals("HSSFWorkbook")) {
response.setHeader("Content-Disposition",
"attachment; filename=" + "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
} else {
response.setHeader("Content-Disposition",
"attachment; filename=" + "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
}
wb.write(response.getOutputStream());
return Results.success();
}catch (IOException e){
return Results.error(e.getMessage());
}
這部分我只是大概寫(xiě)了一下測(cè)試實(shí)現(xiàn),在實(shí)際的工作中,導(dǎo)入導(dǎo)出等代碼肯定是有特別高的復(fù)用率的,可以將代碼中其中一部分抽離出來(lái)一個(gè)公用的工具類進(jìn)行調(diào)用
總結(jié)
以上是生活随笔為你收集整理的easypoi导出数值型_SpringBoot使用EasyPoi进行数据导入导出Excel(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 建立索引和主外约束_Mysql索引原理
- 下一篇: activemenu怎么拼 vue_Vu