JavaFX 8 Tutorial 51 - Export Database to Excel
The code written in this tutorial is :
private Button exportToXL;
exportToXL = new Button("Export To Excel");
exportToXL.setFont(Font.font("Sanserif", 15));
exportToXL.setOnAction( e->{
try {
String query = "Select * from UserDatabase";
pst = conn.prepareStatement(query);
rs = pst.executeQuery();
//Apache POI Jar Link-
//http://a.mbbsindia.com/poi/release/bin/poi-bin-3.13-20150929.zip
XSSFWorkbook wb = new XSSFWorkbook();//for earlier version use HSSF
XSSFSheet sheet = wb.createSheet("User Details");
XSSFRow header = sheet.createRow(0);
header.createCell(0).setCellValue("ID");
header.createCell(1).setCellValue("First Name");
header.createCell(2).setCellValue("Last Name");
header.createCell(3).setCellValue("Email");
sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);
sheet.setColumnWidth(3, 256*25);//256-character width
sheet.setZoom(150);//scale-150%
int index = 1;
while(rs.next()){
XSSFRow row = sheet.createRow(index);
row.createCell(0).setCellValue(rs.getString("ID"));
row.createCell(1).setCellValue(rs.getString("FirstName"));
row.createCell(2).setCellValue(rs.getString("LastName"));
row.createCell(3).setCellValue(rs.getString("Email"));
index++;
}
FileOutputStream fileOut = new FileOutputStream("UserDetails.xlsx");// before 2007 version xls
wb.write(fileOut);
fileOut.close();
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("User Details Exported in Excel Sheet.");
alert.showAndWait();
pst.close();
rs.close();
} catch (SQLException | FileNotFoundException ex) {
Logger.getLogger(Tutorial14.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Tutorial14.class.getName()).log(Level.SEVERE, null, ex);
}
});
HBox hbox = new HBox(5);
hbox.getChildren().addAll( exportToXL);
Hi ram,
ReplyDeleteYour tutorials are very good . I want to know if we can make sheets in the same excel using the above traditional method.
My requirement is something like separate sheets for each class the teacher is handling..any possibility?? please let me know.