poi导入

springmvc

service层



    @Override
    public void exportDataArea(HttpServletResponse resp, List<DataArea> list)
            throws IOException {
        // TODO Auto-generated method stub
        OutputStream out = null;
        try {
            out = resp.getOutputStream();
            String exlName = "投放地域.xls";
            resp.setContentType("application/vnd.ms-excel;charset=UTF-8");
            resp.setHeader("Content-disposition", "attachment; filename="
                    + new String(exlName.getBytes("GBK"), "ISO8859-1"));
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("投放地域");
            HSSFRow row = sheet.createRow((int) 0);
            HSSFCellStyle headerStyle = wb.createCellStyle();
            Font font = wb.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            headerStyle.setFont(font);
            headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            String[] excelHeader = { "投放平台", "类别", "省份", "指数", "日期", "创建人",
                    "创建时间" };
            int[] excelHeaderWidth = { 150, 150, 50, 50, 150, 100, 150 };
            // 设置列宽
            for (int i = 0; i < excelHeaderWidth.length; i++) {
                sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
            }

            // 创建标头
            for (int i = 0; i < excelHeader.length; i++) {
                HSSFCell cell = row.createCell(i);
                cell.setCellValue(excelHeader[i]);
                cell.setCellStyle(headerStyle);
            }
            // 表内容设置样式
            for (int i = 0; i < excelHeaderWidth.length; i++) {

            }
            // 创建表内容
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                DataArea area = list.get(i);
                HSSFCell cell0 = row.createCell(0);
                cell0.setCellValue(area.getPlatformName());
                cell0.setCellStyle(style);
                HSSFCell cell1 = row.createCell(1);
                cell1.setCellValue(area.getCategoryName());
                cell1.setCellStyle(style);
                HSSFCell cell2 = row.createCell(2);
                cell2.setCellValue(area.getProvince());
                cell2.setCellStyle(style);
                HSSFCell cell3 = row.createCell(3);
                cell3.setCellValue(area.getIndexNum());
                cell3.setCellStyle(style);
                String dateStr = null;
                if(area.getDate()!=null){
                    dateStr = DateUtils.format(area.getDate(), "yyyy/MM/dd");
                }
                HSSFCell cell4 = row.createCell(4);
                cell4.setCellValue(dateStr);
                cell4.setCellStyle(style);
                HSSFCell cell5 = row.createCell(5);
                cell5.setCellValue(area.getCreatorName());
                cell5.setCellStyle(style);
                String createTimeStr = null;
                if(area.getCreateTime()!=null){
                    createTimeStr = DateUtils.format(area.getCreateTime(), "yyyy/MM/dd");
                }                
                HSSFCell cell6 = row.createCell(6);
                cell6.setCellValue(createTimeStr);
                cell6.setCellStyle(style);
            }
            wb.write(out);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }

    }


controller 层

调用时 方法返回值是String 返回 null

@RequestMapping("/exportExcel.jhtml")
    public String exportExcel(HttpServletRequest req, HttpServletResponse resp,
            HttpSession session, DataArea dataArea) {
        // 从session中获取投放平台map 查询时使用
        Map<Integer, String> dataPlatformMap = (Map<Integer, String>) session
                .getAttribute("platformMap");
        if (dataPlatformMap!=null && dataArea != null && dataArea.getPlatformName() != null) {
            for (Map.Entry<Integer, String> map : dataPlatformMap.entrySet()) {
                if (map.getValue().equals(dataArea.getPlatformName())) {
                    dataArea.setPlatformId(map.getKey());
                }
            }
        }
        // 从session中获取类别map 查询时使用
        Map<Integer, String> dataCategoryMap = (Map<Integer, String>) session
                .getAttribute("dataCategoryMap");
        if (dataCategoryMap!=null && dataArea != null && dataArea.getCategoryName() != null) {
            for (Map.Entry<Integer, String> map : dataCategoryMap.entrySet()) {
                if (map.getValue().equals(dataArea.getCategoryName())) {
                    dataArea.setCategoryId(map.getKey());
                }
            }
        }
        List<DataArea> list = dataAreaService.exportList(dataArea);
        try {
            dataAreaService.exportDataArea(resp, list);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值