Excel文件上传,解析,下载(三 创建文件和文件下载)

实现文件下载的功能

1.Workbook workBookSpare = spareFlowService.getExcelFile(context, header);获得文件

2.out = response.getOutputStream();获得输出流

3.workBookSpare.write(out);将文件写出到输出流当中

4.设置请求的头部信息

1 response.setCharacterEncoding("utf-8");

2 response.setContentType("multipart/form-data");

3 response.setHeader("Content-Disposition", "attachment;fileName=" 4 + fileName); 

4.1设置编码格式

4.2设置成文件传输模式

4.3设置下载的附件

5.最后别忘记关闭流

 1     @RequestMapping(params = "method=fileExport")
 2     public void fileExport(@RequestParam("header") String header,
 3             @RequestParam("context") String context,
 4             HttpServletResponse response) {
 5         Date date = new Date();
 6         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
 7         String str = sdf.format(date);
 8         String fileName = str+".xls";
 9         response.setCharacterEncoding("utf-8");
10         response.setContentType("multipart/form-data");
11         response.setHeader("Content-Disposition", "attachment;fileName="
12                 + fileName);
13         Workbook workBookSpare = spareFlowService.getExcelFile(context, header);
14         OutputStream out = null;
15         try {
16             out = response.getOutputStream();
17             workBookSpare.write(out);
18             out.flush();
19             out.close();
20         } catch (FileNotFoundException e) {
21             logger.debug(e.getMessage());
22         } catch (IOException e) {
23             logger.debug(e.getMessage());
24         }
25     }

 创建文件,里面使用的是面向接口的编程方式。详情,关注注释即可。

public Workbook createExcelFile(List<ResProperties> resProperties,
            List<List<String>> content) {
        // 创建显示的头部
        Workbook work = new HSSFWorkbook();
        Sheet sheet = work.createSheet();
        //第一行 表示的是列头
        createHeader(resProperties, sheet);
        //第二行 对应的列头在数据库当中的ID
        createHeaderIdHider(resProperties, sheet);
        createContent(content, sheet);
        return work;
        // 遍历显示的内容
    }

 

private Row createHeader(List<ResProperties> resProperties,Sheet sheet){
        Row rowHeaderExcel = sheet.createRow(SpareTransaction.HEADER_EXCEL);
        for(int i=0;i<resProperties.size();i++){
            //前面空出一列 用来表示ID
            Cell cellHeader = rowHeaderExcel.createCell(i+1);
            cellHeader.setCellValue(resProperties.get(i).getFieldDescription());
        }
        return rowHeaderExcel;
    }
private Row createHeaderIdHider(List<ResProperties> resProperties,Sheet sheet){
        Row rowHeaderExcel = sheet.createRow(SpareTransaction.HIDEER_ID_EXCEL);
        //隐藏
        rowHeaderExcel.setZeroHeight(true);
        for(int i=0;i<resProperties.size();i++){
            //前面空出一列 用来表示ID
            Cell cellHeader = rowHeaderExcel.createCell(i+1);
            cellHeader.setCellValue(resProperties.get(i).getPropertyId());
        }
        return rowHeaderExcel;
    }
private void createContent(List<List<String>> content, Sheet sheet) {
        for (int i = 0; i < content.size(); i++) {
            List<String> row = content.get(i);
            Row rowExcel = sheet
                    .createRow(i + SpareTransaction.HIDEER_ID_EXCEL+1);
            for (int k = 0; k < row.size(); k++) {
                Cell cell = rowExcel.createCell(k);
                cell.setCellValue(row.get(k));
            }
        }
    }

 

posted @ 2016-03-03 12:44  BinarySheep  阅读(763)  评论(0编辑  收藏  举报