java poi导出excel压缩成zip格式代码
OutputStream out = response.getOutputStream();
ZipOutputStream zip = new ZipOutputStream(out);
ZipEntry entry = new ZipEntry("xx.xls");
zip.putNextEntry(entry);
workbook.write(zip);
zip.flush();
zip.close();
这样导出来是整个action 求教~!毕业论文
最后一句没加return;
public void printAva() throws Exception { String trade = httpServletRequest.getParameter("trade"); AvaCompanyForm avaCompany = avaService.getAvaCompanyForm(avaId,trade); if (avaCompany!=null) { String path = getWebInfPath(); HttpServletResponse response = ServletActionContext.getResponse(); // 创建一个excel HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个sheet HSSFSheet sheet = workbook.createSheet("sheet1"); //new CellRangeAddress(0, 1, 0, 7) 0,第一行是0,最后一行是1;第一列是0,最后一列是2 ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); String root=Constants.PROPERTYPARENT.get("resource.save.path");//得到根路径 String logo = avaCompany.getLogo(); HSSFPatriarch patri1 = sheet.createDrawingPatriarch(); if (logo!=null&&!"".equals(logo)&&!"undefined".equals(logo)) { logo = root+logo; BufferedImage companyLogo = ImageIO.read(new File(logo)); ImageIO.write(companyLogo,"PNG",byteArrayOut); // 设置图片的位置.开始位置1(竖),1(横) 占用格子5(横),4(竖) HSSFClientAnchor anchor1 = new HSSFClientAnchor(1,1,1,1,(short)1,1,(short)5,6); patri1.createPicture(anchor1 ,workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG)); } //创建 ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream(); // 设置图片的位置 HSSFClientAnchor anchor3 = new HSSFClientAnchor(0,0,1,1,(short)12,0,(short)15,8); //创建 HSSFPatriarch patri3 = sheet.createDrawingPatriarch(); patri1.createPicture(anchor3 ,workbook.addPicture(byteArrayOut2.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG)); printAvaLand(workbook, sheet, avaCompany, trade); OutputStream out = response.getOutputStream(); // // filename下载excel名 // response.setHeader("Content-disposition", // "attachment; filename=printAva_"+avaCompany.getId()+".xls"); // // 设置类型 // response.setContentType("application/msexcel;charset=UTF-8"); // // 设置头 // response.setHeader("Pragma", "No-cache"); // // 设置头 // response.setHeader("Cache-Control", "no-cache"); // // 设置日期头 // response.setDateHeader("Expires", 0); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String Ename = sdf.format(new Date()); ZipOutputStream zip = new ZipOutputStream(out); ZipEntry entry = new ZipEntry(Ename+".xls"); zip.putNextEntry(entry); workbook.write(zip); zip.flush(); zip.close(); } }