jxl Or poi 壓縮導出 總結

poi:

 

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();
   //如果房源沒有設置主圖片,就拿上傳的圖片的第一張設爲該房源的圖片做顯示
   String img = avaCompany.getMainImageUrl();
   if (img!=null&&!"".equals(img)&&!"undefined".equals(img)) {
    img = root+img;
    BufferedImage companyImg = ImageIO.read(new File(img));
    ImageIO.write(companyImg,"PNG",byteArrayOut1);
    // 設置圖片的位置
    HSSFClientAnchor anchor2 = new HSSFClientAnchor(0,0,1,1,(short)1,10,(short)8,18);
    patri1.createPicture(anchor2 ,workbook.addPicture(byteArrayOut1.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
   }else if("undefined".equals(img)&&avaCompany.getImageUrl()!=null&&!"".equals(avaCompany.getImageUrl())) {
    int num = avaCompany.getImageUrl().indexOf(",");
    if (num<0) {
     img = root+avaCompany.getImageUrl();
    }else {
     String[] str = avaCompany.getImageUrl().split(",");
     img = root+str[0];
    }
    BufferedImage companyImg = ImageIO.read(new File(img));
    ImageIO.write(companyImg,"PNG",byteArrayOut1);
    // 設置圖片的位置
    HSSFClientAnchor anchor2 = new HSSFClientAnchor(0,0,1,1,(short)1,10,(short)8,18);
    patri1.createPicture(anchor2 ,workbook.addPicture(byteArrayOut1.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
   }
   //創建
   ByteArrayOutputStream byteArrayOut2 = new ByteArrayOutputStream();
   String img2 ="";
   if ("2".equals(trade)) {
    img2 = path+"/images/sell.gif";
   }else if("1".equals(trade)){
    img2 = path+"/images/rent.gif";
   }
   BufferedImage companyImg2 = ImageIO.read(new File(img2));
   ImageIO.write(companyImg2,"PNG",byteArrayOut2);
   // 設置圖片的位置
   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);//填充數據
   
   
   SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
   String Ename = sdf.format(new Date());
   String outputFile=Ename+".xls";
   
   OutputStream out = response.getOutputStream();
//   // filename下載excel名
   response.setContentType( "application/octet-stream "); 
   response.setHeader("Content-Disposition","attachment;filename=\""+outputFile+".zip"+"\"");  
//   // 設置類型
//   response.setContentType("application/msexcel;charset=UTF-8");
//   // 設置頭
//   response.setHeader("Pragma", "No-cache");
//   // 設置頭
//   response.setHeader("Cache-Control", "no-cache");
//   // 設置日期頭
//   response.setDateHeader("Expires", 0);
   
   ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(out));
   
   ZipEntry entry = new ZipEntry(outputFile);
   zip.putNextEntry(entry);

   workbook.write(zip);
   zip.closeEntry();
   zip.flush();
   zip.close();
   response.flushBuffer(); 
   response.setStatus(HttpServletResponse.SC_OK); 
   out.close();
  }
 }

jxl:

public void expSimpReport() throws Exception{
  HttpSession session =getSession();
  Broker broker =(Broker) session.getAttribute("nowUser");
  
  ServletOutputStream out = httpServletResponse.getOutputStream();
  httpServletResponse.reset();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  String fileName = sdf.format(new Date());
//  String fileName = "detail_Promotion_report";
  httpServletResponse.setContentType("application/octet-stream");
  httpServletResponse.setHeader("Content-Disposition","attachment;filename=\""+fileName+".zip"+"\"");
  
  ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(out));
  ZipEntry entry = new ZipEntry(fileName+".xls");
  zout.putNextEntry(entry);
  WorkbookSettings ws = new WorkbookSettings();

  WritableWorkbook book = Workbook.createWorkbook(new BufferedOutputStream(zout),ws);
 // fileName = new String(fileName.getBytes(), "UTF-8");//解決中文 文件名問題
  httpServletResponse.setContentType("application/x-msdownload");
  httpServletResponse.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
//  WritableWorkbook book = Workbook.createWorkbook(out);
  // 創建第一個sheet--第一行
  WritableSheet sheet = book.createSheet("簡略推廣報告", 0);
  sheet.addCell(new Label(0,0,"合作統計"));
  sheet.addCell(new Label(1,0,"看房次數"));
  sheet.addCell(new Label(2,0,"公衆瀏覽"));
  sheet.addCell(new Label(3,0,"經紀人瀏覽"));
  sheet.addCell(new Label(4,0,"出價統計"));
  sheet.addCell(new Label(5,0,"最高價"));
  sheet.addCell(new Label(6,0,"最低價"));
  sheet.addCell(new Label(7,0,"平均"));
  sheet.addCell(new Label(8,0,"電話推廣"));
  sheet.addCell(new Label(9,0,"短信推廣"));
  sheet.addCell(new Label(10,0,"郵件推廣"));
  sheet.setColumnView(10,20);
  
  //填充數據
  report = promotionService.queryPromotionReportById(avaId,broker);
  sheet.addCell(new Label(0,1,String.valueOf(report.getCooperationCount()))); //合作統計
  sheet.addCell(new Label(1,1,String.valueOf(report.getLookCount())));      //看房次數
  sheet.addCell(new Label(2,1,String.valueOf(report.getHitCount())));       //公衆瀏覽
  sheet.addCell(new Label(3,1,String.valueOf(report.getBrokerLookCount())));  //經紀人瀏覽
  sheet.addCell(new Label(4,1,String.valueOf(report.getBidCount())));         //出價統計
  sheet.addCell(new Label(5,1,String.valueOf(report.getBestBid())));         //最高出價(萬元)
  sheet.addCell(new Label(6,1,String.valueOf(report.getLowestPrice())));       //最低出價(萬元)
  sheet.addCell(new Label(7,1,String.valueOf(report.getAvgPrice())));         //平均價
  sheet.addCell(new Label(8,1,String.valueOf(report.getPhoneCount())));         //電話推廣次數
  sheet.addCell(new Label(9,1,String.valueOf(report.getSmsCount())));         //短信推廣次數
  sheet.addCell(new Label(10,1,String.valueOf(report.getEmailCount())));         //郵箱推廣次數
  
  book.write();
  if(book!=null){
   book.close();
  }
  
  zout.closeEntry();
  zout.flush();
  zout.close();
  httpServletResponse.flushBuffer();
  httpServletResponse.setStatus(HttpServletResponse.SC_OK);
  
  if(out!=null){
   out.close();
  }
 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章