Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java EasyExcel不弹出下载excel文件界面_Java - Fatal编程技术网

Java EasyExcel不弹出下载excel文件界面

Java EasyExcel不弹出下载excel文件界面,java,Java,我正在使用导出excel文件 implementation group: 'com.alibaba', name: 'easyexcel', version: '2.2.3' 这是我最简单的代码演示,控制器如下: @Api @RequestMapping("/illidan/report/game") @FeignClient(name = "soa-illidan-service") public interface IGameRecordController { /**

我正在使用导出excel文件

implementation group: 'com.alibaba', name: 'easyexcel', version: '2.2.3'
这是我最简单的代码演示,控制器如下:

@Api
@RequestMapping("/illidan/report/game")
@FeignClient(name = "soa-illidan-service")
public interface IGameRecordController {

    /**
     * @return
     */
    @GetMapping(value = "/export")
    void export(HttpServletResponse httpServletResponse) throws IOException;
}
这就是实现:

 @Override
    public void export(HttpServletResponse response) throws IOException {
        EasyExcel.write(response.getOutputStream(), DemoData.class).sheet("bala").doWrite(data());
    }

    private List<DemoData> data() {
        List<DemoData> list = new ArrayList<DemoData>();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("bala" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }
它不会在我的浏览器中弹出下载界面并返回401。我正在推导代码,它在服务器成功且无错误输出的情况下运行。我该怎么做才能让它工作?这是DemoData类定义:

@Data
public class DemoData {
    @ExcelProperty("string")
    private String string;
    @ExcelProperty("date")
    private Date date;
    @ExcelProperty("number")
    private Double doubleData;

    @ExcelIgnore
    private String ignore;
}

在阅读github项目时,我找到了下载此文件的方法

在编写excel之前,您需要设置一些值作为响应

@GetMapping("download")
public void download(HttpServletResponse response) throws IOException {
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode("bala", "UTF-8");
    response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    EasyExcel.write(response.getOutputStream(), DemoData.class).sheet("bala").doWrite(data());
}

在这段代码中,您认为您正在启动下载?您需要使用
HttpServletResponse
,这是您的用户将得到的响应。@Dolphin当您尝试下载文件时得到401时,我看到了您的评论。在调用任何url之前,您是否使用spring secutiry或其他需要自动验证的东西?代码可能会成功进入代码中,我可以成功调试到代码中,我已关闭身份验证。@Dolphin您是否在使用日志?您是否可以设置跟踪的日志记录级别,以查看此401错误http的日志错误?如果您达到de代码,这意味着服务器授权了您的请求,但http进程中的某些内容未授权返回此资源。我们需要日志错误来查看发生这种情况的位置和原因。
@GetMapping("download")
public void download(HttpServletResponse response) throws IOException {
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode("bala", "UTF-8");
    response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    EasyExcel.write(response.getOutputStream(), DemoData.class).sheet("bala").doWrite(data());
}