[英]Print data in excel using java POI
我正在從SQL表中獲取數據。 sql表的行取決於ID,因此此處的操作行不固定,可能有所不同。 其中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);
)並進行相應的編寫。
在這種情況下,建議您遵循以下准則來重構代碼:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.