実戦クエリーデータ生成excelレポートファイル

3117 ワード

次のコードを参照してください.後でゆっくりと文章を整理します.
chargeStatDTO.getBetsDataList():データベースでクエリーされたデータ(自分のニーズに合わせて変更可能)
 
// excel 
IExcelAPI excelService = new ExcelAPIImpl();
filePath = excelService.genExcelFile(chargeStatDTO.getBetsDataList(),SltDataConstants.SLT_CHARGE_EXCEL_COLUMNS);

 
 
ExcelAPIImpl.java
 
public String genExcelFile(List chargeDataList,int columns) throws BizException{
	if(chargeDataList == null || chargeDataList.isEmpty()){
		throw new BizException("##  excel :  ");
	}
		
	//  EXCEL 
	String[][] content = null; //  , EXCEL 
	String fileName = null; //  
	SltUserCharge sltUserCharge = null;
	int rows = chargeDataList.size() + 1;
	logger.info("**  : " + rows);

	content = new String[rows][columns];
	content[0][0] = " ID";
	content[0][1] = " ";
	content[0][2] = " ";
	content[0][3] = " ";

	 int i = 1;
	 for(Iterator it = chargeDataList.iterator();it.hasNext();) {
	        sltUserCharge = (SltUserCharge) it.next();
	        content[i][0] = String.valueOf(sltUserCharge.getUserid().longValue()); 
	        content[i][1] = sltUserCharge.getChannelName();
	        content[i][2] = sltUserCharge.getCenterName();
	        content[i][3] = String.valueOf(sltUserCharge.getTotalChargeMoney().doubleValue());
	        
	        i++;
	    }
	    
	    // excel 
	    fileName = SltDataConstants.EXCEL_FILE_ADDR;
	    

	    //  EXCEL 
	    String now = sdf.format(new Date());
	    //String time = now.substring(0, 8);
	    
	    String filePath = null;
	    if (content != null && fileName != null) {
	    	ExcelProcessor excel = ExcelProcessor.getInstance();
	        if (content.length <= EXCEL_MAX_ROW) {
	        	filePath = fileName + now + ".xls";
	        	logger.info("EXCEL :" + filePath);
	          
	        	if(!excel.write2Excel(filePath, content)) {
	        		logger.error(" EXCEL (" + sdf.format(now) + "):" + filePath);
	        	}
	        	
	        }else {
	          int size = content.length;
	          int archives = size / EXCEL_MAX_ROW;
	          int start = 1;
	          int end = 1;
	          
	          for (int j = 0; j <= archives; j++) {
	            int num = j + 1;
	            filePath = fileName + now + "_" + num + ".xls";
	            logger.info("EXCEL :" + filePath);
	            
	            start = end;
	            end = start + (EXCEL_MAX_ROW - 1);
	            if (end >= size) {
	              end = size;
	            }
	            if (!excel.write2Excel(filePath, content, start, end)) {
	            	logger.error(" EXCEL (" + sdf.format(now) + "):" + filePath);
	            }
	            
	          }
	        }
	      }
	    
	    logger.info("** [ excel!]");
	    
	    String fileRS = filePath.replaceAll(SltDataConstants.FILE_DIR_ADDR, "");
	    
	    logger.info("** filePath = " + fileRS);
	    return fileRS;
	}

 
以上のコードは難しくないので、あまり説明しませんでした.
皆さんは何か問題があったら交流してください.