struts2 + poi 导出下载无法实现 浏览器无法响应
struts2.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"></include>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="default" extends="struts-default">
<!-- poi实现Excel文件下载 -->
<action name="download" class="excelDownloadAction" method="excelDownload">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">attachment;filename="AllUsers.xls"</param>
<param name="inputName">excelStream</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
action文件:
public class ExcelDownloadAction extends BaseAction{
/**
* @fields serialVersionUID
*/
private static final long serialVersionUID = 4846417235749456473L;
private DownService downService;
private InputStream excelStream;
public DownService getDownService() {
return downService;
}
public void setDownService(DownService downService) {
this.downService = downService;
}
public String excelDownload()throws Exception{
response.reset();
response.setContentType("application/vn.ms-excel");
response.setCharacterEncoding("utf-8");
//利用json获取数据
List<Integer> did = new ArrayList<Integer>();
String jsonstr = request.getParameter("jsonstr");
JSONArray json = JSONArray.fromObject(jsonstr);
Object[] obj = json.toArray();
for (int i = 0; i < obj.length; i++) {
JSONObject object = JSONObject.fromObject(obj[i]);
for (@SuppressWarnings("unchecked")
Iterator<String> iterator = object.keys(); iterator.hasNext();) {
String key = iterator.next();
int id =Integer.parseInt(object.get(object.get(key)).toString());
did.add(id);
}
}
List<Device> deviceList =downService.excelDownload(did);
// for(int i = 0;i<deviceList.size();i++){
// Device device = deviceList.get(i);
// System.out.println(device.getDevicename());
// }
//下载
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.reset();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet");
{
// 设置单元格格式(文本)
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
for(int index=0 ;index<deviceList.size(); index++){
HSSFRow row = sheet.createRow(index);
Device device = deviceList.get(index);
HSSFCell cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
HSSFRichTextString str = new HSSFRichTextString(device.getDeviceid());
cell.setCellValue(str);
cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
str = new HSSFRichTextString(device.getDevicename());
cell.setCellValue(str);
}
try {
workbook.write(baos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] ba = baos.toByteArray();
excelStream = new ByteArrayInputStream(ba);
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
action没有报错,页面不提示下载,根本无法响应,求教对Struts2有很深了解的人给予解答。
由于处于学习阶段,用的包比较多。我猜测可能是由于包冲突的原因,测试调试了很多次都无法解决,下面是我导入工程的java包:
同时欢迎其他方法重写导出下载,只要能运行均可。 struts2 java 导出下载 java包 --------------------编程问答-------------------- 1. struts配置文件里的class="excelDownloadAction"加上完整包名,比如class="XX.YY.excelDownloadAction"
2.去掉一个common--fileupload.jar
3.去掉一个poi-ooxml-3.9-xxxx.jar,看下是否和poi-ooxml-schemas-3.9-xxxx.jar一样,不一样就不用去了。 --------------------编程问答-------------------- jar包引入有点问题,代码有点多,你可以参考我写的一个例子,http://download.csdn.net/detail/xiaoliouc/5117319 --------------------编程问答-------------------- 给你个execl下载工具列
ExeclBuilder类里面包括execl的名字和内容
--------------------编程问答-------------------- 上面能解决?不能解决请改用JXL兼容性比这个POI好很多 --------------------编程问答-------------------- 你可以返回json,但是不用ajax方式调用 --------------------编程问答-------------------- 楼主注意:引用jar包的时候同类型的包不要引用多个,如果有多个版本的同类型jar包,去掉版本较低的那个。如:common--fileupload.jar 和 common--fileupload-1.2.1.jar,去掉前面那个包,引用后面那个jar就可以了。 --------------------编程问答--------------------
protected void downloadReport(ExcelBuilder excel) {
HttpServletResponse response = ServletActionContext.getResponse();
ServletOutputStream out;
try {
String csvName = excel.getExcelName();
response.setContentType("application/csv; charset=GB2312");
response.setHeader("Content-Disposition", "inline; filename=\""
+ new String(csvName.getBytes("UTF-8"), "ISO8859-1") + "\"");
out = response.getOutputStream();
ExcelHelper2.poiWrite(excel, out);
out.flush();
out.close();
} catch (UnsupportedEncodingException e) {
// Pass
} catch (IOException e) {
throw new RuntimeException("生成CSV文件时发生错误", e);
}
}
1.class文件导入类我用Sping框架部署了,之前我单独用struts2框架,方class="XX.YY.excelDownloadAction"也无法解决
2.包已去,谢谢。这是历史遗留问题
3.poi-ooxml-schemas-3.9-xxxx.jar和poi-ooxml-3.9-xxxx.jar不一样,poi-ooxml-schemas-3.9-xxxx.jar用于解决Excel中.xlsx文件格式的! --------------------编程问答--------------------
谢谢。 --------------------编程问答--------------------
谢谢。 --------------------编程问答--------------------
这种方法我试过了!还是不行
可以确定的是不是我程序的问题,单独用3楼的方法可以下载,整合到我写的程序里就不行了。貌似是包或者流的问题。具体的我在试试吧!不行就放弃了!这种问题只能说人品差点!
补充:Java , Web 开发