2016-08-03 24 views
0

Ich hole Daten aus SQL-Tabelle. Zeilen der SQL-Tabelle hängen von der ID ab, daher sind Aktionszeilen hier nicht festgelegt und können variieren. wo als xxxx Zeile ist fest (einzelne Zeile). Ich möchte Ausgabe in Excel in diesem Format druckenDrucken von Daten in Excel mit Java POI

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 

Datei ist unter meinem Code. Druckt Werte von XXXX Zeile. Code nicht für Aktionen 1 ... 4 haben. Dafür brauche deine Hilfe. Wo und welche Aussagen sollte ich hinzufügen, um über die Ausgabe zu kommen? 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(); 

Vielen Dank.

Antwort

1

Damit Ihr Code Aktion 1 bis 4 enthält, müssen Sie Ihre Daten (die mit rs = stmt.executeQuery(actionsData); erstellt wurden) durchlaufen und entsprechend schreiben.

In diesem Fall empfiehlt Refactoring ich Ihren Code unter Beachtung folgender Richtlinien:

  1. die Arbeitsmappe erstellen.
  2. Erstellen Sie das Blatt.
  3. Holen Sie Ihre Daten.
  4. Durchlaufen Sie Ihre Daten (bis Sie den letzten Datensatz erreichen).
    • Während durch Ihre Daten Looping, gehen Sie wie folgt vor:
      • auf dem Blatt eine neue Zeile erstellen.
      • Füllen Sie die Zellen in der erstellten Zeile mit Ihren Daten.
      • Wenden Sie Ihren Stil auf die Zelle an.
  5. Ihre Änderungen an der Datei schreiben.