Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么web导出excel 是zip格式 不是 xlsx #225

Closed
gjklop opened this issue Jan 25, 2019 · 6 comments
Closed

为什么web导出excel 是zip格式 不是 xlsx #225

gjklop opened this issue Jan 25, 2019 · 6 comments

Comments

@gjklop
Copy link

gjklop commented Jan 25, 2019

No description provided.

@ghostlwp
Copy link

image
image
代码这样的,导出是zip
image

@wapmnw
Copy link

wapmnw commented Feb 25, 2019

直接调接口会出现这种吗?如果是前端页面调你的接口出现的,可以看看是不是因为前端服务器有代理

@yuzhaocai
Copy link

出现同样的问题,请问解决了吗?

@jeremain
Copy link

参考#217把response的头部参数设置提前

@542869246
Copy link

这样就行。
`

@GetMapping(value = "/exportExcel")
public void exportExcel(HttpServletResponse response) throws Exception {
    PageData pd = this.getPageData();
    ExcelWriter writer = new ExcelWriter(getOutputStream("采购合同设备", response), ExcelTypeEnum.XLSX);
    Sheet sheet = new Sheet(1, 0, ThPurchaseBoxModel.class);
    sheet.setSheetName("sheet");
    sheet.setAutoWidth(Boolean.TRUE);
    List<ThPurchaseBoxModel> data = thPurchaseBoxService.selectPurchasesByPd4Excel(pd);
    writer.write(data, sheet);
    writer.finish();
}


/**
 * 导出文件时为Writer生成OutputStream.
 *
 * @param fileName 文件名
 * @param response response
 * @return ""
 */
public OutputStream getOutputStream(String fileName,
                                            HttpServletResponse response) throws Exception {
    try {
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "max-age=0");
        return response.getOutputStream();
    } catch (IOException e) {
        throw new Exception("导出excel表格失败!", e);
    }
}

`

@zhuangjiaju
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants