`
苹果超人
  • 浏览: 199870 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

导出excel文件

    博客分类:
  • java
阅读更多
@RequestMapping(value = "/exportUserSourceConsumeFundList", method = RequestMethod.GET)
    public void doExportUserSourceConsumeFundList(HttpServletResponse response, UserProfileVO userProfile, Integer exportType) throws Exception {

        LOGGER.info("用户消费金额导出:usersource/exportUserSourceConsumeFundList");

        if (null != userProfile.getBeginTime()) {
            userProfile.setBeginDateTime(DateUtil.parseDate(userProfile.getBeginTime()));
        }
        if (null != userProfile.getEndTime()) {
            userProfile.setEndDateTime(DateUtil.parseDate(userProfile.getEndTime()));
        }
        List<UserProfileVO> userSourceConsumeFundList = userProfileBO.exportUserSourceConsumeFundList(userProfile);

        String[] columns = {"账户用户名","用户真实姓名","手机号","消费金额","加金额","注册时间"};
        String[] dataFields = {"username","name","mobile","fundAmount","goldAmount","registerTime"};
        
        // 设置response参数,可以打开下载页面
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        
        ServletOutputStream out = response.getOutputStream();
        
        if (exportType == ExportFileType.XLS.getKey()) {
        	Workbook wb = new HSSFWorkbook();        	
        	response.setHeader("Content-Disposition", "attachment;filename=" + new String((EXPORT_NAME_USER_CONSUME_FUND + DateUtil.formatStandardDate(new Date()) + EXTENSION_XLS).getBytes(), "iso-8859-1"));
        	FileUtil.<UserProfileVO>exportExcel(out, columns, dataFields, userSourceConsumeFundList, wb, EXPORT_NAME_USER_CONSUME_FUND);
        } else if (exportType == ExportFileType.XLSX.getKey()) {
        	Workbook wb = new XSSFWorkbook();
        	response.setHeader("Content-Disposition", "attachment;filename=" + new String((EXPORT_NAME_USER_CONSUME_FUND + DateUtil.formatStandardDate(new Date()) + EXTENSION_XLSX).getBytes(), "iso-8859-1"));
            FileUtil.<UserProfileVO>exportExcel(out, columns, dataFields, userSourceConsumeFundList,wb, EXPORT_NAME_USER_CONSUME_FUND);
        } else if (exportType == ExportFileType.CSV.getKey()) {
        	response.setHeader("Content-Disposition", "attachment;filename=" + new String((EXPORT_NAME_USER_CONSUME_FUND + DateUtil.formatStandardDate(new Date()) + EXTENSION_CSV).getBytes(), "iso-8859-1"));
            FileUtil.<UserProfileVO>exportCsvFile(out, columns, dataFields, userSourceConsumeFundList, EXPORT_NAME_USER_CONSUME_FUND);
        }
    }


 public static <T> void exportExcel(OutputStream out,String[] columns, String[] dataFields, List<T> dataList, Workbook wb, String fileName) throws Exception {
        // 创建第一个sheet(页),并命名
        Sheet sheet = wb.createSheet(fileName);
        // 创建第一行
        Row row = sheet.createRow((short) 0);
        Cell cell = null;

        // 创建title
        for (int i = 0; i < columns.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(columns[i]);
        }

        for (int i = 0; i < dataList.size(); i++) {
            row = sheet.createRow(i + 1);
            T userTemp = dataList.get(i);
            HashMap<String, Object> userMap = HashMapUtil.objToHash(userTemp);

            for (int j = 0; j < dataFields.length; j++) {
                cell = row.createCell(j);
                String dataField = dataFields[j];
                Object value = userMap.get(dataField);
                if (value != null) {
                    cell.setCellValue(value.toString());
                } else {
                    cell.setCellValue("");
                }
            }
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);


        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);

            byte[] buff = new byte[2048];
            int bytesRead;

            // Simple read/write loop.
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }

        } catch (final IOException e) {
            e.getMessage();
        } finally {
            if (bis != null) {
                bis.close();
            }
            if (bos != null){
                bos.close();
            }
        }
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics