zoukankan      html  css  js  c++  java
  • 批量导出

    前言:

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

        }

  • 相关阅读:
    git rebase 还是 merge的使用场景最通俗的解释
    漏洞复现:Struts2 远程代码执行漏洞(S2-033)
    linux临时网络配置
    漏洞复现:Struts2 S2-032 漏洞环境
    XXE攻击学习
    启用了不安全的HTTP方法【转】
    HTTP参数污染【转】
    逻辑漏洞挖掘方式
    大漏洞时代下的僵尸网络追踪-笔记
    markdown入门杂记
  • 原文地址:https://www.cnblogs.com/yplq/p/9151870.html
Copyright © 2011-2022 走看看