ExcelUtils.java 909B

123456789101112131415161718192021222324252627
  1. package com.yunzhi.inte.common;
  2. import com.alibaba.excel.EasyExcel;
  3. import javax.servlet.http.HttpServletResponse;
  4. import java.io.IOException;
  5. import java.util.List;
  6. public class ExcelUtils {
  7. /**
  8. * 发送 excel 到客户端
  9. * 暂时只支持单 sheet 页
  10. * @param response
  11. * @param data
  12. * @param fileName
  13. * @throws IOException
  14. */
  15. public static void flush(HttpServletResponse response, Class dataClass, List data, String fileName) throws IOException {
  16. response.setContentType("application/vnd.ms-excel");
  17. response.setCharacterEncoding("utf-8");
  18. response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
  19. response.setHeader("Content-Disposition", "attachment;filename="+StringUtils.urlEncode(fileName)+".xlsx");
  20. EasyExcel.write(response.getOutputStream(), dataClass).sheet("sheet1").doWrite(data);
  21. }
  22. }