使用java POI在Excel中打印数据

我从SQL表中获取数据。 sql表的行依赖于ID,所以这里的Action行不固定,可能会有所不同。 其中xxxx行是固定的(单行)。 我想在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 

以下是我的代码。 打印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(); 

非常感谢。

为了让你的代码包含Action 1到4,你需要遍历你的数据(使用rs = stmt.executeQuery(actionsData); )来获取并相应地写入。

在这种情况下,我build议使用以下指导重构您的代码:

  1. 创build工作簿。
  2. 创build工作表。
  3. 获取您的数据。
  4. 循环你的数据(直到你到达最后一个logging)。
    • 在循环数据时,请执行以下操作:
      • 在工作表上创build一个新行。
      • 用您的数据填充创build的行上的单元格。
      • 在单元格上应用你的风格。
  5. 将您的更改写入文件。