基于Spring Mvc實(shí)現(xiàn)的Excel文件上傳下載示例
最近工作遇到一個(gè)需求,需要下載excel模板,編輯后上傳解析存儲(chǔ)到數(shù)據(jù)庫(kù)。因此為了更好的理解公司框架,我就自己先用spring mvc實(shí)現(xiàn)了一個(gè)樣例。
基礎(chǔ)框架
之前曾經(jīng)介紹過(guò)一個(gè)最簡(jiǎn)單的spring mvc的項(xiàng)目如何搭建,傳送門在這里。
這次就基于這個(gè)工程,繼續(xù)實(shí)現(xiàn)上傳下載的小例子。需要做下面的事情:
1 增加index.html,添加form提交文件
2 引入commons-fileupload、commons-io、jxl等工具包
3 創(chuàng)建upload download接口
4 注入multipartResolver bean
5 在upload中使用HttpServletRequest獲取文件流,通過(guò)WorkBook進(jìn)行解析
6 在download中通過(guò)HttpServerResponse返回文件流,實(shí)現(xiàn)下載
頁(yè)面
頁(yè)面很簡(jiǎn)單,其實(shí)就是一個(gè)form標(biāo)簽,需要注意的是:
- form中enctype="multipart/form-data"
- action指定訪問(wèn)的url
- input中需要設(shè)置name屬性,這樣后端才能獲取到文件對(duì)象
<form role="form" action="/upload" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="file">上傳文件</label>
<input type="file" id="file" name="file">
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
引入commons-fileupload、jxl等工具包
涉及的jar包有:
- commons-fileupload 用于獲取上傳文件
- jxl 用于解析excel
<!-- springframework begins -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jexcelapi/jxl -->
<dependency>
<groupId>jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6</version>
</dependency>
Xml的配置
在web.xml中需要配置默認(rèn)的訪問(wèn)頁(yè)面,因?yàn)橹耙呀?jīng)設(shè)置過(guò)攔截的請(qǐng)求是/,因此如果不設(shè)置所有的靜態(tài)頁(yè)面都會(huì)被攔截下來(lái)。
<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
在spring的配置文件中,加入CommonsMultipartResolver的bean。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
上傳代碼
@RequestMapping("upload")
public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException, BiffException, WriteException {
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
MultipartFile file = mRequest.getFile("file");
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
//遍歷Sheet頁(yè)
Arrays.stream(workbook.getSheets())
.forEach(sheet -> {
int size = sheet.getRows();
for(int i=0; i<size; i++){
//遍歷每一行,讀取每列信息
Arrays.stream(sheet.getRow(i)).forEach(cell -> System.out.println(cell.getContents().equals("")?'空':cell.getContents()));
}
});
response.setHeader("Content-Disposition", "attachment; filename=return.xls");
WritableWorkbook writableWorkbook = ExcelUtils.createTemplate(response.getOutputStream());
writableWorkbook.write();
writableWorkbook.close();
}
下載代碼
@RequestMapping("download")
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException, BiffException, WriteException {
response.setHeader("Content-Disposition", "attachment; filename=template.xls");
WritableWorkbook writableWorkbook = ExcelUtils.createTemplate(response.getOutputStream());
writableWorkbook.write();
writableWorkbook.close();
}
模板類
static class ExcelUtils {
public static WritableWorkbook createTemplate(OutputStream output) throws IOException, WriteException {
WritableWorkbook writableWorkbook= Workbook.createWorkbook(output);
WritableSheet wsheet = writableWorkbook.createSheet("測(cè)試title", 0);
CellFormat cf = writableWorkbook.getSheet(0).getCell(1, 0).getCellFormat();
WritableCellFormat wc = new WritableCellFormat();
// 設(shè)置居中
wc.setAlignment(Alignment.CENTRE);
// 設(shè)置邊框線
// wc.setBorder(Border.ALL, BorderLineStyle.THIN);
wc.setBackground(jxl.format.Colour.GREEN);
Label nc0 = new Label(0, 0, "標(biāo)題1",wc);//Label(x,y,z)其中x代表單元格的第x+1列,第y+1行, 單元格的內(nèi)容是z
Label nc1 = new Label(1, 0, "標(biāo)題2",wc);
Label nc2 = new Label(2, 0, "標(biāo)題3",wc);
Label nc3 = new Label(0, 1, "dddd");
Label nc4 = new Label(1, 1, "ffff");
wsheet.addCell(nc0);
wsheet.addCell(nc1);
wsheet.addCell(nc2);
wsheet.addCell(nc3);
wsheet.addCell(nc4);
return writableWorkbook;
}
}
最后貢獻(xiàn)下相關(guān)的代碼:SpringTest_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Swagger Api自動(dòng)生成文檔的實(shí)現(xiàn)
本文主要介紹了SpringBoot整合Swagger Api自動(dòng)生成文檔的實(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Java Kafka實(shí)現(xiàn)延遲隊(duì)列的示例代碼
kafka作為一個(gè)使用廣泛的消息隊(duì)列,很多人都不會(huì)陌生。本文將利用Kafka實(shí)現(xiàn)延遲隊(duì)列,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-08-08
淺談xml配置spring profiles的幾個(gè)注意點(diǎn)
這篇文章主要介紹了淺談xml配置spring profiles的幾個(gè)注意點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
用IDEA創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)步驟記錄
Idea有著非常簡(jiǎn)便的Spring Boot新建過(guò)程,同時(shí)依靠pom自動(dòng)下載依賴,下面這篇文章主要給大家介紹了關(guān)于用IDEA創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)步驟,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
springboot+mybatis+redis 二級(jí)緩存問(wèn)題實(shí)例詳解
Mybatis默認(rèn)沒(méi)有開(kāi)啟二級(jí)緩存,需要在全局配置(mybatis-config.xml)中開(kāi)啟二級(jí)緩存。本文講述的是使用Redis作為緩存,與springboot、mybatis進(jìn)行集成的方法。需要的朋友參考下吧2017-12-12
解決SpringBoot @value注解取不到值的問(wèn)題
這篇文章主要介紹了解決SpringBoot @value注解取不到值的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot MongoDB與MongoDB GridFS基本使用
這篇文章主要為大家介紹了SpringBoot MongoDB與MongoDB GridFS基本使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
String.intern()作用與常量池關(guān)系示例解析
這篇文章主要為大家介紹了String.intern()作用與常量池關(guān)系示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

