struts POI 下载excel 的设置

引用

Struts.xml
<result name="excel" type="stream"> 
                <param name="contentType">application/vnd.ms-excel</param> 
                <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> 
                <param name="bufferSize">1024</param>
                <param name="inputName">excelFile</param> 
            </result> 



引用

private InputStream excelFile;
private String downloadFileName;


Setter&Getter
public String getDownloadFileName() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd ");

String downloadFileName = (sf.format(new Date()).toString())
+ "项目信息.xls";
try {
downloadFileName = new String(downloadFileName.getBytes(),
"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}

public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}

public InputStream getExcelFile() {
return excelFile;
}

public void setExcelFile(InputStream excelFile) {
this.excelFile = excelFile;
}

// 导出
public String export() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
List<FrontProjectList> dataList = projectApplyBaseService
.query_ProjectApply3(pqc, 0, projectApplyBaseService
.count_queryTotalProject_consumption(pqc) + 1);
HSSFWorkbook workbook = exportExcel(dataList);
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.write(output);

byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
return "excel";
}


引用

关于 has already been called for this response 的问题
respones.getWrite()和response.getOutputStream()不能同时使用
或者使用前加上response.reset();
不过reset的时候 有可能要response.setContentType(String);


参考:
Struts2 poi导出excel
http://www.cnblogs.com/rason2008/archive/2012/03/24/2415564.html

你可能感兴趣的:(struts)