將數據導出爲excel

public void exportXls(){
        HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        ServletOutputStream servletOutputStream;
        try {
            servletOutputStream = httpServletResponse.getOutputStream();
        } catch (IOException e) {
            procException(e, "獲取導出文件HttpServletResponse異常");
            return;
        }
        try {
            httpServletResponse.setHeader("Content-disposition","attachment; filename=" +  new String("商品房網籤統計".getBytes("gb2312"), "ISO8859-1") + ".xls");
        } catch (UnsupportedEncodingException e) {
            procException(e, "設置ResponseHeader異常,無法設置文件名");
            return;
        }
        httpServletResponse.setContentType("application/vnd.ms-excel");
        
        // 聲明一個工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一個表格
        HSSFSheet sheet = workbook.createSheet("信息統計");
        // 設置列寬
        sheet.setColumnWidth(0, 2000);
        sheet.setColumnWidth(1, 5000);
        sheet.setColumnWidth(2, 8000);
        sheet.setColumnWidth(3, 4000);
        sheet.setColumnWidth(4, 4000);
        sheet.setColumnWidth(5, 4000);
        sheet.setColumnWidth(6, 4000);
        sheet.setColumnWidth(7, 4000);
        sheet.setColumnWidth(8, 4000);
        sheet.setColumnWidth(9, 4000);
        sheet.setColumnWidth(10, 4000);
        
        // 生成一個樣式(表頭樣式)
        HSSFCellStyle style = workbook.createCellStyle();
        // 設置這些樣式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 生成一個字體
        HSSFFont font = workbook.createFont();
//                font.setColor(HSSFColor.VIOLET.index);
//                font.setFontHeightInPoints((short) 10);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        // 把字體應用到第一行標題的樣式
        style.setFont(font);
        
        // 生成並設置另一個樣式
        HSSFCellStyle style2 = workbook.createCellStyle();
        // 設置字體自動換行
        style2.setWrapText(true);
        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 生成另一個字體
        HSSFFont font2 = workbook.createFont();
        font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        // 把字體應用到數據的樣式
        style2.setFont(font2);
        
        HSSFRow row = sheet.createRow(0);
        // 生成表頭
        String[] header = new String[]{"序號","合同編號","小區","購房人","共有人","樓房號","主房面積","附屬房屋","附房面積","面積合計","總房款"};
        for (int i = 0; i < header.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(style);
            HSSFRichTextString text = new HSSFRichTextString(header[i]);
            cell.setCellValue(text);
        }
        //這個地方是獲取jsf的表格內容,要是用的別的前臺框架,這個地方換成相對應的數據就好
        List<Map<String, Object>> list =  (List<Map<String, Object>>) getDataTable().getWrappedData();
        
        int cicle = 1;
        for (int i = 0; i < list.size(); i++) {
            // 在表頭下邊 開始創建行並加入值
            Map<String, Object> map = list.get(i);
            row = sheet.createRow(cicle);
            row.setRowStyle(style);
            // 序號
            HSSFCell cell = row.createCell(0);
            cell.setCellValue(new HSSFRichTextString(i + 1 + ""));
            cell.setCellStyle(style2);
            // 合同編號
            String busNum = map.get("busNum") == null ? "" : map.get("busNum").toString();
            cell = row.createCell(1);
            cell.setCellValue(new HSSFRichTextString(busNum));
            cell.setCellStyle(style2);
            // 小區
            String areaName = map.get("areaName") == null ? "" : map.get("areaName").toString();
            cell = row.createCell(2);
            cell.setCellValue(new HSSFRichTextString(areaName));
            cell.setCellStyle(style2);
            //購房人
            String name = map.get("name") == null ? "" : map.get("name").toString();
            cell = row.createCell(3);
            cell.setCellValue(new HSSFRichTextString(name));
            cell.setCellStyle(style2);
            //共有人
            String share = map.get("share") == null ? "" : map.get("share").toString();
            cell = row.createCell(4);
            cell.setCellValue(new HSSFRichTextString(share));
            cell.setCellStyle(style2);
            //樓房號
            String floorNum = map.get("floorNum") == null ? "" : map.get("floorNum").toString();
            cell = row.createCell(5);
            cell.setCellValue(new HSSFRichTextString(floorNum));
            cell.setCellStyle(style2);
            //主房面積
            String mainArea = map.get("mainArea") == null ? "" : map.get("mainArea").toString();
            cell = row.createCell(6);
            cell.setCellValue(new HSSFRichTextString(mainArea));
            cell.setCellStyle(style2);
            //附屬房屋
            String subHouse = map.get("subHouse") == null ? "" : map.get("subHouse").toString();
            cell = row.createCell(7);
            cell.setCellValue(new HSSFRichTextString(subHouse));
            cell.setCellStyle(style2);
            //附房面積
            String subarea = map.get("subarea") == null ? "" : map.get("subarea").toString();
            cell = row.createCell(8);
            cell.setCellValue(new HSSFRichTextString(subarea));
            cell.setCellStyle(style2);
            //面積合計
            String account = map.get("account") == null ? "" : map.get("account").toString();
            cell = row.createCell(9);
            cell.setCellValue(new HSSFRichTextString(account));
            cell.setCellStyle(style2);
            //總房款
            String totalAmount = map.get("totalAmount") == null ? "" : map.get("totalAmount").toString();
            cell = row.createCell(10);
            cell.setCellValue(new HSSFRichTextString(totalAmount));
            cell.setCellStyle(style2);
            cicle++;
        }
        try {
            workbook.write(servletOutputStream);
            //告訴瀏覽器已經完成
            FacesContext.getCurrentInstance().responseComplete();
        } catch (IOException e) {
            procException(e, "文件流寫入異常");
        }finally {
            if(servletOutputStream != null){
                try {
                    servletOutputStream.close();
                } catch (IOException e) {
                    procException(e, "關閉servletOutputStream異常");
                }
            }
        }  
        
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章