私はデータベースからデータを取得し、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を書き込む必要があります。
は誰かが条件とループのために書き込みスニペットに私に
- を助けることができます。
- とセルにデータを設定する方法。