批量导出

Posted yplq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量导出相关的知识,希望对你有一定的参考价值。

前言:

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

    }

以上是关于批量导出的主要内容,如果未能解决你的问题,请参考以下文章

Thinkphp excel批量导出-分表导出

ArcGIS批量导出栅格影像的属性表

java批量从svn导出多个项目

oracle数据库可否批量导出存储过程

C# | CAD批量导出多段线节点坐标(附源代码下载)

java导出大批量(百万以上)数据的excel文件