java 实现导出数据到 excel

代码如下:

/**
     * 账单明细导出
     * @param request
     * @param response
     * @throws Exception
     */
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String[] heard = {"序号","业务号","收付登记号","保单号码","业务类型","付款人","险种","币别","金额",
                "缴费信息截止日期","起保生效日期","核保完成日期","业务归属机构","处理人代码"};
        String[] xuhao = request.getParameterValues("xuhao");
        String[] gpPayFeeInfoDtoBusinessNo = request.getParameterValues("gpPayFeeInfoDtoBusinessNo");
        String[] gpPayFeeInfoDtoPaymentNo = request.getParameterValues("gpPayFeeInfoDtoPaymentNo");
        String[] gpPayFeeInfoDtoPolicyNo = request.getParameterValues("gpPayFeeInfoDtoPolicyNo");
        String[] gpPayFeeInfoDtoBusinessType = request.getParameterValues("gpPayFeeInfoDtoBusinessType");
        String[] gpPayFeeInfoDtoPayeeName = request.getParameterValues("gpPayFeeInfoDtoPayeeName");
        String[] gpPayFeeInfoDtoRiskCode = request.getParameterValues("gpPayFeeInfoDtoRiskCode");
        String[] gpPayFeeInfoDtoCurrency = request.getParameterValues("gpPayFeeInfoDtoCurrency");
        String[] gpPayFeeInfoDtoPayFee = request.getParameterValues("gpPayFeeInfoDtoPayFee");
        String[] gpPayFeeInfoDtoPayFeeDueDate = request.getParameterValues("gpPayFeeInfoDtoPayFeeDueDate");
        String[] gpPayFeeInfoDtoStartDate = request.getParameterValues("gpPayFeeInfoDtoStartDate");
        String[] gpPayFeeInfoDtoUnderwriteEndDate = request.getParameterValues("gpPayFeeInfoDtoUnderwriteEndDate");
        String[] gpPayFeeInfoDtoCompanyCode = request.getParameterValues("gpPayFeeInfoDtoCompanyCode");
        String[] gpPayFeeInfoDtoDealerCode = request.getParameterValues("gpPayFeeInfoDtoDealerCode");
        //导出
        // 第一步,创建一个webbook,对应一个Excel文件    
        HSSFWorkbook wb = new HSSFWorkbook();    
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet    
        HSSFSheet sheet = wb.createSheet("sheet1");    
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short    
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = null;
        //设置样式
        row.setHeight((short) 400);
        HSSFCellStyle style = wb.createCellStyle();
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中  
        //设置字体  
        HSSFFont font = wb.createFont();  
        font.setFontHeightInPoints((short) 10);//设置字体大小  
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体粗细  
        style.setFont(font);//选择需要用到的字体格式    
        //设置列宽
        sheet.setColumnWidth((short) 1, (short) 6500);
        sheet.setColumnWidth((short) 2, (short) 6000);
        sheet.setColumnWidth((short) 3, (short) 6000);
        sheet.setColumnWidth((short) 9, (short) 4500);
        sheet.setColumnWidth((short) 10, (short) 4000);
        sheet.setColumnWidth((short) 11, (short) 4000);
        sheet.setColumnWidth((short) 12, (short) 4000);
        sheet.setColumnWidth((short) 13, (short) 4000);

        //设置标题
        for(int i=0;i<heard.length;i++){
            cell = row.createCell((short) i);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(heard[i]);  
        }

        //设置内容
        for(int i=0;i<xuhao.length;i++){
            row = sheet.createRow(i+1);
            row.createCell((short) 0).setCellValue(xuhao[i]);
            row.createCell((short) 1).setCellValue(gpPayFeeInfoDtoBusinessNo[i]);
            row.createCell((short) 2).setCellValue(gpPayFeeInfoDtoPaymentNo[i]);
            row.createCell((short) 3).setCellValue(gpPayFeeInfoDtoPolicyNo[i]);
            cell = row.createCell((short) 4);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue("1".equals(gpPayFeeInfoDtoBusinessType[i])?"投保":"批改申请" );
            cell = row.createCell((short) 5);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(gpPayFeeInfoDtoPayeeName[i]);
            row.createCell((short) 6).setCellValue(gpPayFeeInfoDtoRiskCode[i]);
            row.createCell((short) 7).setCellValue(gpPayFeeInfoDtoCurrency[i]);
            row.createCell((short) 8).setCellValue(gpPayFeeInfoDtoPayFee[i]);
            row.createCell((short) 9).setCellValue(gpPayFeeInfoDtoPayFeeDueDate[i]);
            row.createCell((short) 10).setCellValue(gpPayFeeInfoDtoStartDate[i]);
            row.createCell((short) 11).setCellValue(gpPayFeeInfoDtoUnderwriteEndDate[i]);
            row.createCell((short) 12).setCellValue(gpPayFeeInfoDtoCompanyCode[i]);
            row.createCell((short) 13).setCellValue(gpPayFeeInfoDtoDealerCode[i]);
        }
         //初始化流  
        ByteArrayOutputStream os = new ByteArrayOutputStream();  
        BufferedInputStream bis = null;  
        BufferedOutputStream bos = null;  
        try {  
            //表格定稿数据  
            wb.write(os);  
            byte[] content = os.toByteArray();  
            InputStream is = new ByteArrayInputStream(content);  
            // 设置response参数,可以打开下载页面  
            response.reset();  
            response.setContentType("application/vnd.ms-excel;charset=utf-8");  
            response.setHeader("Content-Disposition", "attachment;filename="+ new String(("交易信息明细.xls").getBytes(), "iso-8859-1"));  
            ServletOutputStream out = response.getOutputStream();  
            bis = new BufferedInputStream(is);  
            bos = new BufferedOutputStream(out);  
            byte[] buff = new byte[2048];  
            int bytesRead;  
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
                bos.write(buff, 0, bytesRead);  
            }  
        } catch (Exception e) {  
            logger.info("文件下载失败");  
            e.printStackTrace();  
        } finally {  
            //关闭流  
            if (bis != null)  
                bis.close();  
            if (bos != null)  
                bos.close();  
            if (os != null)  
                os.close();  
        }  

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值