JavaFX學習之道:FileChooser 、POI匯出Excel檔案

來源:互聯網
上載者:User

標籤: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;
            }
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.