java - 使用java POI打印excel中的数据

标签 java mysql excel apache-poi

我正在从 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();

非常感谢。

最佳答案

为了让您的代码包含操作 1 到 4,您需要循环访问数据(使用 rs = stmt.executeQuery(actionsData); 获取的数据)并相应地编写它。

在这种情况下,我建议使用以下准则重构您的代码:

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

关于java - 使用java POI打印excel中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745895/

相关文章:

java - 将非常大的整数转换为位 vector 失败

mysql - 在 UNION 之后引用别名进行计算

vba - 如何查看 if 语句中正在比较的内容

excel - 如果在 Excel 中,则为空白单元格

Android 应用程序的 Java 服务器

java - 如何使用 Google Drive API v3 Java 的服务帐户访问 Team Drive

java - 实现接口(interface)的类和那个接口(interface)之间有什么关系吗?

php - 从准备好的语句函数返回变量 PHP

php - 与多个较小的 SQL 相比,使用连接执行单个 SQL SELECT 是否更有效?

具有多个条件的Excel聚合公式