问题描述
action中;publicvoidgetDownloadFile(){if(this.obj!=null){Integerdepid=Integer.parseInt(this.depId);Filefile=this.employeeService.getInputStream(depid);//request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);//response=(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);Stringstr="/Excel/"+file.getName();Stringpath=request.getSession().getServletContext().getRealPath(str);DownloadFiles.download(path,response);}}serviceimple中:publicFilegetInputStream(Integerdepid){HSSFWorkbookwb=newHSSFWorkbook();HSSFSheetsheet=wb.createSheet("sheet1");HSSFCellStylestyle=wb.createCellStyle();//设置表头的类型style.setAlignment(HSSFCellStyle.ALIGN_CENTER);HSSFCellStylestyle1=wb.createCellStyle();//设置表头的类型style1.setAlignment(HSSFCellStyle.ALIGN_LEFT);HSSFFontfont=wb.createFont();//设置字体//表头标题HSSFRowrow=sheet.createRow(0);HSSFCellcell=row.createCell(0);cell.setCellValue("序号");sheet.setColumnWidth(0,3000);//设置列的宽度font.setColor(HSSFFont.COLOR_NORMAL);//设置单元格字体的颜色.//font.setFontHeight((short)350);//设置单元字体高度style.setFont(font);//设置字体风格cell.setCellStyle(style);////数据List<Employee>list=this.employeeDAO.getEmployeeInfo(depid);for(inti=0;i<list.size();i++){Employeeemployee=list.get(i);row=sheet.createRow(i+1);cell=row.createCell(0);cell.setCellValue(i+1);cell.setCellStyle(style1);}//第一种方法:为每个用户产生不同的文件,随机文件名.xls,但下载完成后要删除(不一定及时删除,如过1小时删除)//将数据从内存中放到真正的test.xls中//StringfileName=CharacterUtil.makeRandomString(10)+".xls";StringfileName="D:/apache-tomcat-6.0.29/webapps/assetManage/Excel/empInfo.xls";finalFilefile=newFile(fileName);//临时文件test.xls默认会放在tomcat/bin下面,//最好生成随机文件名.xls,并在使用完成后,动态删除这些文件try{OutputStreamout=newFileOutputStream(file);wb.write(out);out.close();}catch(Exceptione){e.printStackTrace();}//InputStreamis=null;//try{////is=newFileInputStream(file);//}catch(FileNotFoundExceptione){//e.printStackTrace();//}//用后台线程删除生成的临时文件,这样有问题:万一服务器意外停止,此文件就永远不能删除了//解决办法:让服务器启动时,用servlet检查是否有xls文件,有了就删除//finalfile=newFile(fileName);//newThread(newRunnable(){//内部类//publicvoidrun(){////线程睡10秒,让用户有时间来下载//try{//Thread.sleep(100);//线程睡10秒//}catch(InterruptedExceptione){//e.printStackTrace();//}//file.delete();//删除临时文件//}//}).start();returnfile;}实体中publicclassDownloadFiles{publicDownloadFiles(){}publicstaticHttpServletResponsedownload(Stringpath,HttpServletResponseresponse){try{//path是指欲下载的文件的路径Filefile=newFile(path);//取得文件名。Stringfilename=file.getName();filename=newString(filename.getBytes("GBK"),"iso8859-1");//以流的形式下载文件。InputStreamfis=newBufferedInputStream(newFileInputStream(path));byte[]buffer=newbyte[fis.available()];fis.read(buffer);fis.close();//清空responseresponse.reset();//设置response的Header//HTTP响应弹出下载框的response.setContentType("application/vnd.ms-excel;charset=gb2312");//把文件流写入http响应里面response.addHeader("Content-Disposition","inline;filename="+newString(filename.getBytes()));//attachment---作为附件下载,inline---在线打开response.addHeader("Content-Length",""+file.length());response.sendRedirect(filename);//response.addHeader("Content-Type","application/vnd.ms-excel");//response.setHeader("Content-Disposition","attachment;filename=test.xls");OutputStreamtoClient=newBufferedOutputStream(response.getOutputStream());toClient.write(buffer);toClient.flush();toClient.close();}catch(IOExceptionex){ex.printStackTrace();}returnresponse;}}struts.xml中<actionname="employee_*"method="{1}"class="employeeAction"><resultname="showSuccess"type="json"></result><resultname="Error">/index.jsp</result><resultname="success"type="stream"><paramname="contentType">application/vnd.ms-excel</param><paramname="contentDisposition">attachment;filename="empInfo.xls"</param><paramname="inputName">downloadFile</param></result></action>各位大侠帮忙看看哪里出错了?Stringpath=request.getSession().getServletContext().getRealPath(str);运行到这里就跳过该方法..........
解决方案
解决方案二:
SpringMVC中才难实现啊...
解决方案三:
该回复于2012-08-29 14:12:59被版主删除
解决方案四:
怎样用js导出啊