Excel导出

来源:互联网 发布:jdbc mysql原理 编辑:程序博客网 时间:2024/05/16 01:37

1.service层

  /**     * 导出Excel     * @param fileName     * @param titles     * @param contents     * @param resp     * @throws Exception     */    public void download_xls(String fileName, List<String> titles , List<PtRecord> contents,                             HttpServletResponse resp, HttpServletResponse response) throws Exception {        try {            /*********************浏览器弹出下载框***************************/            OutputStream out = resp.getOutputStream();            HSSFWorkbook workbook = new HSSFWorkbook();//创建exc文件            Date date=new Date();            DateFormat format=new SimpleDateFormat("yyyy-MM-dd");            String time=format.format(date);            response.setContentType("application/x-download");//下面三行是关键代码,处理乱码问题            response.setCharacterEncoding("utf-8");            response.setHeader("Content-Disposition", "attachment;fileName="+new String(fileName.getBytes("gbk"), "iso8859-1")+time+".xls");            Sheet sheet = workbook.createSheet("信息");            Row row = sheet.createRow(0);            Cell cell ;            for(int i=0; i<titles.size(); i++){ //设置标题                cell = row.createCell(i);                cell.setCellValue(titles.get(i));            }            for(int i=0;i<contents.size();i++) {                PtRecord ptRecord = contents.get(i);                row = sheet.createRow(i + 1);                // 合同备案编号                cell = row.createCell((short) 0);                if(ptRecord.getRecordCode() == null){                    cell.setCellValue("--");                }else {                    cell.setCellValue(ptRecord.getRecordCode());                }                // 成交方式                cell = row.createCell((short) 1);                if(ptRecord.getHousePublishType() == null){                    cell.setCellValue("--");                }else {                    cell.setCellValue(ptRecord.getHousePublishType().getName());                }                // 申请人                cell = row.createCell((short) 2);                if(ptRecord.getRecordApplication() == null){                    cell.setCellValue("--");                }else {                    cell.setCellValue(ptRecord.getRecordCode());                }                // 申请时间                cell = row.createCell((short) 3);                if(ptRecord.getApplicationAt() == null){                    cell.setCellValue("--");                }else {                    cell.setCellValue(ptRecord.getApplicationAt());                }                // 房源类型                cell = row.createCell((short) 4);                if(ptRecord.getHouseType() == null){                    cell.setCellValue("--");                }else {                    cell.setCellValue(ptRecord.getHouseType().getName());                }                //  购房合同号                cell = row.createCell((short) 5);                if(ptRecord.getContractNumber() == null){                    cell.setCellValue("--");                }else {                    cell.setCellValue(ptRecord.getContractNumber());                }                //  产权证(不动产证件号)                cell = row.createCell((short) 6);                if(ptRecord.getPropertyNumber() == null){                    cell.setCellValue("--");                }else {                    cell.setCellValue(ptRecord.getPropertyNumber());                }                // 房源编号                cell = row.createCell((short) 7);                if(ptRecord.getHouseCode() == null){                    cell.setCellValue("--");                }else {                    cell.setCellValue(ptRecord.getHouseCode());                }            }            workbook.write(out);            out.close();        }        catch (Exception e){        }    }
2.action层

@At("/excel")@Ok("void")public void excel(HttpServletResponse resp, HttpServletRequest request, HttpServletResponse response) throws Exception {List<String> titles = new ArrayList<String>();List<PtRecord> listContexts = bphcPtRecordService.fetchAll();String fileName="合同备案管理";titles.add("合同备案编号");titles.add("成交方式");titles.add("申请人");titles.add("申请时间");titles.add("房源类型");titles.add("购房合同号");titles.add("产权证(不动产证件号)");titles.add("房源编号");bphcPtRecordService.download_xls(fileName,titles,listContexts,resp,response);}



0 0