标签:javafx poi filechooser hssfworkbook excel
以下是JavaFX中导出Excel的核心代码:
private HSSFWorkbook workbook;
/* Build Operation Button Area */
Button exportBn = ButtonBuilder.create().text("导出Excel").prefWidth(80).prefHeight(30).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记录");
// Set the table for 15 byte 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("房间号"));
//Create second column cell
HSSFCell cell2 = row.createCell(1);
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
cell2.setCellValue(new HSSFRichTextString("状态"));
//Create thrid column cell
HSSFCell cell3 = row.createCell(2);
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setCellValue(new HSSFRichTextString("客人姓名"));
//Create four column cell
HSSFCell cell4 = row.createCell(3);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setCellValue("提交时间");
//Create five column cell
HSSFCell cell5 = row.createCell(4);
cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
cell5.setCellValue(new HSSFRichTextString("已读?"));
//Create six column cell
HSSFCell cell6 = row.createCell(5);
cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
cell6.setCellValue("处理时间");
//Create seven column cell
HSSFCell cell7 = row.createCell(6);
cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
cell7.setCellValue(new HSSFRichTextString("备注"));
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);
}
/**
* 输入EXCEL文件
*
* @param fileName
* 文件名
*/
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<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;
}
}
}
JavaFX学习之道:FileChooser 、POI导出Excel文件,布布扣,bubuko.com
JavaFX学习之道:FileChooser 、POI导出Excel文件
标签:javafx poi filechooser hssfworkbook excel
原文地址:http://blog.csdn.net/u011511429/article/details/38487431