JavaFX学习之道:FileChooser 、POI导出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文件

时间: 2024-12-21 11:41:22

JavaFX学习之道:FileChooser 、POI导出Excel文件的相关文章

POI导出excel文件样式

需求: 公司业务和银行挂钩,各种形式的数据之间交互性比较强,这就涉及到了存储形式之间的转换 比如数据库数据与excel文件之间的转换 解决: 我目前使用过的是POI转换数据库和文件之间的数据,下边上代码 package org.triber.portal.model.area; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; import java.io.*; import ja

简单的poi导出excel文件

1 import java.io.FileOutputStream; 2 import java.io.IOException; 3 import java.util.Calendar; 4 import java.util.List; 5 6 import org.apache.poi.hssf.usermodel.HSSFCell; 7 import org.apache.poi.hssf.usermodel.HSSFRow; 8 import org.apache.poi.hssf.use

java poi 导出Excel文件

1,导包  poi-3.9-XXX.JAR 2, 创建一个实体对象 public class Student implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int id; private String name; private int age; private Date borth; public Student(int id, String name, i

Springboot使用POI实现导出Excel文件示例

Springboot使用POI实现导出Excel文件示例 前面讲述了使用POI导出Word文件和读取Excel文件,这两个例子都相对简单,接下来要讲述的使用POI导出Excel文件要复杂得多,内容也会比较长.创建表头信息表头信息用于自动生成表头结构及排序public class ExcelHeader implements Comparable<ExcelHeader>{/**  * excel的标题名称  */private String title;/**  * 每一个标题的顺序  */p

Excel导出学习之道:Java Web利用POI导出Excel简单例子

采用Spring mvc架构: Controller层代码如下 [java] view plaincopy @Controller public class StudentExportController{ @Autowired private StudentExportService studentExportService; @RequestMapping(value = "/excel/export") public void exportExcel(HttpServletReq

POI导出EXCEL经典实现

在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.而数据导出的格式一般是EXCEL或者PDF,我这里就用两篇文章分别给大家介绍下.(注意,我们这里说的数据导出可不是数据库中的数据导出!么误会啦^_^) 呵呵,首先我们来导出EXCEL格式的文件吧.现在主流的操作Excel文件的开源工具有很多,用得比较多的就是Apache的POI及JExcelAPI.这里我们用Apache POI!我们先去Apach

POI导出EXCEL经典实现(转)

http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html 1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI (POI for .NET) 来存取 POI 的功能. 2.POI结构 HSSF - 提供读写Microsoft Excel XLS格式档案的功能.XS

Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框

在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.而数据导出的格式一般是EXCEL或者PDF,我这里就用两篇文章分别给大家介绍下.(注意,我们这里说的数据导出可不是数据库中的数据导出!么误会啦^_^) 呵呵,首先我们来导出EXCEL格式的文件吧.现在主流的操作Excel文件的开源工具有很多,用得比较多的就是Apache的POI及JExcelAPI.这里我们用Apache POI!我们先去Apach

JavaFX学习之道:JavaFX API详解之Window,Stage,PopupWindow

stage包中包含 Window, Stage, PopupWindow, Popup, FileChooser, DirectoryChooser, Screen等类. 其中Window类可理解成一个窗体,用于存放Scene,并与用户操作.一般window作为窗体,都用其子类Stage和PopupWindow. 看一下Window作为窗体的顶级类包含的一些共同属性 eventDispatcher setEventDispatcher(EventDispatcher value) focused