java ajax 导出excel文件_springMVC(4)---生成excel文件并导出
springMVC(4)---生成excel文件并導(dǎo)出
在開發(fā)過(guò)程中,需要將數(shù)據(jù)庫(kù)中的數(shù)據(jù)以excel表格的方式導(dǎo)出。
首先說(shuō)明。我這里用的是Apache的POI項(xiàng)目,它是目前比較成熟的HSSF接口,用來(lái)處理Excel對(duì)象。其實(shí)POI不僅僅只能處理excel,它還可以處理word、PowerPoint、Visio、甚至Outlook。
一.首先介紹利用POI如何生成excel。
首先在生成Excel前,我們需要理解一下Excel文件的組織形式。在POI中,是這樣理解的:一個(gè)Excel文件對(duì)應(yīng)一個(gè)workbook,一個(gè)workerbook是有若干個(gè)sheet組成的。一個(gè)sheet有多個(gè)row,一個(gè)row一般存在多個(gè)cell。
對(duì)于上面的四個(gè)名詞我們可以在下圖理解:
對(duì)于生成Excel,POI提供了如下幾個(gè)基本對(duì)象:
HSSFWorkbook ? ? ? ? ? ? excel 的文檔對(duì)象
HSSFSheet ? ? ? ? ? ? ? ?excel 的表單
HSSFRow ? ? ? ? ? ? ? ? ?excel 的行
HSSFCell ? ? ? ? ? ? ? ? excel 的格子單元
從上面的圖片和Excel的組織結(jié)構(gòu),我們就可以明白創(chuàng)建Excel的步驟。
1、生成文檔對(duì)象HSSHWorkbook。
2、通過(guò)HSSFWorkbook生成表單HSSFSheet。
3、通過(guò)HSSFSheet生成行HSSFRow
4、通過(guò)HSSFRow生成單元格HSSFCell。
下面展示代碼:
第一步、導(dǎo)入jar包
org.apache.poi
poi-ooxml
3.9
第二步,創(chuàng)建Model對(duì)象
public classPerson {privateString id;privateString name;privateString password;privateString age;publicPerson(String id, String name, String password, String age) {super();this.id =id;this.name =name;this.password =password;this.age =age;
}//提供set和get方法
}
第三步.下載界面 exportexcel.jsp
functiondownload(){varurl="download_excel?id=10&name=張三";
window.open(url);
}
第四步、ExcleController.java
importjava.io.UnsupportedEncodingException;importjava.net.URLEncoder;importjavax.servlet.ServletOutputStream;importjavax.servlet.http.HttpServletResponse;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importcom.ssm.service.impl.ExcleImpl;
@Controllerpublic classExcleController {//這里直接new了
ExcleImpl excleImpl=newExcleImpl();
@RequestMapping(value="/jsp/download_excel")//獲取url鏈接上的參數(shù)
public @ResponseBody String dowm(HttpServletResponse response,@RequestParam("id") String id,@RequestParam("name") String name){
response.setContentType("application/binary;charset=UTF-8");try{
ServletOutputStream out=response.getOutputStream();try{//設(shè)置文件頭:最后一個(gè)參數(shù)是設(shè)置下載文件名(這里我們叫:張三.pdf)
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name+".xls", "UTF-8"));
}catch(UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String[] titles= { "用戶id", "用戶姓名", "用戶密碼", "用戶年齡"};
excleImpl.export(titles, out);return "success";
}catch(Exception e){
e.printStackTrace();return "導(dǎo)出信息失敗";
}
}
}
第五步、ExcleImpl 報(bào)表導(dǎo)出實(shí)現(xiàn)層
importjava.util.ArrayList;importjavax.servlet.ServletOutputStream;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFCellStyle;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importcom.ssm.model.Person;public classExcleImpl {public void export(String[] titles, ServletOutputStream out) throwsException{try{//第一步,創(chuàng)建一個(gè)workbook,對(duì)應(yīng)一個(gè)Excel文件
HSSFWorkbook workbook = newHSSFWorkbook();//第二步,在webbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet
HSSFSheet hssfSheet = workbook.createSheet("sheet1");//第三步,在sheet中添加表頭第0行,注意老版本poi對(duì)Excel的行數(shù)列數(shù)有限制short
HSSFRow row= hssfSheet.createRow(0);//第四步,創(chuàng)建單元格,并設(shè)置值表頭 設(shè)置表頭居中
HSSFCellStyle hssfCellStyle =workbook.createCellStyle();//居中樣式
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell hssfCell= null;for (int i = 0; i < titles.length; i++) {
hssfCell= row.createCell(i);//列索引從0開始
hssfCell.setCellValue(titles[i]);//列名1
hssfCell.setCellStyle(hssfCellStyle);//列居中顯示
}//第五步,寫入實(shí)體數(shù)據(jù)
Person person1=new Person("1","張三","123","26");
Person person2=new Person("2","李四","123","18");
Person person3=new Person("3","王五","123","77");
Person person4=new Person("4","徐小筱","123","1");//這里我把list當(dāng)做數(shù)據(jù)庫(kù)啦
ArrayList list=new ArrayList();
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);for (int i = 0; i < list.size(); i++) {
row= hssfSheet.createRow(i+1);
Person person=list.get(i);//第六步,創(chuàng)建單元格,并設(shè)置值
String id = null;if(person.getId() != null){
id=person.getId();
}
row.createCell(0).setCellValue(id);
String name= "";if(person.getName() != null){
name=person.getName();
}
row.createCell(1).setCellValue(name);
String password= "";if(person.getPassword() != null){
password=person.getPassword();
}
row.createCell(2).setCellValue(password);
String age=null;if(person.getAge() !=null){
age=person.getAge();
}
row.createCell(3).setCellValue(age);
}//第七步,將文件輸出到客戶端瀏覽器
try{
workbook.write(out);
out.flush();
out.close();
}catch(Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();throw new Exception("導(dǎo)出信息失敗!");
}
}
}
第六步:最終效果,當(dāng)我點(diǎn)擊報(bào)表導(dǎo)出按鈕
完美!
想的太多,做的太少,中間的落差就是煩惱,要么去做,要么別想?少尉【11】
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java ajax 导出excel文件_springMVC(4)---生成excel文件并导出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jfCacheMgr.exe是什么进程?
- 下一篇: s3c2440移植MQTT