JavaFX: FileChooser and POI export Excel files
The following is the core code for exporting Excel files in JavaFX:
Private HSSFWorkbook workbook;
/* Build Operation Button Area */
Button exportBn = ButtonBuilder. create (). text ("Export Excel"). prefWidth (80). prefHeight (30). build ();
ExportBn. setDefaultButton (true );
ExportBn. setOnAction (new EventHandler (){
@ Override
Public void handle (ActionEvent event ){
UpdateQueryResultAllRecords ();
FileChooser fileChooser = new FileChooser ();
FileChooser. setTitle ("LaundryService ");
FileChooser. setInitialFileName ("laundryrecords.xls ");
FileChooser. getExtensionFilters (). add (new FileChooser. ExtensionFilter ("XLS Files", "*. xls "));
File file = fileChooser. showSaveDialog (mRecordDetailStage );
If (file! = Null ){
ExportExcel (file. getAbsolutePath ());
}
}
});
Private void exportExcel (String fileName ){
// Declare a work sheet
Workbook = new HSSFWorkbook ();
// Generate a form
HSSFSheet sheet = workbook. createSheet ("LaundryService record ");
// Set the table for 15 byte default column width
Sheet. setdefacolumcolumnwidth (short) 15 );
// Create a style
HSSFCellStyle style = workbook. createCellStyle ();
// The style settings
Style. setFillForegroundColor (HSSFColor. SKY_BLUE.index );
Style. setFillPattern (HSSFCellStyle. SOLID_FOREGROUND );
Style. setBorderBottom (HSSFCellStyle. BORDER_THIN );
Style. setBorderLeft (HSSFCellStyle. BORDER_THIN );
Style. setBorderRight (HSSFCellStyle. BORDER_THIN );
Style. setBorderTop (HSSFCellStyle. BORDER_THIN );
Style. setAlignment (HSSFCellStyle. ALIGN_CENTER );
// Create a font
HSSFFont font = workbook. createFont ();
Font. setColor (HSSFColor. VIOLET. index );
Font. setFontHeightInPoints (short) 12 );
Font. setBoldweight (HSSFFont. BOLDWEIGHT_BOLD );
// The font applied to the current style
Style. setFont (font );
// Create first row
HSSFRow row = sheet. createRow (0 );
// Create first column cell
HSSFCell cell1 = row. createCell (0 );
Cell1.setCellType (HSSFCell. CELL_TYPE_STRING );
Cell1.setCellValue (new HSSFRichTextString ("room number "));
// Create second column cell
HSSFCell cell2 = row. createCell (1 );
Cell2.setCellType (HSSFCell. CELL_TYPE_STRING );
Cell2.setCellValue (new HSSFRichTextString ("state "));
// Create thrid column cell
HSSFCell cell3 = row. createCell (2 );
Cell3.setCellType (HSSFCell. CELL_TYPE_STRING );
Cell3.setCellValue (new HSSFRichTextString ("guest name "));
// Create four column cell
HSSFCell cell4 = row. createCell (3 );
Cell4.setCellType (HSSFCell. CELL_TYPE_STRING );
Cell4.setCellValue ("submission time ");
// Create five column cell
HSSFCell cell5 = row. createCell (4 );
Cell5.setCellType (HSSFCell. CELL_TYPE_STRING );
Cell5.setCellValue (new HSSFRichTextString ("read? "));
// Create six column cell
HSSFCell cell6 = row. createCell (5 );
Cell6.setCellType (HSSFCell. CELL_TYPE_STRING );
Cell6.setCellValue ("Processing Time ");
// Create seven column cell
HSSFCell cell7 = row. createCell (6 );
Cell7.setCellType (HSSFCell. CELL_TYPE_STRING );
Cell7.setCellValue (new HSSFRichTextString ("Remarks "));
For (int I = 0; I <mResultAllRecords. size (); I ++ ){
HSSFRow datarow = sheet. createRow (I + 1 );
RecordInfo m = mResultAllRecords. get (I );
HSSFCell datacell1 = datarow. createCell (0 );
Datacell1.setCellType (HSSFCell. CELL_TYPE_STRING );
Datacell1.setCellValue (m. getRoomNo ());
// Create second column cell
HSSFCell datacell2 = datarow. createCell (1 );
Datacell2.setCellType (HSSFCell. CELL_TYPE_STRING );
Datacell2.setCellValue (m. getStatus ());
// Create thrid column cell
HSSFCell datacell3 = datarow. createCell (2 );
Datacell3.setCellType (HSSFCell. CELL_TYPE_STRING );
Datacell3.setCellValue (m. getSubscriberName ());
// Create four column cell
HSSFCell datacell4 = datarow. createCell (3 );
Datacell4.setCellType (HSSFCell. CELL_TYPE_STRING );
Datacell4.setCellValue (m. getSubmitTime ());
// Create five column cell
HSSFCell datacell5 = datarow. createCell (4 );
Datacell5.setCellType (HSSFCell. CELL_TYPE_STRING );
Datacell5.setCellValue (m. getIsReaded ());
// Create six column cell
HSSFCell datacell6 = datarow. createCell (5 );
Datacell6.setCellType (HSSFCell. CELL_TYPE_STRING );
Datacell6.setCellValue (m. getProcessTime ());
// Create seven column cell
HSSFCell datacell7 = datarow. createCell (6 );
Datacell7.setCellType (HSSFCell. CELL_TYPE_STRING );
Datacell7.setCellValue (m. getComments ());
}
OutputExcel (fileName );
}
/**
* Input an EXCEL file
*
* @ Param fileName
* File name
*/
Public void outputExcel (String fileName ){
FileOutputStream fos = null;
Try {
Fos = new FileOutputStream (new File (fileName ));
Workbook. write (fos );
Fos. close ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
Private void updateQueryResultAllRecords (){
Synchronized (mResultAllRecords ){
WorkHandler worker = new WorkHandler ();
ArrayList RecordList = worker. queryAllLaundryRecordByCondition (mLastQueryStartDate,
MLastQueryEndDate, mLastQueryDstRoomNo, mLastQueryStatus,
MLastQueryDstReadFlag, mLastQueryComments, mLastQueryGuestName,
MLastQueryOffset, 0, mLastQueryTotal );
MSwapResultRecordList. clear ();
MResultAllRecords. clear ();
If (recordList! = Null ){
MSwapResultRecordList. addAll (recordList );
For (int n = 0; n <mSwapResultRecordList. size (); n ++ ){
MResultAllRecords. add (new RecordInfo (mSwapResultRecordList. get (n )));
}
RecordList = null;
}
}
}