1

I am using EasyExcel to export an excel file.

implementation group: 'com.alibaba', name: 'easyexcel', version: '2.2.3'

This is my simplest code demo, the controller like this:

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

    /**
     * @return
     */
    @GetMapping(value = "/export")
    void export(HttpServletResponse httpServletResponse) throws IOException;
}

this is the implemention:

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

but when I using this url to download the excel:

https://api.example.com/illidan-hub/illidan/report/game/export

it not pop up the download UI in my browser and return 401 Unauthorized. I am dedugging the code,and it runs in server success and no error output. What should I do to make it works? This is DemoData class define:

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

    @ExcelIgnore
    private String ignore;
}
Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • 2
    Where in this code do you think you're initiating a download? You need to [do something](https://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers) with `HttpServletResponse response`, which is the response your user will get. – Christopher Schneider May 21 '20 at 13:55

1 Answers1

1

Reading github project, I find a way to download this.

U need to set some values in response before write 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());
}
Eduardo Nobre
  • 200
  • 1
  • 13
  • @Dolphin i saw your comment, when u get 401 when try download the file. Are u using spring secutiry or other thing that need autenticate before call any url ? – Eduardo Nobre May 21 '20 at 16:23
  • 1
    The code could success going in the code,I can success debug into the code,I am closed the auth. – Dolphin May 21 '20 at 16:25
  • 1
    @Dolphin are you using something for logs? Can you set the logging level for trace, to see the log error for this 401 error http? If u reached de code, thats means the server authorized your request but something in http process not authorized the return of this resource. We need the log error for see where and why occurs this. – Eduardo Nobre May 21 '20 at 16:33