2016-08-03 17 views
0

SQLテーブルからデータを取得しています。 SQLテーブルの行はIDに依存しますので、ここではアクション行は固定されていません。 xxxx行は固定されています(1行)。私は以下Java POIを使用してExcelでデータを印刷

Column1  Column2    Column3    Column4   Column5 
NAME  completedWorkflows runningWorkflows failedWorkflows cancelledWorkflows 
xxxx  2233    1312    123    1232 

ONE BLANK ROW(In below table Rows are not fixed it may change depends on data) 

NAME  completedWorkflows runningWorkflows failedWorkflows cancelledWorkflows 
Action 1 12365    54545    55    788 
Action 2 54545    88     88    4 
Action 3 97     123     2    87 
Action 4 788     24     24    274 

をExcelファイルにこのフォーマットで出力を印刷したい は私のコードです。 XXXX行の値を出力します。アクション1〜4をコードしていない。そのためにはあなたの助けが必要です。上記の出力を得るには、どこで、どのステートメントを追加すればよいですか? TIA

stmt = conn.createStatement(); 
String completedWorkflows = "Some Query"; 
String runningWorkflows = "Some Query"; 
String failedWorkflows = "Some Query"; 
String cancelledWorkflows = "Some Query"; 
String cancellingWorkflows = "Some Query"; 
String actionsData = "Some Query"; 

      ResultSet rs = stmt.executeQuery(completedWorkflows); 
      rs.next(); 
      int totalCompletedWF = rs.getInt("COMPLETED_WF"); 

      rs = stmt.executeQuery(runningWorkflows); 
      rs.next(); 
      int totalRunningWF = rs.getInt("RUNNING_WF"); 

      rs = stmt.executeQuery(failedWorkflows); 
      rs.next(); 
      int totalFailedWF = rs.getInt("FAILED_WF"); 

      rs = stmt.executeQuery(cancelledWorkflows); 
      rs.next(); 
      int totalCancelleddWF = rs.getInt("CANCELLED_WF"); 

      rs = stmt.executeQuery(cancellingWorkflows); 
      rs.next(); 
      int totalCancellingdWF = rs.getInt("CANCELLING_WF"); 

      // Fetching Action data. THIS QUERY RETURNS DYNAMIC NUMBER OF ROWS WITH DETAILS 
      rs = stmt.executeQuery(actionsData); 
      rs.next(); 
      String actionName = rs.getString("NAME"); 
      int actionWaiting = rs.getInt("WAITING"); 
      int actionRunning = rs.getInt("RUNNING"); 
      int actionFailed = rs.getInt("FAILED"); 
      int actionCancelled = rs.getInt("CANCELLED"); 
      int actionCompleted = rs.getInt("COMPLETED"); 

      // Excel file generation code 
      HSSFWorkbook workbook = new HSSFWorkbook(); 
      HSSFSheet sheet = workbook.createSheet("Results"); 

      CellStyle style = workbook.createCellStyle(); 
      Font font = workbook.createFont(); 
      font.setFontHeightInPoints((short) 11); 
      font.setFontName(HSSFFont.FONT_ARIAL); 
      font.setBoldweight(HSSFFont.COLOR_NORMAL); 
      font.setBold(true); 
      font.setColor(HSSFColor.BLACK.index); 

      style.setFont(font); 
      style.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex()); 
      style.setFillPattern(CellStyle.SOLID_FOREGROUND); 
      style.setAlignment(style.ALIGN_JUSTIFY); 
      style.setBorderBottom(style.BORDER_THIN); 
      style.setBorderLeft(style.BORDER_THIN); 
      style.setBorderTop(style.BORDER_THIN); 
      style.setWrapText(true); 
      style.setVerticalAlignment(CellStyle.ALIGN_CENTER); 

      HSSFRow row = sheet.createRow(1); 
      HSSFRow rowhead = sheet.createRow((short) 0); 
      rowhead.setRowStyle(style); 

      HSSFCell cell1 = rowhead.createCell(1); 
      cell1.setCellStyle(style); 
      cell1.setCellValue("Completed Workflows"); 
      row.createCell(1).setCellValue(totalCompletedWF); 

      HSSFCell cell2 = rowhead.createCell(2); 
      cell2.setCellStyle(style); 
      cell2.setCellValue("Running Workflows"); 
      row.createCell(2).setCellValue(totalRunningWF); 

      HSSFCell cell3 = rowhead.createCell(3); 
      cell3.setCellStyle(style); 
      cell3.setCellValue("Failed Workflows"); 
      row.createCell(3).setCellValue(totalFailedWF); 

      HSSFCell cell4 = rowhead.createCell(4); 
      cell4.setCellStyle(style); 
      cell4.setCellValue("Cancelled Workflows"); 
      row.createCell(4).setCellValue(totalCancelleddWF); 

      HSSFCell cell5 = rowhead.createCell(5); 
      cell5.setCellStyle(style); 
      cell5.setCellValue("Cancelling Workflows"); 
      row.createCell(5).setCellValue(totalCancellingdWF); 

      sheet.autoSizeColumn(0); 
      sheet.autoSizeColumn(1); 
      sheet.autoSizeColumn(2); 
      sheet.autoSizeColumn(3); 
      sheet.autoSizeColumn(4); 
      sheet.autoSizeColumn(5); 

      // Action results set to Excel sheet 

      FileOutputStream fileOut = new FileOutputStream(fileLocation + "\\Results.xls"); 
      workbook.write(fileOut); 
      fileOut.close(); 

      rs.close(); 
      stmt.close(); 
      conn.close(); 

ありがとうございました。

答えて

1

コードにアクション1〜4を含めるには、データ(ループをrs = stmt.executeQuery(actionsData);とする)をループし、それに応じて書き込む必要があります。この場合

、私は、次のガイドラインを使用してコードをリファクタリングお勧め:

  1. は、ワークブックを作成します。
  2. シートを作成します。
  3. データを取得します。
  4. データをループします(最後のレコードに到達するまで)。
    • あなたのデータをループしながら、次の操作を行います。
      • シートに新しい行を作成します。
      • 作成した行のセルにデータを入力します。
      • セルにスタイルを適用します。
  5. ファイルに変更を書き留めます。
関連する問題