java
复制代码
/**
* @param response 响应
* @param fileName 文件名称
* @param sheetName sheet名称
* @param headNameList 头部名称
* @param <T>
* @throws IOException
*/
public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<String> headNameList) throws IOException {
OutputStream out = null;
try {
// response.setContentType("application/vnd.ms-excel");
response.setContentType("text/csv");
response.setCharacterEncoding(CharEncoding.UTF_8);
fileName = URLEncoder.encode(fileName, CharEncoding.UTF_8);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".csv");
out = response.getOutputStream();
EasyExcelFactory.write(out).excelType(ExcelTypeEnum.CSV).sheet(sheetName).head(EasyExcelUtil.headList(headNameList)).doWrite(new ArrayList<>());
} catch (Exception e) {
e.printStackTrace();
} finally {
IoUtil.close(out);
}
}
}