download.jsp代码显示:
form提交:
按钮:<input type="button" onclick="toDownLoad('<%=webroot%>/expExamMarksAuditList.action')" value="导出excel">
utl:<%=webroot%>/newalregister/expExamMarksAuditList.action')
function toDownLoad(url){
form00.action = url;
form00.submit();
}
struts.xml文件代码
<!-- 下载 导出审批管理 -->
<action name="expExcel" class="com.DownLoadAction" method="expExcel">
<param name="inputPath">/file/marks.xls</param>
<param name="fileName">marks.xls</param>
<result name="success" type="stream">
<!-- 下载文件的类型,可以去 tomcat\conf\web.xml下找 -->
<param name="contentType">
application/vnd.ms-excel
</param>
<!-- 返回流 excelStream为action中的流变量名称 -->
<param name="inputName">excelStream</param>
<!-- attachment 这个位置的参数挺特殊的,可以设置成下载时,是否出现个下载提示框,或者直接下载之类的,
fileName指定生成的文件名字 为action中变量 -->
<param name="contentDisposition">
attachment;filename="${excelFileName}"
</param>
<!-- 缓存大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
整个配置文件中最重要的两个变量就是 excelStream、excelFileName,这两个变量一定要在action中有定义,否则会报错。
变量templatePosition,这个是运用模板的路径,用模板的意思,是可以预先设置好各种单元格包括颜色,对齐方式,数据格式,
这些东西在程序里也可以控制,但太累,还是用模板吧,代码量少,假如客户想换个颜色什么的也简单,改下模板就好。
DownLoadAction.java
package com;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class DownLoadAction extends ActionSupport {
private InputStream excelStream; // 输出流变量
private String excelFileName; // 下载文件名
private String templatePosition; // 模板路径
private DownloadService downloadService;
public void setDownloadService(DownloadService downloadService) {
this.downloadService = downloadService;
}
public DownloadService getDownloadService() {
return downloadService;
}
public String expExcel() throws Exception {
HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
String contextPath = request.getRealPath("/");
Map<String, Object> aa = Map<String, Object> aa = downloadService
.expExcel(contextPath + "WEB-INF/classes"
+ templatePosition);
this.excelStream = (InputStream) aa.get("stream");
this.excelFileName = (String) aa.get("excelName") + ".xls";
return SUCCESS;
}
/**
* 提供中文转换的功能
*
* @return
*/
public String getExcelFileName() {
try {
return new String(this.excelFileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "aaa.xls";
}
// 对应struts.xml文件中的inputName参数 <param name="inputName">excelStream</param>
public InputStream getExcelStream() {
return excelStream;
}
public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}
public String getTemplatePosition() {
return templatePosition;
}
public void setTemplatePosition(String templatePosition) {
this.templatePosition = templatePosition;
}
public void setExcelFileName(String excelFileName) {
this.excelFileName = excelFileName;
}
}
DownloadService.java
public Map<String,Object> expExcel(String templatePosition) {
Map<String,Object> returnMap=new HashMap<String, Object>();
InputStream is;
ByteArrayInputStream aas;
try {
is = new FileInputStream(templatePosition);
HSSFWorkbook wb = new HSSFWorkbook(is);
HSSFSheet sheet = wb.getSheetAt(0);
/**
* .... 构建 sheet 代码
*/
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
aas = new ByteArrayInputStream(content);
returnMap.put("stream", aas);
returnMap.put("excelName", titlePreStr);
return returnMap;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
service中返回的是个 Map,当中包括返回流跟 excel 名称两部分,因为我们的需求 excel 的标题就是文件名,所有这样写,挺好的,另外再附上一个我自己写的解析样式的方法,个人觉得挺好用
/**
* 样式读取方法
*
* @param styleName
* 样式名数组
* @param styleRow
* 样式行
* @param startCol
* 开始列位置
* @param startCol
* 空白样式列位置 null:不进行空替换
* @return
*/
public static Map<String, HSSFCellStyle> readStyle(String[] styleName,
HSSFRow styleRow, short startCol, Integer blankCol) {
Map<String, HSSFCellStyle> returnMap = new HashMap<String, HSSFCellStyle>();
HSSFCellStyle blankStyle = null;
if (null != blankCol) {
blankStyle = styleRow.getCell(blankCol.shortValue()).getCellStyle();
styleRow.getCell(blankCol.shortValue()).setCellValue("");
}
for (String name : styleName) {
HSSFCell cell = styleRow.getCell(startCol);
HSSFCellStyle style = cell.getCellStyle();
returnMap.put(name, style);
cell.setCellValue("");
if (null != blankStyle) {
cell.setCellStyle(blankStyle);
}
startCol++;
}
return returnMap;
}
把样式都放到一个Map里,方法间传递样式的时候只需要传这个样式Map就好,避免传一堆样式参数