刚开始工作,第一个项目里有这样一个功能,导出为Excel,导出Excel的文件的接口别人已经写好,之前也写过利用JXL导出的方法,那都是直接导出数据到本地,写一个类就能搞定,但是该高人写的接口太复杂了,。。得需要用XML文件作为模板,然后填充,但是该方法返回一个InputStream,,不知道该怎么处理该输入流,并且怎样实现在客户端点击按钮,导出excel,自定义保存路径呢??public static InputStream createExcel(List objectList,
ExcelTemplate template,boolean serialNum){
try {
OutputStream out =  new ByteArrayOutputStream();
WritableWorkbook workBook = createExcelWorkbook(out);
int sheetNum = objectList.size()/SHEETNO;
for(int i=0;i<=sheetNum;i++){
int end = (i+1)*SHEETNO;
if(end>=objectList.size()){
end = objectList.size();
}
List sheetList = objectList.subList( i*SHEETNO,end);
WritableSheet sheet = createExcelSheet(workBook,
template.getSheet(),i);
List<Cell> cellList = template.getCellList();
initLable(sheet, cellList,serialNum);
writeExcel(sheet, sheetList, cellList,serialNum);
}
workBook.write();
workBook.close();
return new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray());   
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

解决方案 »

  1.   

    一直都用poi呢,关注中
       public String download() throws Exception {
          try {
             String filePath = servletRequest.getParameter("filePath");
             File file = new File(filePath);
             String fileName = file.getName().replaceAll("\\[.+\\]", "");
             fileName = URLEncoder.encode(fileName, "utf-8");
             fileName = fileName.replace('+', ' ');
             servletResponse.setContentType("application/x-download");
             servletResponse.addHeader("Content-Disposition",
                   "attachment;filename=" + fileName);         excelStream = new FileInputStream(file);
          }
          catch(Exception e) {
             write(Tool.getErrorMsg(e.getMessage()));         return ERROR;
          }      return SUCCESS;
       }
    另存为代码。
      

  2.   

    大侠,你的servletResponse是什么参数???