导出excel文件_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 导出excel文件

导出excel文件

 2014/6/30 16:17:36  苹果超人  程序员俱乐部  我要评论(0)
  • 摘要:@RequestMapping(value="/exportUserSourceConsumeFundList",method=RequestMethod.GET)publicvoiddoExportUserSourceConsumeFundList(HttpServletResponseresponse,UserProfileVOuserProfile,IntegerexportType)throwsException{LOGGER.info("用户消费金额导出
  • 标签:excel 文件 导出excel
class="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();
            }
        }
    }
发表评论
用户名: 匿名