SSM框架使用poi導入導出Excel的詳細方法
1.首先我們先導入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">
<!-- 設置上傳文件的最大尺寸為 5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
3.創(chuàng)建index.html
<!-- excel文件導出 --> <p><a href="User/exportExcel.do" rel="external nofollow" >導出</a> <!-- excel文件導入 --> <form action="User/importExcel.do" method="post" enctype="multipart/form-data"> <input type="file" name="userExcel" /> <input type="submit" value="導入"> </form>
4.創(chuàng)建實體類
public class User {
private Integer id;
private String username;
private String password;
/* get 和 set */
}
5.Controller層
/**
* 導出Excel
* @param request
* @param response
*/
@RequestMapping("/exportExcel")
@ResponseBody
public void exportExcel(HttpServletRequest request, HttpServletResponse response){
try {
//獲取數(shù)據(jù)源
List<User> userList = service.queryUserAll();
//導出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();
//導出
service.exportExcel(userList,outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 導入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", "文件格式不正確!請使用.xls或.xlsx后綴的文檔,導入失??!");
return null;
}
//導入
service.importExcel(userExcel);
session.setAttribute("excelName", "導入成功!");
return "redirect:queryUserAll.do";
}
6.運行測試

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

2. 選擇Excel文件點擊導入會將文件里的內容導入到數(shù)據(jù)庫中


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

