2012-01-07 10 views
0

私はデータベースからデータを取得し、excelで表示するために取り組んでいます。私はApache poiライブラリを使用してExcelを生成しています。Apache Poi - Algo

ここでは、Excelを生成するスニペットを示します。 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」)の条件がありますを取得する方法を助ける必要がある

。はい、このセルの下にc15.setCellValue( "Action")、特定のアクションが記述されます。


ID日付時間YES/NOアクション


はい/いいえデータがデータベース(sampleDao.getAllData();)から来ていないはいが存在する場合、それから私は、特定のアクションを記述しています2番目のデータをフェッチした後、前のYes/Noをチェックし、両方が同じか変更されている場合は、それに応じてActionを書き込む必要があります。

は誰かが条件とループのために書き込みスニペットに私に

  1. を助けることができます。
  2. とセルにデータを設定する方法。

答えて

2

あなたがあまりにも多くのことを書いてしまったとき、なぜそのループを埋めるのが難しいのか理解できません。

ここアルゴリズム/擬似コードは、セルにデータを設定するには...

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++; 
} 

です:

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); 
のようにセルにデータを設定することができます