public void generateReport() throws IOException { System.out.println("Test Report Gen"); Sheet sheet = workbook.getSheet("sheet1"); try { String strSql = "Select * from participant;"; Sql db = new Sql(); ResultSet rs = db.executeQuery(strSql); ResultSetMetaData rsMeta = rs.getMetaData(); Row row = sheet.createRow(0); Cell cell; // get table columns which will act as title columns for (int i = 1; i <= rsMeta.getColumnCount(); i++) { String colName = rsMeta.getColumnName(i); int dataType = rsMeta.getColumnType(i); cell = row.createCell(i - 1); cell.setCellValue(colName); } int i = 1; while (rs.next()) { // create row to write data to row = sheet.createRow(i); // create cells for writing data to for (int c = 1; c <= rsMeta.getColumnCount(); c++) { cell = row.createCell(c - 1); String colName = rsMeta.getColumnName(c); cell.setCellValue(rs.getString(colName)); } i++; } JOptionPane.showMessageDialog(null, "Excel Report of All Participants Generated"); } catch (SQLException ex) { ex.printStackTrace(); // log Error FacesFingerPrintProject.logger.log(Level.SEVERE, "ERROR", ex); } /// Write the output to a file FileOutputStream fileOut = new FileOutputStream("reportTest.xls"); workbook.write(fileOut); fileOut.close(); }
/** Execute the query and set the result. */ public void run() throws IOException { ResultSet queryResult = null; queryResult = Sql.executeQuery(scon, this, sqlName, parameters, withMetaData); setResult(queryResult); }