欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

使用Spring MVC生成Excel文檔

系統 1892 0

Spring不僅支持jsp,velocity,freemarker,同時可以將數據生成到Excel,PDF等非html文檔

首先,我們編寫控制器,返回邏輯名“ ListStudentUseExcel

package ?Action;

import ?javax.servlet.http.HttpServletRequest;
import ?javax.servlet.http.HttpServletResponse;

import ?org.springframework.validation.BindException;
import ?org.springframework.web.servlet.ModelAndView;
import ?org.springframework.web.servlet.mvc.AbstractCommandController;

public ? class ?TestExcelController? extends ?AbstractCommandController? ... {



????
protected ?ModelAndView?handle(HttpServletRequest?arg0,?HttpServletResponse?arg1,?Object?arg2,?BindException?arg3)? throws ?Exception? ... {
????????
return ? null ;
????}


????
protected ?ModelAndView?handleRequestInternal(HttpServletRequest?arg0,?HttpServletResponse?arg1)? throws ?Exception? ... {
????????
return ? new ?ModelAndView( " ListStudentUseExcel " , "" , "" );
????}


}

?配置文件:

?

<? xml?version="1.0"?encoding="UTF-8" ?>
<! DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd"? >
< beans >

< bean? id ="simpleUrlMapping" ?class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
?
< property? name ="mappings" >
???
< props >
???????
< prop? key ="/excel.mvc" > TestExcelController </ prop >
???
</ props >
?
</ property >
</ bean >


< bean? id ="ListStudentUseExcel" ?class ="Action.ListStudentUseExcel" ></ bean >

< bean? id ="beanNameViewResolver" ?class ="org.springframework.web.servlet.view.BeanNameViewResolver" /> ?

< bean? id ="TestExcelController" ?class ="Action.TestExcelController" />


</ beans >

?

由于上述配置文件采用了beanNameViewResolver的當時,所以,我們要編寫一個合controller返回邏輯名相同的
ViewClass---ListStudentUseExcel,代碼如下:

?

package ?Action;

import ?java.util.ArrayList;
import ?java.util.Iterator;
import ?java.util.Map;

import ?javax.servlet.http.HttpServletRequest;
import ?javax.servlet.http.HttpServletResponse;

import ?model.Student;

import ?org.apache.poi.hssf.usermodel.HSSFCellStyle;
import ?org.apache.poi.hssf.usermodel.HSSFDataFormat;
import ?org.apache.poi.hssf.usermodel.HSSFRow;
import ?org.apache.poi.hssf.usermodel.HSSFSheet;
import ?org.apache.poi.hssf.usermodel.HSSFWorkbook;
import ?org.springframework.web.servlet.view.document.AbstractExcelView;

public ? class ?ListStudentUseExcel? extends ?AbstractExcelView? ... {

????
protected ?? void ?buildExcelDocument(Map?model,
????????????HSSFWorkbook?workbook,
????????????HttpServletRequest?request,
????????????HttpServletResponse?response)
throws ?Exception ... {

????????
??????????
// 設置response方式,使執行此controller時候自動出現下載頁面,而非直接使用excel打開
??????????response.setContentType( " APPLICATION/OCTET-STREAM " );?
??????????response.setHeader(
" Content-Disposition " ,?
??????????
" attachment;?filename=" " ? + ? " excel.xls " ? + ? " " " );?

????????
// 構造數據
????????Student?stu1 = new ?Student( " gaoxiang1 " , " male1 " , " 20060101 " , 1 );
????????Student?stu2
= new ?Student( " gaoxiang2 " , " male2 " , " 20060102 " , 2 );
????????Student?stu3
= new ?Student( " gaoxiang3 " , " male3 " , " 20060103 " , 3 );
????????Student?stu4
= new ?Student( " gaoxiang4 " , " male4 " , " 20060104 " , 4 );
????????Student?stu5
= new ?Student( " gaoxiang5 " , " male5 " , " 20060105 " , 5 );
????????ArrayList?stuList
= new ?ArrayList();
????????stuList.add(stu1);
????????stuList.add(stu2);
????????stuList.add(stu3);
????????stuList.add(stu4);
????????stuList.add(stu5);
????????
????????
// 產生Excel表頭
????????HSSFSheet?sheet = workbook.createSheet( " studentList " );
????????HSSFRow?header
= sheet.createRow( 0 );? // 第0行
????????
// 產生標題列
????????header.createCell(( short ) 0 ).setCellValue( " name " );
????????header.createCell((
short ) 1 ).setCellValue( " sex " );
????????header.createCell((
short ) 2 ).setCellValue( " date " );
????????header.createCell((
short ) 3 ).setCellValue( " count " );
????????HSSFCellStyle?cellStyle
= workbook.createCellStyle();
????????cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(
" mm/dd/yyyy " ));
????????
????????
// 填充數據
???????? int ?rowNum = 1 ;
????????
for ?(Iterator?iter? = ?stuList.iterator();?iter.hasNext();)? ... {
????????????Student?element?
= ?(Student)?iter.next();
????????????HSSFRow?row
= sheet.createRow(rowNum ++ );
????????????row.createCell((
short ) 0 ).setCellValue(element.getName().toString());
????????????row.createCell((
short ) 1 ).setCellValue(element.getSex().toString());
????????????row.createCell((
short ) 2 ).setCellValue(element.getDate().toString());
????????????row.getCell((
short ) 2 ).setCellStyle(cellStyle);
????????????row.createCell((
short ) 3 ).setCellValue(element.getCount());
????????}

????????
????????
// 列總和計算
????????HSSFRow?row = sheet.createRow(rowNum);
????????row.createCell((
short ) 0 ).setCellValue( " TOTAL: " );
????????String?formual
= " SUM(D2:D " + rowNum + " ) " ;? // D2到D[rowNum]單元格起(count數據)
????????row.createCell(( short ) 3 ).setCellFormula(formual);
????????
????}


}

測試頁面:/exlce/ListStudentUseExcel.jsp

?

<% ... @?page?language = " java " ?contentType = " text/html;?charset=GB18030 "
????pageEncoding
= " GB18030 "
%>
<! DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >

< meta? http-equiv ="Content-Type" ?content ="text/html;?charset=GB18030" >
< title > Insert?title?here </ title >
</ head >
< body >

< input? type ="button" ?onclick ="javascript:window.location.href='<%=request.getContextPath()?%>/excel.mvc'" ?value ="download?excel" ></ input >

</ body >
</ html >

?

運行ListStudentUseExcel.jsp,點擊下載按鈕,程序運行結果及生成的excel如下:

?



使用Spring MVC生成Excel文檔


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲精品老司机综合影院 | 欧美一区二区在线视频 | 精品视频入口 | 国产精品久久久久无码人妻 | 日韩第一| 国产欧美一区二区三区另类精品 | 国产精品片一区二区三区 | 精品欧美成人高清视频在线观看 | 91毛片网 | 成人九色 | 黄色av网站免费看 | 久久精品网 | 国产高清永久免费 | 狠狠五月深爱婷婷网免费 | 亚洲一区中文字幕 | 国产精品美女视频 | 青草视频在线免费观看 | 超级碰碰碰视频在线观看 | 成人精品鲁一区一区二区 | 欧美精品99久久久久久人 | 一级片在线播放 | 5c5c5c精品视频在线观看 | 欧美影院在线 | 亚洲国产综合精品中文第一区 | 精品欧美乱码久久久久久1区2区 | 亚洲欧美另类综合 | 亚洲精品国精品久久99热 | 波多野一区二区三区在线 | 欧美成人a∨高清免费观看 久久亚洲欧美日韩精品专区 | 欧美在线观看视频一区 | 国产综合在线视频 | 欧美性免费视频 | a级欧美片免费观看 | 99福利视频 | 日本亚洲天堂网 | 成人欧美s视频在线观看 | 99re6在线视频精品免费 | av免费资源| 国产亚洲精品久久久久久打不开 | 欧美日韩一区二区中文字幕视频 | 欧美日韩在线视频不卡一区二区三区 |