Excel导出

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);

	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值