在webwork2 的action中处理:
public class TestDownLoadAction
{
private String fileName ;
//添加setter|getter方法
public void downLoad()throws Exception
{
// 下载全部数据
//pageIterator = stateBo.getStateList(storeId, null, null, "default",pageNo,pageSize);
list = stateBo.getStateList(storeId, null, null, "default");
String pathFile = new File(OpeExcel.class.getResource("/")
.toString()).getParent().toString();
String pathFile_deal = pathFile.substring(6, pathFile.length());
String filePath = new String((pathFile_deal + "\\download\\"
+ storeId + ".xls").getBytes("ISO8859_1"), "utf-8");
map.put("list1", list);
OpeExcel.writeStatanaly(map, filePath);// 通过poi生成一个excel
// 下载生成的excel文件
try
{
fileName = "统计分析";
//把文件名转码位"ISO8859-1"的格式,避免在弹出下载对话框时文件名是乱码
fileName = new String(fileName.getBytes("utf-8"),"ISO8859-1");
File input = new File(filePath);
excelStream = new FileInputStream(input);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
在xwork.xml里配置:
result name="download" type="stream">
<param name="contentType">application/x-download;charset=UTF-8</param>
<param name="inputName">excelStream</param>
<param name="bufferSize">2048</param>
<param name="contentDisposition">attachment;fileName="${fileName}.xls"</param>
</result>