2012-01-07 3 views
0

데이터베이스에서 데이터를 가져와 Excel에 표시하려고합니다. Apache poi 라이브러리를 사용하여 Excel을 생성했습니다.Apache Poi - Algo

여기에 엑셀을 생성하는 스 니펫이 있습니다. DAO

public void setSampleDao(
       SampleDao sampleDao) { 
      this.sampleDao = sampleDao; 
     } 

     public SampleDao getSampleDao() { 
      return sampleDao; 
     } 

    } 


sampleDao.getAllData(); 

getAllData() 메소드

public class GenerateReport { 

    public void showReport() { 

    List<T> t = sampleDao.getAllData(); 


    HSSFWorkbook workbook = new HSSFWorkbook(); 
    HSSFSheet sheet = workbook.createSheet(); 
    HSSFRow row  = null; 
     HSSFCell column = null; 

     HSSFRow row1 = sheet.createRow(1); 

     HSSFCell c11 = row1.createCell(1); 
     HSSFCell c12 = row1.createCell(2); 
     HSSFCell c13 = row1.createCell(3); 
     HSSFCell c14 = row1.createCell(4); 
     HSSFCell c15 = row1.createCell(5); 

     c11.setCellValue("ID"); 
     c12.setCellValue("Date"); 
     c13.setCellValue("Time"); 
     c14.setCellValue("YES/NO"); 
     c15.setCellValue("Action"); 



for (final T sampleT: t) { 

      ....algo and what should be written 

     } 


    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    ExternalContext externalContext = facesContext.getExternalContext(); 
    externalContext.setResponseContentType("application/vnd.ms-excel"); 
    externalContext.setResponseHeader("Content-Disposition", 
       "attachment; filename=\"Sample Report"); 
    workbook.write(externalContext.getResponseOutputStream()); 
    facesContext.responseComplete(); 

    } 
    } 

가 // 게터 세터 쿼리를 통해 표시해야 할 데이터를 반환합니다.

나는 데이터를 얻을하고 무엇 조건이있다 루프

for (final T sampleT: t) { 

       ....algo and what should be written 

      } 

이 작성되어야하는 도움을 필요로하는 c14.setCellValue ("YES/NO")의 경우; Yes이면 셀 c15.setCellValue ("Action") 아래에 특정 작업이 기록됩니다.


ID 날짜 시간 YES/NO 액션


예/아니오 데이터가 데이터베이스 (sampleDao.getAllData();)에서 나지 않는 예는 존재하는 경우, 그럼 난 특정 작업을 작성해야합니다 액션 열에서 두 번째 데이터를 가져온 후 이전 예/아니요를 확인하고 둘 다 동일하거나 변경된 경우 이에 따라 액션을 작성해야합니다.

누군가는 조건 루프에 서면으로 조각 나에게

  1. 를 할 수 있습니다.
  2. 및 셀에 데이터를 설정하는 방법

답변

2

너무 많이 작성하면 루프를 채우는 것이 왜 어려운지 이해하지 못합니다.

HSSFRow row = sheet.getRow(rowIdx); 
HSSFCell cell = row.getCell(colIdx); 
//if its a string.. otherwise choose the correct cell type 
cell.setCellType(HSSFCell..CELL_TYPE_STRING); 
cell.setCellValue(new HSSFRichTextString(data)); 
0

당신이

`

Cell cell = row.getCell(excelColNum); 
      if (cell == null) { 
    cell = row.createCell(excelColNum); 
} 
cell.setCellType(Cell.CELL_TYPE_STRING); 
cell.setCellValue(cellValue); 
같은 셀에 데이터를 설정할 수 있습니다 여기에

String previousYesNo=null": 
int rowcounter=2; 
for (final T sampleT: t) { 
    create hssf row with rowcounter 
    create hssf cell for the 5 columns 
    set data to all the columns from the sampleT object 
    String yesNo=sampleT.getYesNO(); 
    if(previousYesNo!=null && prviousYesNo.equals(yesNo)){ 
    set action cell value to something 
    }else{ 
    set action cell value to something else 
    } 
    previousYesNo=yesNo; 
    rowCounter++; 
} 

세포의 데이터를 설정하려면 ... 알고리즘/의사 코드입니다

'