JAVA中导出excel的公用类的方法怎么调用?

1.我想导出前端查询后的数据,然后将数据导出到excel中,在网上查到有一个公用的方法,我创建了,但是我不会调用。请各位大神帮助下,谢谢了。刚开始学习java的小白。
2.疑问:controller(这个不会写)怎么调用这个方法呢?
3.环境:Java后端+vue前端(前端过滤后的数据进行导出)

如下为controller,这个确实不会写,请各位大神帮助下。谢谢。

 @RequestMapping("/exportExcel")
 //@ApiOperation("数据导出excel")
@ResponseBody
    public R download(@RequestParam Map<String, Object> params){
        //exportSsExcel(String tempPath, String path, HttpServletResponse response, List<T> list, int count)
        //获取日期类型根据类型分发业务
        String energy_name = MapUtil.getStr(map,"tempPath");
        String to_build = MapUtil.getStr(map,"path");


    }

如下为共用类ExcelUtils中的导出方法。

public void exportSsExcel(String tempPath, String path, HttpServletResponse response, List<T> list, int count) {
        File newFile = createNewFile(tempPath, path);
        InputStream is = null;
        HSSFWorkbook workbook = null;
        HSSFSheet sheet = null;
        try {
            is = new FileInputStream(newFile);// 将excel文件转为输入流
            workbook = new HSSFWorkbook(is);// 创建个workbook,
            // 获取第一个sheet
            sheet = workbook.getSheetAt(0);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        if (sheet != null) {
            try {
                // 写数据
                FileOutputStream fos = new FileOutputStream(newFile);
                HSSFRow row = sheet.getRow(0);
                if (row == null) {
                    row = sheet.createRow(0);
                }
                HSSFCell cell = row.getCell(0);
                if (cell == null) {
                    cell = row.createCell(0);
                }
                //cell.setCellValue("我是标题");
                for (int i = 0; i < list.size(); i++) {
                    T vo1 = list.get(i);
                    row = sheet.createRow(i+count); //从第count行开始
                    //这里就可以使用sysUserMapper,做相应的操作
                    Field[] fields=vo1.getClass().getDeclaredFields();
                    for(int j=0;j<fields.length;j++){
                        String firstLetter = fields[j].getName().substring(0, 1).toUpperCase();
                        String getter = "get" + firstLetter + fields[j].getName().substring(1);
                        Method method = vo1.getClass().getMethod(getter, new Class[] {});
                        if(fields[j].getGenericType().toString().equals("class java.util.Date")){
                            Object value =method.invoke(vo1, new Object[] {});
                            if(value!=null){
                                Date date =(Date)value;
                                createRowAndCell(format.format(date), row, cell, j);
                            }else {
                                createRowAndCell(value, row, cell, j);
                            }
                        }else {
                            createRowAndCell(method.invoke(vo1, new Object[] {}), row, cell, j);
                        }
                    }
                    }
                workbook.write(fos);
                fos.flush();
                fos.close();

                // 下载
                InputStream fis = new BufferedInputStream(new FileInputStream(
                        newFile));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                response.reset();
                response.setContentType("text/html;charset=UTF-8");
                OutputStream toClient = new BufferedOutputStream(
                        response.getOutputStream());
                response.setContentType("application/x-msdownload");
                String newName = URLEncoder.encode(
                        "防火漏电" + System.currentTimeMillis() + ".xls",
                        "UTF-8");
                response.addHeader("Content-Disposition",
                        "attachment;filename=\"" + newName + "\"");
                response.addHeader("Content-Length", "" + newFile.length());
                toClient.write(buffer);
                toClient.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != is) {
                        is.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        // 删除创建的新文件
        this.deleteFile(newFile);
    }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问