码迷,mamicode.com
首页 > 其他好文 > 详细

批量导出

时间:2018-06-07 19:27:24      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:method   dex   oid   file   integer   puts   depend   tee   lis   

前言:

maven中需要添加依赖:

       <dependency>

            <groupId>jexcelapi</groupId>

            <artifactId>jxl</artifactId>

            <version>2.4.2</version>

       </dependency>

 public static void excleExport(OutputStream ouputStream, List<List<Object>> dataList) {
        try {
            WritableWorkbook book = Workbook.createWorkbook(ouputStream);
            WritableSheet sheet = null;
            //
            Integer column = 0;
            //
            Integer row = 0;

            // sheet
            Integer sheetIndex = 0;
            sheet = book.createSheet("sheet" + (sheetIndex + 1), sheetIndex);
            for (Integer index = 0; index < dataList.size(); index++) {

                row = index;

                for (Integer titleIndex = 0; titleIndex < dataList.get(index)
                        .size(); titleIndex++) {
                    String content = String.valueOf(dataList.get(index).get(titleIndex));
                    Label label = new Label(column, row, content);

                    sheet.addCell(label);
                    column++;
                }
                // 回归到第一列
                column = 0;
            }
            book.write();
            book.close();
            ouputStream.flush();
            ouputStream.close();
        } catch (IOException e) {

            e.printStackTrace();
        } catch (WriteException e) {

            e.printStackTrace();
        }


    }

使用:

controller层:

 @RequestMapping(value = "api/upload/excel/{type}", method = RequestMethod.GET)
    @ResponseBody
    public R LoadExcel(@PathVariable(value = "type") String type, HttpServletResponse response) {
        try {
            OutputStream outputStream = response.getOutputStream();
            //设置响应类型
            response.setContentType("application/vnd.ms-excel");
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddkkmmssSSS");
            String filename = "Report" + sf.format(new Date());
            //设置响应头
            response.setHeader("Content-disposition", "attachment;filename=" + filename + ".xls");
            byte[] bytes = tanantEMMService.exportExcel(type, outputStream);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return R.ok();
    }        

这一步最重要的就是设置响应类型和响应头,至于为啥。。。  因为我总是忘记咋设置!!!

 

 public static void excleExport(OutputStream ouputStream, List<List<Object>> dataList) {        try {            WritableWorkbook book = Workbook.createWorkbook(ouputStream);            WritableSheet sheet = null;            // 列            Integer column = 0;            // 行            Integer row = 0;
            // sheet            Integer sheetIndex = 0;            sheet = book.createSheet("sheet" + (sheetIndex + 1), sheetIndex);            for (Integer index = 0; index < dataList.size(); index++) {
                row = index;
                for (Integer titleIndex = 0; titleIndex < dataList.get(index)                        .size(); titleIndex++) {                    String content = String.valueOf(dataList.get(index).get(titleIndex));                    Label label = new Label(column, row, content);
                    sheet.addCell(label);                    column++;                }                // 回归到第一列                column = 0;            }            book.write();            book.close();            ouputStream.flush();            ouputStream.close();        } catch (IOException e) {
            e.printStackTrace();        } catch (WriteException e) {
            e.printStackTrace();        }

    }

批量导出

标签:method   dex   oid   file   integer   puts   depend   tee   lis   

原文地址:https://www.cnblogs.com/yplq/p/9151870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!