The following is the core code for exporting excel in JavaFX:
Private Hssfworkbook Workbook;
/* Build Operation Button Area */
Button exportbn = Buttonbuilder.create (). Text ("Export Excel"). Prefwidth. Prefheight (). build ();
Exportbn.setdefaultbutton (TRUE);
Exportbn.setonaction (New eventhandler<actionevent> () {
@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 all bytes default column width
Sheet.setdefaultcolumnwidth ((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);
}
/**
* Enter Excel File
*
* @param fileName
* File name
*/
public void Outputexcel (String fileName) {
FileOutputStream fos = null;
try {
FOS = new FileOutputStream (new File);
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<workhandler.laundryrecord> 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;
}
}
}