将数据导出为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[]{"1","2","3","4","5","6","7","8","9","10","11"};
        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);
            // 1
            HSSFCell cell = row.createCell(0);
            cell.setCellValue(new HSSFRichTextString(i + 1 + ""));
            cell.setCellStyle(style2);
            // 2
            String busNum = map.get("busNum") == null ? "" : map.get("busNum").toString();
            cell = row.createCell(1);
            cell.setCellValue(new HSSFRichTextString(busNum));
            cell.setCellStyle(style2);
            // 3
            String areaName = map.get("areaName") == null ? "" : map.get("areaName").toString();
            cell = row.createCell(2);
            cell.setCellValue(new HSSFRichTextString(areaName));
            cell.setCellStyle(style2);
            //4
            String name = map.get("name") == null ? "" : map.get("name").toString();
            cell = row.createCell(3);
            cell.setCellValue(new HSSFRichTextString(name));
            cell.setCellStyle(style2);
            //5
            String share = map.get("share") == null ? "" : map.get("share").toString();
            cell = row.createCell(4);
            cell.setCellValue(new HSSFRichTextString(share));
            cell.setCellStyle(style2);
            //6
            String floorNum = map.get("floorNum") == null ? "" : map.get("floorNum").toString();
            cell = row.createCell(5);
            cell.setCellValue(new HSSFRichTextString(floorNum));
            cell.setCellStyle(style2);
            //7
            String mainArea = map.get("mainArea") == null ? "" : map.get("mainArea").toString();
            cell = row.createCell(6);
            cell.setCellValue(new HSSFRichTextString(mainArea));
            cell.setCellStyle(style2);
            //8
            String subHouse = map.get("subHouse") == null ? "" : map.get("subHouse").toString();
            cell = row.createCell(7);
            cell.setCellValue(new HSSFRichTextString(subHouse));
            cell.setCellStyle(style2);
            //9
            String subarea = map.get("subarea") == null ? "" : map.get("subarea").toString();
            cell = row.createCell(8);
            cell.setCellValue(new HSSFRichTextString(subarea));
            cell.setCellStyle(style2);
            //10
            String account = map.get("account") == null ? "" : map.get("account").toString();
            cell = row.createCell(9);
            cell.setCellValue(new HSSFRichTextString(account));
            cell.setCellStyle(style2);
            //11
            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异常");
                }
            }
        }  
        
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值