批量导出

前言:

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();        }

    }

猜你喜欢

转载自www.cnblogs.com/yplq/p/9151870.html