近段时间在做一个项目,其中有一个功能是需要将用户填的信息以及上传的附件等进行打包下载。于是乎,网上各种找资料,现将成果分享如下:
/** * 打包下载文件 信息 * @param mav * @param request * @param response * @param attachmentId */ @Transactional(propagation=Propagation.REQUIRED, readOnly = true) public void zipDownLoadAttachment(ModelAndView mav, HttpServletRequest request,Map<String, Object> result,HttpServletResponse response,String id){ List<SupplierSubAttachmentEntity> listAttachment=supplierSubAttachmentDao.findByChiefId(Integer.valueOf(id)); List<SupplierSubContactEntity> listContact=supplierSubContactDao.findByChiefId(Integer.valueOf(id)); List<SupplierSubTrackEntity> listTrack=supplierSubTrackDao.findByChiefId(Integer.valueOf(id)); SupplierChiefEntity chief=supplierChiefDao.findById(Integer.valueOf(id)); //导出基础数据 this.outPutExcel(request,chief,listContact,listTrack); String filePath=BusSystemConfig.downLoadPath; String path=BusSystemConfig.contentFileUpLocalPath; // //生成的ZIP文件名为Demo.zip String tmpFileName = chief.getName()+".zip"; byte[] buffer = new byte[1024]; String strZipPath = filePath +tmpFileName; try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream( strZipPath)); String filename=""; // 需要同时下载的多个文件 File[] file1=new File[listAttachment.size()+1]; for(int i=0;i<listAttachment.size();i++){ file1[i]=new File(path + "/"+listAttachment.get(i).getAttachmentUrl()); } file1[listAttachment.size()]=new File(filePath+chief.getName()+".xls"); for (int i = 0; i < file1.length; i++) { if(!file1[i].exists()){ continue; } if(i<listAttachment.size()){ filename=listAttachment.get(i).getName(); }else{ filename=file1[i].getName(); } FileInputStream fis = new FileInputStream(file1[i]); out.putNextEntry(new ZipEntry(filename)); //设置压缩文件内的字符编码,不然会变成乱码 //out.setEncoding("GBK"); int len; // 读入需要下载的文件的内容,打包到zip文件 while ((len = fis.read(buffer)) > 0) { out.write(buffer, 0, len); } out.closeEntry(); fis.close(); } out.flush(); out.close(); this.downLoad(mav,request,response,tmpFileName,filePath); } catch (Exception e) { logger.error("文件下载出错", e); }finally{ try { File f = new File(filePath+chief.getName()+".xls"); f.delete(); } catch (Exception e) { e.printStackTrace(); } } }
//这段代码是将压缩包下载到本地
public void downLoad(ModelAndView mav,HttpServletRequest request,HttpServletResponse response,String str,String filePath){ String path=filePath+str; try { File file = new File(path); if(!file.exists()){ file.createNewFile(); } OutputStream os = response.getOutputStream(); response.reset();// 清空输出流 response.setContentType(request.getSession().getServletContext().getMimeType(str)); // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名 response.setHeader("Content-disposition", "attachment;filename=" + new String(str.getBytes("UTF-8"),"ISO8859-1")); InputStream fis = new FileInputStream(path); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); os.write(buffer);// 输出文件 os.flush(); os.close(); } catch (IOException e) { logger.error("文件下载出错", e); } finally{ try { File f = new File(path); f.delete(); } catch (Exception e) { e.printStackTrace(); } } }
//这段代码是将子数据导出到excel表中
public void outPutExcel(HttpServletRequest request,SupplierChiefEntity chiefEntity, List<SupplierSubContactEntity> listContact,List<SupplierSubTrackEntity> listTrack) { try { String fileName = chiefEntity.getName()+".xls"; String filePath=BusSystemConfig.downLoadPath; WritableWorkbook workbook = Workbook.createWorkbook(new File(filePath+fileName)); if (workbook != null) { WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); // 定义格式 WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义 WritableCellFormat wcfmt = new WritableCellFormat(); // 单元格定义 // 设置标题 sheet.addCell(new jxl.write.Label(列(从0开始), 行(从0开始), // 内容.)); try { String[] baseTitles=baseTitle(); wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式 wcf.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK); wcfmt.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK); WritableSheet sheet = workbook.createSheet("基础信息", 0); sheet.addCell(new Label(0, 0, "基础信息", wcf)); // 合并单元格 sheet.mergeCells(0, 0, baseTitles.length-1, 0); // 设置行高度 sheet.setRowView(0, 500); for(int i=0;i<baseTitles.length;i++){ // 设置单元格的宽度 sheet.setColumnView(i, 25); sheet.addCell(new Label(i, 1, baseTitles[i], wcf)); } sheet.setRowView(2, 400); sheet.addCell(new Label(0, 2 , chiefEntity.getName(), wcfmt)); sheet.addCell(new Label(1, 2 , chiefEntity.getChiefBusinissShow(), wcfmt)); sheet.addCell(new Label(2, 2 , chiefEntity.getCorporate(), wcfmt)); sheet.addCell(new Label(3, 2 , chiefEntity.getMobile(), wcfmt)); sheet.addCell(new Label(4, 2 , chiefEntity.getEmail(), wcfmt)); sheet.addCell(new Label(5, 2 , chiefEntity.getSetupDate(), wcfmt)); sheet.addCell(new Label(6, 2 , chiefEntity.getRegistFund(), wcfmt)); sheet.addCell(new Label(7, 2 , chiefEntity.getTinNumber(), wcfmt)); sheet.addCell(new Label(8, 2 , chiefEntity.getFixPhone(), wcfmt)); sheet.addCell(new Label(9, 2 , chiefEntity.getFax(), wcfmt)); sheet.addCell(new Label(10, 2 , chiefEntity.getAddress(), wcfmt)); sheet.addCell(new Label(11, 2 , chiefEntity.getCompanyUrl(), wcfmt)); //==========================导出业务联系人信息======================= String[] contactTitles=contactTitle(); WritableSheet sheet1 = workbook.createSheet("业务联系人信息", 1); sheet1.addCell(new Label(0, 0, "业务联系人信息", wcf)); // 合并单元格 sheet1.mergeCells(0, 0,contactTitles.length-1, 0); // 设置行高度 sheet1.setRowView(0, 400); for(int i=0;i<contactTitles.length;i++){ // 设置单元格的宽度 sheet1.setColumnView(i, 22); sheet1.addCell(new Label(i, 1, contactTitles[i], wcf)); } SupplierSubContactEntity contactEntity=null; for(int i=0;i<listContact.size();i++){ contactEntity=listContact.get(i); sheet1.setRowView((i+2), 400); sheet1.addCell(new Label(0, (i+2) , contactEntity.getName(), wcfmt)); sheet1.addCell(new Label(1, (i+2) , contactEntity.getSexTypeShow(), wcfmt)); sheet1.addCell(new Label(2, (i+2) , contactEntity.getDepartShow(), wcfmt)); sheet1.addCell(new Label(3, (i+2) , contactEntity.getJobShow(), wcfmt)); sheet1.addCell(new Label(4, (i+2) , contactEntity.getMobile(), wcfmt)); sheet1.addCell(new Label(5, (i+2) , contactEntity.getEmail(), wcfmt)); } //==========================导出业务联系人END============================= String[] trackTitles=trackTitle(); WritableSheet sheet2 = workbook.createSheet("主要业绩信息", 2); sheet2.addCell(new Label(0, 0, "主要业绩信息", wcf)); // 合并单元格 sheet2.mergeCells(0, 0,trackTitles.length-1, 0); // 设置行高度 sheet2.setRowView(0, 400); for(int i=0;i<trackTitles.length;i++){ // 设置单元格的宽度 sheet2.setColumnView(i, 22); sheet2.addCell(new Label(i, 1, trackTitles[i], wcf)); } SupplierSubTrackEntity trackEntity=null; for(int i=0;i<listTrack.size();i++){ trackEntity=listTrack.get(i); sheet2.setRowView((i+2), 400); sheet2.addCell(new Label(0, (i+2) , trackEntity.getEngineerName(), wcfmt)); sheet2.addCell(new Label(1, (i+2) , trackEntity.getMoney().toString(), wcfmt)); sheet2.addCell(new Label(2, (i+2) , trackEntity.getEngineerStartDate(), wcfmt)); sheet2.addCell(new Label(3, (i+2) , trackEntity.getEngineerEndDate(), wcfmt)); sheet2.addCell(new Label(4, (i+2) , trackEntity.getFirstParty(), wcfmt)); sheet2.addCell(new Label(5, (i+2) , trackEntity.getRemark(), wcfmt)); } //============================导出主要业绩Start=========================== //============================导出主要业绩END============================= // 从内存中写入文件中 workbook.write(); // 关闭资源,释放内存 workbook.close(); } catch (RowsExceededException e) { logger.error("sheet不存在", e); } catch (WriteException e) { logger.error("创建列名出错", e); } } } catch (IOException e) { logger.error("文件创建出错", e); } } public String[] baseTitle(){ String[] titles=new String[]{"单位名称","主要业务","法人代表","法人手机","法人邮箱","成立时间","注册资金(万元)","税务登记","固定电话","公司传真","公司地址","公司网址"}; return titles; } public String[] contactTitle(){ String[] titles=new String[]{"姓名","性别","所在部门","职务","手机号码","电子邮箱"}; return titles; } public String[] trackTitle(){ String[] titles=new String[]{"工程名称","合同金额","工程开始时间","工程结束时间","开发商","备注"}; return titles; }
至些,打包下载全部完成。欢迎批评指正。
作者:Sugar_521 发表于2016/11/4 18:35:21 原文链接
阅读:68 评论:0 查看评论