SSM框架使用poi導(dǎo)入導(dǎo)出Excel的詳細(xì)方法
1.首先我們先導(dǎo)入poi和文件上傳的依賴
<!--POI-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14-beta1</version>
</dependency>
<!--文件上傳依賴-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2.在spring-mvc.xml中配置文件上傳解析器
<!-- 配置文件上傳解析器 -->
<!-- id 的值是固定的-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 設(shè)置上傳文件的最大尺寸為 5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
3.創(chuàng)建index.html
<!-- excel文件導(dǎo)出 --> <p><a href="User/exportExcel.do" rel="external nofollow" >導(dǎo)出</a> <!-- excel文件導(dǎo)入 --> <form action="User/importExcel.do" method="post" enctype="multipart/form-data"> <input type="file" name="userExcel" /> <input type="submit" value="導(dǎo)入"> </form>
4.創(chuàng)建實(shí)體類
public class User {
private Integer id;
private String username;
private String password;
/* get 和 set */
}
5.Controller層
/**
* 導(dǎo)出Excel
* @param request
* @param response
*/
@RequestMapping("/exportExcel")
@ResponseBody
public void exportExcel(HttpServletRequest request, HttpServletResponse response){
try {
//獲取數(shù)據(jù)源
List<User> userList = service.queryUserAll();
//導(dǎo)出excel
response.setHeader("Content-Disposition","attachment;filename="+new String("用戶信息.xls".getBytes(),"ISO-8859-1"));
response.setContentType("application/x-excel;charset=UTF-8");
OutputStream outputStream = response.getOutputStream();
//導(dǎo)出
service.exportExcel(userList,outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 導(dǎo)入exc
* @param userExcel
* @param request
* @param session
* @return
*/
@RequestMapping("/importExcel")
@ResponseBody
public String importExcel(MultipartFile userExcel, HttpServletRequest request, HttpSession session) throws IOException, InvalidFormatException {
if(userExcel == null){
session.setAttribute("excelName", "未上傳文件,上傳失敗!");
return null;
}
String userExcelFileName = userExcel.getOriginalFilename();
if(!userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){
session.setAttribute("excelName", "文件格式不正確!請(qǐng)使用.xls或.xlsx后綴的文檔,導(dǎo)入失??!");
return null;
}
//導(dǎo)入
service.importExcel(userExcel);
session.setAttribute("excelName", "導(dǎo)入成功!");
return "redirect:queryUserAll.do";
}
6.運(yùn)行測(cè)試

1.點(diǎn)擊導(dǎo)出將數(shù)據(jù)庫的內(nèi)容以后綴為 .xls的文件下載下來

2. 選擇Excel文件點(diǎn)擊導(dǎo)入會(huì)將文件里的內(nèi)容導(dǎo)入到數(shù)據(jù)庫中


到此這篇關(guān)于SSM框架使用poi導(dǎo)入導(dǎo)出Excel的文章就介紹到這了,更多相關(guān)SSM框架導(dǎo)入導(dǎo)出Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA修改生成jar包名字的兩種方法實(shí)現(xiàn)
本文主要介紹了IDEA修改生成jar包名字的兩種方法實(shí)現(xiàn),通過簡單的步驟,您可以修改項(xiàng)目名稱并在打包時(shí)使用新的名稱,具有一定的參考價(jià)值,感興趣的可以了解下2023-08-08
java經(jīng)典問題:連個(gè)字符串互為回環(huán)變位
連個(gè)字符串互為回環(huán)變位經(jīng)常出現(xiàn)在java程序員面試中,這個(gè)是考驗(yàn)程序員的解題思路和方法的最經(jīng)典的一題,小編為大家詳細(xì)分析一下,一起來學(xué)習(xí)吧。2017-11-11
JAVA編程實(shí)現(xiàn)TCP網(wǎng)絡(luò)通訊的方法示例
這篇文章主要介紹了JAVA編程實(shí)現(xiàn)TCP網(wǎng)絡(luò)通訊的方法,簡單說明了TCP通訊的原理并結(jié)合具體實(shí)例形式分析了java實(shí)現(xiàn)TCP通訊的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
springcloud檢索中間件?ElasticSearch?分布式場(chǎng)景的使用
單機(jī)的elasticsearch做數(shù)據(jù)存儲(chǔ),必然面臨兩個(gè)問題:海量數(shù)據(jù)存儲(chǔ)問題、單點(diǎn)故障問題,本文重點(diǎn)給大家介紹springcloud檢索中間件?ElasticSearch?分布式場(chǎng)景的運(yùn)用,感興趣的朋友跟隨小編一起看看吧2023-10-10

