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;

    }





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章