ExcelUtils.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package com.poteviohealth.cgp.statistics.utils;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.ExcelWriter;
  4. import com.alibaba.excel.event.SyncReadListener;
  5. import com.alibaba.excel.write.metadata.WriteSheet;
  6. import com.google.common.collect.Lists;
  7. import lombok.SneakyThrows;
  8. import org.apache.commons.io.IOUtils;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.context.ResourceLoaderAware;
  12. import org.springframework.context.annotation.Lazy;
  13. import org.springframework.core.io.Resource;
  14. import org.springframework.core.io.ResourceLoader;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.io.UnsupportedEncodingException;
  22. import java.net.URLEncoder;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.Map;
  26. @Lazy(false)
  27. @Component
  28. public class ExcelUtils implements ResourceLoaderAware {
  29. private static Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
  30. /**
  31. * excel文件的导出下载
  32. *
  33. * @param templateLoacation excel模板文件位置,格式参见org.springframework.core.io.ResourceLoader#getResource方法注释
  34. * @param data excel模板需要的单项数据
  35. * @param list excel模板需要的列表数据
  36. * @param filename 给浏览器端下载的文件名
  37. * @param resp 响应类
  38. * @see ResourceLoader#getResource(String)
  39. */
  40. public static void easyDownload(String templateLoacation, Map<String, Object> data, List<?> list, String filename,
  41. HttpServletResponse resp) {
  42. Resource template = resourceLoader.getResource(templateLoacation);
  43. resp.setHeader("Content-Type", "application/vnd.ms-excel; charset=utf-8");
  44. try {
  45. //resp.setHeader("Transfer-Encoding", "chunked");
  46. resp.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
  47. //resp.setContentLength(getExcelFileSize(list));
  48. } catch (Exception e) {
  49. throw new RuntimeException(e);
  50. }
  51. long begin = System.currentTimeMillis();
  52. try {
  53. InputStream is = template.getInputStream();
  54. OutputStream outputStream = resp.getOutputStream();
  55. ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(is).build();
  56. is.close();
  57. WriteSheet writeSheet = EasyExcel.writerSheet().build();
  58. // 直接写入数据
  59. long t4 = System.currentTimeMillis();
  60. List<? extends List<?>> newList = Lists.partition(list, 5000);
  61. for (List<?> objects : newList) {
  62. excelWriter.fill(objects, writeSheet);
  63. }
  64. if (!data.isEmpty()) {
  65. excelWriter.fill(data, writeSheet);
  66. }
  67. long t5 = System.currentTimeMillis();
  68. excelWriter.finish();
  69. outputStream.flush();
  70. logger.info("excelWriter time:" + (t5- t4));
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. throw new RuntimeException(e);
  74. } finally {
  75. try {
  76. resp.getOutputStream().close();
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. logger.info("export time:" + (System.currentTimeMillis() - begin));
  82. }
  83. /**
  84. * excel文件的导出下载
  85. *
  86. * @param templateLoacation excel模板文件位置,格式参见org.springframework.core.io.ResourceLoader#getResource方法注释
  87. * @param data excel模板需要的单项数据
  88. * @param list excel模板需要的列表数据
  89. * @param filename 给浏览器端下载的文件名
  90. * @param resp 响应类
  91. * @see ResourceLoader#getResource(String)
  92. */
  93. public static void easyDownloadOrder(String templateLoacation, Map<String, Object> data, List<?> list, String filename,
  94. HttpServletResponse resp) {
  95. Resource template = resourceLoader.getResource(templateLoacation);
  96. resp.setContentType("application/vnd.ms-excel");
  97. try {
  98. resp.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
  99. resp.setCharacterEncoding("utf8");
  100. resp.setHeader("Pragma", "public");
  101. resp.setHeader("Cache-Control", "no-store");
  102. resp.addHeader("Cache-Control", "max-age=0");
  103. } catch (UnsupportedEncodingException e) {
  104. throw new RuntimeException(e);
  105. }
  106. long begin = System.currentTimeMillis();
  107. try {
  108. InputStream is = template.getInputStream();
  109. OutputStream outputStream = resp.getOutputStream();
  110. ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(is).build();
  111. is.close();
  112. WriteSheet writeSheet = EasyExcel.writerSheet().build();
  113. // 直接写入数据
  114. excelWriter.fill(list, writeSheet);
  115. if (!data.isEmpty()) {
  116. excelWriter.fill(data, writeSheet);
  117. }
  118. excelWriter.finish();
  119. outputStream.flush();
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. throw new RuntimeException(e);
  123. } finally {
  124. try {
  125. resp.getOutputStream().close();
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. logger.info("export time:" + (System.currentTimeMillis() - begin));
  131. }
  132. /**
  133. * 实体模型导出
  134. * @param response 响应类
  135. * @param modelClass 实体类型
  136. * @param fileName 文件名
  137. * @param sheetName sheet名
  138. * @param dataList 数据
  139. */
  140. @SneakyThrows
  141. public static void easyDownload(HttpServletResponse response, Class<?> modelClass, String fileName, String sheetName, List<?> dataList) {
  142. response.setContentType("application/vnd.ms-excel");
  143. response.setCharacterEncoding("utf-8");
  144. response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  145. EasyExcel.write(response.getOutputStream(), modelClass).sheet(sheetName).doWrite(dataList);
  146. }
  147. public static <T> List<T> easyImport(MultipartFile file, Integer head, Class<T> modelClass){
  148. InputStream io = null;
  149. try {
  150. io = file.getInputStream();
  151. SyncReadListener listener = new SyncReadListener();
  152. EasyExcel.read(io, modelClass, listener).sheet().headRowNumber(head).doRead();
  153. return (List<T>)listener.getList();
  154. }catch (IOException e){
  155. return Collections.emptyList();
  156. }
  157. finally {
  158. IOUtils.closeQuietly(io);
  159. }
  160. }
  161. private static ResourceLoader resourceLoader;
  162. @Override
  163. public void setResourceLoader(ResourceLoader resourceLoader) {
  164. setResourceLoader0(resourceLoader);
  165. }
  166. private static void setResourceLoader0(ResourceLoader resourceLoader) {
  167. ExcelUtils.resourceLoader = resourceLoader;
  168. }
  169. ExcelUtils() {
  170. }
  171. }