2013-10-29 125 views
4
package efms.bizobj; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.List; 
import java.util.Random; 

import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 

import efms.db.bean.PsatMetricsDrillDown2ActionList; 
import efms.db.bean.PsatMetricsDrillDown2Bean; 
import efms.log.EfmsLogger; 

public class DrillDown2ExcelReport { 

    public void generateHeader1(HSSFWorkbook wb, HSSFSheet sheet, String name,List<PsatMetricsDrillDown2Bean> records) { 
     EfmsLogger.debug("Inside generateHeader1"); 
     HSSFRow row = sheet.createRow(0); 
     HSSFCell cell; 

     int count = 14; 
     row = sheet.createRow(0); 


     cell = row.createCell(0); 
     cell.setCellValue("USO Number"); 
     cell = row.createCell(1); 
     cell.setCellValue("CLCI"); 
     cell = row.createCell(2); 
     cell.setCellValue("CLO"); 
     cell = row.createCell(3); 
     cell.setCellValue("OCO CLLI"); 
     cell = row.createCell(4); 
     cell.setCellValue("Service Type"); 
     cell = row.createCell(5); 
     cell.setCellValue("Order Action"); 
     cell = row.createCell(6); 
     cell.setCellValue("Order Status"); 
     cell = row.createCell(7); 
     cell.setCellValue("Test Section"); 
     cell = row.createCell(8); 
     cell.setCellValue("Test Status"); 
     cell = row.createCell(9); 
     cell.setCellValue("Condition Code and Desc"); 
     cell = row.createCell(10); 
     cell.setCellValue("Details on Failure Reason"); 
     cell = row.createCell(11); 
     cell.setCellValue("CKL Number"); 
     cell = row.createCell(12); 
     cell.setCellValue("Test Triggered Date/Time"); 
     cell = row.createCell(13); 
     cell.setCellValue("Trigger"); 
     cell = row.createCell(14); 
     cell.setCellValue("Trigger Name"); 

     for(int i =0 ;i <= count ;i ++) 
      sheet.autoSizeColumn(i); 
    } 

    public void insertReportData(HSSFWorkbook wb, HSSFSheet sheet, String name,List<PsatMetricsDrillDown2Bean> records,String failType) { 
     HSSFRow row ; 
     HSSFCell cell; 

     String clci ; 
     String clo; 
     int orderType; 
     String usoNumber ; 
     int svcOrderId ; 
     String serviceType; 
     String ocoClli ; 
     String orderAction; 
     String orderStatus; 
     String testStatus ; 
     String conditionCode ; 
     String conditionCodeDesc ; 
     String cklNum ; 
     String triggerDateTime ; 
     String trigger ; 
     String triggerName ; 


     for(int i=0 ; i < records.size() ;i ++){ 

      clci =" "; 
      clo =" "; 
      orderType = -1; 
      usoNumber = " "; 
      svcOrderId = -1; 
      serviceType = " "; 
      ocoClli = " "; 
      orderAction = " "; 
      orderStatus = " "; 
      testStatus = " "; 
      conditionCode = " "; 
      conditionCodeDesc = " "; 
      cklNum = " " ; 
      triggerDateTime = " "; 
      trigger = " "; 
      triggerName = " "; 


      if(records.get(i).getClci() != null) 
       clci = records.get(i).getClci();  

      if(records.get(i).getClo() != null) 
       clo = records.get(i).getClo(); 

      if(records.get(i).getOrderType() != -1) 
       orderType = records.get(i).getOrderType(); 

      if(records.get(i).getUsoNumber() != null) 
       usoNumber = records.get(i).getUsoNumber(); 

      if(records.get(i).getSvcOrderId() != -1) 
       svcOrderId = records.get(i).getSvcOrderId(); 

      if(records.get(i).getServiceType() != null) 
       serviceType = records.get(i).getServiceType(); 

      if(records.get(i).getOcoClli() != null) 
       ocoClli = records.get(i).getOcoClli(); 

      if(records.get(i).getOrderAction() != null) 
       orderAction = records.get(i).getOrderAction(); 

      if(records.get(i).getOrderStatus() != null) 
       orderStatus = records.get(i).getOrderStatus(); 

      if(records.get(i).getTestStatus() != null) 
       testStatus = records.get(i).getTestStatus(); 

      if(records.get(i).getConditionCode() != null) 
       conditionCode = "("+ records.get(i).getConditionCode()+")"; 

      if(records.get(i).getConditionCodeDescription() != null) 
       conditionCodeDesc = records.get(i).getConditionCodeDescription(); 

      if(records.get(i).getCklNumber() != null) 
       cklNum = records.get(i).getCklNumber(); 

      if(records.get(i).getTrigDateTime() != null) 
       triggerDateTime = records.get(i).getTrigDateTime(); 

      if(records.get(i).getTrigger() != null) 
       trigger = records.get(i).getTrigger(); 

      if(records.get(i).getTriggerName() != null) 
       triggerName = records.get(i).getTriggerName(); 

      row = sheet.createRow(i+1); 

      cell = row.createCell(0); 
      cell.setCellValue(usoNumber); 
      sheet.autoSizeColumn(0); 

      cell = row.createCell(1); 
      cell.setCellValue(clci); 
      sheet.autoSizeColumn(1); 

      cell = row.createCell(2); 
      cell.setCellValue(clo); 
      sheet.autoSizeColumn(2); 

      cell = row.createCell(3); 
      cell.setCellValue(ocoClli); 
      sheet.autoSizeColumn(3);    


      cell = row.createCell(4); 
      cell.setCellValue(serviceType); 
      sheet.autoSizeColumn(4); 


      cell = row.createCell(5); 
      cell.setCellValue(orderAction); 
      sheet.autoSizeColumn(5); 

      cell = row.createCell(6); 
      cell.setCellValue(orderStatus); 
      sheet.autoSizeColumn(6); 

      cell = row.createCell(7); 
      cell.setCellValue(failType); 
      sheet.autoSizeColumn(7); 

      cell = row.createCell(8); 
      cell.setCellValue(testStatus); 
      sheet.autoSizeColumn(8); 


      cell = row.createCell(9); 
      cell.setCellValue(conditionCode + conditionCodeDesc); 
      sheet.autoSizeColumn(9); 

      if(records.get(i).getActionList()!=null){ 
       for(PsatMetricsDrillDown2ActionList p: records.get(i).getActionList()) { 
       cell = row.createCell(10); 
       cell.setCellValue(p.getActionName() + " - " + p.getActionReason()); 
       sheet.autoSizeColumn(10); 
       } 
     }else{ 
      cell = row.createCell(10); 
      cell.setCellValue(" "); 
      sheet.autoSizeColumn(10); 
     } 

      cell = row.createCell(11); 
      cell.setCellValue(cklNum); 
      sheet.autoSizeColumn(11); 

      cell = row.createCell(12); 
      cell.setCellValue(triggerDateTime); 
      sheet.autoSizeColumn(12); 

      cell = row.createCell(13); 
      cell.setCellValue(trigger); 
      sheet.autoSizeColumn(13); 

      cell = row.createCell(14); 
      cell.setCellValue(triggerName); 
      sheet.autoSizeColumn(14); 


    } 
    } 


     public String generateDD2ExcelReport(List<PsatMetricsDrillDown2Bean> records,String failType){ 
      HSSFWorkbook wb = new HSSFWorkbook(); 
      HSSFSheet dd2ExcelSheet = wb.createSheet("DrillDown2 Report"); 
      generateHeader1(wb,dd2ExcelSheet,"DrillDown2 Report",records); 
      insertReportData(wb,dd2ExcelSheet,"DrillDown2 Report",records,failType); 

      FileOutputStream fileOut; 
      Random rand = new Random(); 
      String fileName = "";   
      String fileLocation = "" ; 
      boolean fileExists = true; 

      try { 


      do{ 
       fileName = "DrillDown2" + rand.nextInt(10000000)+"Report.xls"; 
       fileLocation = "/opt/www/data/psat_report" + File.separator + fileName; 
       fileExists = new File(fileLocation).exists(); 

      }while(fileExists); 


      fileOut = new FileOutputStream(fileLocation); 
      String path = new File(fileLocation).getAbsolutePath(); 

      wb.write(fileOut); 
      fileOut.close(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
      return fileName; 
     } 


} 

此代碼用於生成Excel報表。有15列,如果記錄數量大約爲1000,則需要將近10-15分鐘來生成Excel表格。上述代碼中有沒有問題?如何提高性能(縮短Excel報表生成時間)?Apache POI花費很長時間才能生成Excel表格

+0

可能重複的[一個POI相關代碼塊運行死慢](http://stackoverflow.com/questions/18984785/a-poi-related-code-block-running-dead-slow) – rgettman

回答

9

sheet.autoSizeColumn()方法可能需要一些時間 - 並且在添加到工作表的每一條數據之後調用它。嘗試刪除所有調用,並在代碼的最後執行自動調整 - 每個列只需執行一次。

+0

謝謝傑克,它會幫助我很多,再次感謝+1。 –

+0

** autoSizeColumn()**也是我的情況的罪魁禍首。感謝精彩的精神。爲你+1 – arunsankarkk

相關問題