塔塔庙 2019-12-30 23:23 采纳率: 50%
浏览 177
已结题

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

  • 写回答

1条回答 默认 最新

  • 塔塔庙 2022-10-22 11:56
    关注

    没有人回答,我关闭了。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月22日

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题