今天需要为一个很久的struts2项目做一个导出为EXcel的功能,通过百度实现了功能,特此记录下来,做个笔记
前台页面显示
<s:form action="mobileListForSelf.ce" validate="true" name="searchTab" id="searchTab">
<div class="int">
用户名:<s:textfield name="username" id="username" size="12" cssClass="addinput"/> 手机:<s:textfield name="mobile" id="mobile" size="12" cssClass="addinput"/>
</div>
<div class="table03_btn01">
<s:submit id="selectBut" value="查询" cssClass="small" />
<s:submit id="selectBut" value="导出" cssClass="small" method="exportExcel"/>
</div>
</s:form>
struts.xml文件中用到的action
<action name="mobileListForSelf" class="ContactAction" method="mobileListForSelf">
<result>/WEB-INF/jsp/version2/contact_mobile.jsp</result>
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param> <!-- 注意这里的ContentType -->
<param name="inputName">excelStream</param> <!-- 这里需要和Action里的变量名一致 -->
<param name="contentDisposition">attachment;filename="download.xls"</param> <!-- 下载文件的名字 -->
</result>
</action>
java代码
private InputStream excelStream;
//ge和set方法
public String exportExcel(){
System.out.println("导出excel");
FileOutputStream out=null;
try{
if (contactCode == null || "".equals(contactCode)) {
contactCode = (String) session.get("contactCode");
}
if (deptNo == null || "".equals(deptNo)) {
deptNo = (String) session.get("deptNo");
}
if (sourceId == null || "".equals(sourceId)) {
sourceId = (String) session.get("sourceId");
}
logger.info("contactCode=[" + contactCode + "]");
logger.info("deptNo=[" + deptNo + "]");
logger.info("sourceId=[" + sourceId + "]");
session.put("contactCode", contactCode);
session.put("deptNo", deptNo);
session.put("sourceId", sourceId);
if (pageNo == 0) {
// ActionContext ctx = ActionContext.getContext();
// HttpServletRequest request = (HttpServletRequest) ctx
// .get(ServletActionContext.HTTP_REQUEST);
//
// tourl = request.getHeader("Referer");
pageNo = 1;
}
PagingQuery PagingQuery = new PagingQuery();
PagingQuery.setPageNo(pageNo);
PagingQuery.setPageSize(50);
// System.out.println("mobilelist contactCode====="+contactCode);
PagingQuery = contactService.getMobileListForSelf(PagingQuery, contactCode,
username, mobile);
mobileListBySelf = PagingQuery.getResults();
// 声明一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
//声明一个单子并命名
HSSFSheet sheet = wb.createSheet("员工通讯录");
//给单子名称一个长度
sheet.setDefaultColumnWidth((short)15);
// 生成一个样式
HSSFCellStyle style = wb.createCellStyle();
//创建第一行(也可以称为表头)
HSSFRow row = sheet.createRow(0);
//样式字体居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//给表头第一行一次创建单元格
HSSFCell cell = row.createCell( (short) 0); ;
cell.setCellValue("员工姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("电话号码");
cell.setCellStyle(style);
//向单元格里填充数据
for (short i = 0; i < mobileListBySelf.size(); i++) {
row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(((BMobiles)(mobileListBySelf.get(i))).getMobileName());
row.createCell(1).setCellValue(((BMobiles)(mobileListBySelf.get(i))).getMobileNumber());
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
wb.write(output);
byte[] ba = output.toByteArray();
excelStream = new ByteArrayInputStream(ba);
output.flush();
return "excel";
}catch (Exception e) {
return "error";
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
切记,前台提交一定是submit提交,否则不会出现下载效果。