一、前言
最近做了一個(gè)導(dǎo)入Excel的功能,需求:
- 先提供一個(gè)下載Excel模板的功能。
- 用戶下載好模板后,可以在模板文件當(dāng)中填寫要上傳的內(nèi)容,填寫完過后再進(jìn)行導(dǎo)入Excel,然后將用戶填寫的數(shù)據(jù)保存到數(shù)據(jù)庫當(dāng)中。
二、下載模板
1.將模板放到resources目錄下,盡量創(chuàng)建一個(gè)專門的文件夾來存放模板,如下:


2.這里我用到了兩個(gè)依賴,一個(gè)是hutool目前最火的工具類,easyexcel基本上導(dǎo)入導(dǎo)出Excel都會(huì)用。
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.1</version>
</dependency>
@GetMapping("/downInChargeOfTemplate")
public void downInChargeOfTemplate(HttpServletResponse response) {
downloadService.downInChargeOfTemplate(response);
}
import cn.hutool.core.io.IoUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@Service
public class DownloadService {
public void downInChargeOfTemplate(HttpServletResponse response) {
responseSetting(response, "各分任務(wù)負(fù)責(zé)人導(dǎo)入模板", ".xlsx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
InputStream inputStream = null;
OutputStream outputStream = null;
try {
// 讀取文件的輸入流
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("templates/各分任務(wù)負(fù)責(zé)人導(dǎo)入模板.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(inputStream);
outputStream = response.getOutputStream();
wb.write(outputStream);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
IoUtil.close(inputStream);
IoUtil.close(outputStream);
}
}
public void responseSetting(HttpServletResponse response, String fileName, String suffix, String contentType) {
// 這里URLEncoder.encode可以防止中文亂碼 當(dāng)然和easyexcel沒有關(guān)系
String newFileName = null;
try {
newFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 當(dāng)客戶端請(qǐng)求的資源是一個(gè)可下載的資源(這里的“可下載”是指瀏覽器會(huì)彈出下載框或者下載界面)時(shí),對(duì)這個(gè)可下載資源的描述(例如下載框中的文件名稱)就是來源于該頭域。
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + newFileName + suffix);
// 服務(wù)器告訴瀏覽器它發(fā)送的數(shù)據(jù)屬于什么文件類型,也就是響應(yīng)數(shù)據(jù)的MIME類型
response.setContentType(contentType);
response.setCharacterEncoding("utf-8");
// 關(guān)閉緩存(HTTP/1.1)
response.setHeader("Cache-Control", "no-store");
// 關(guān)閉緩存(HTTP/1.0)
response.setHeader("Pragma", "no-cache");
// 緩存有效時(shí)間
response.setDateHeader("Expires", 0);
}
}
3.測(cè)試接口

4.補(bǔ)充知識(shí)
在拿到文件的InputStream輸入流之后我們也可以不通過XSSFWorkbook將輸入流寫到輸出流當(dāng)中,可以直接使用流拷貝的方式,這種同樣是可以完成導(dǎo)出模板功能的。
文章來源:http://www.zghlxwxcb.cn/news/detail-613932.html
三、Excel導(dǎo)入
關(guān)于導(dǎo)入功能,直接參考easyexcel官網(wǎng)即可:https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read文章來源地址http://www.zghlxwxcb.cn/news/detail-613932.html
到了這里,關(guān)于Java下載excel模板文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!