You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
6.4 KiB

  1. /**
  2. * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package cn.afterturn.easypoi.excel;
  17. import java.util.Collection;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  21. import org.apache.poi.ss.usermodel.Workbook;
  22. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  23. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  24. import cn.afterturn.easypoi.excel.entity.ExportParams;
  25. import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
  26. import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
  27. import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
  28. import cn.afterturn.easypoi.excel.export.ExcelBatchExportService;
  29. import cn.afterturn.easypoi.excel.export.ExcelExportService;
  30. import cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil;
  31. /**
  32. * excel 导出工具类
  33. *
  34. * @author JueYue
  35. * @version 1.0
  36. * 2013-10-17
  37. */
  38. public class ExcelExportUtil {
  39. private ExcelExportUtil() {
  40. }
  41. /**
  42. * @param entity
  43. * 表格标题属性
  44. * @param pojoClass
  45. * Excel对象Class
  46. * @param dataSet
  47. * Excel对象数据List
  48. */
  49. public static Workbook exportBigExcel(ExportParams entity, Class<?> pojoClass,
  50. Collection<?> dataSet) {
  51. ExcelBatchExportService batchService = ExcelBatchExportService
  52. .getExcelBatchExportService(entity, pojoClass);
  53. return batchService.appendData(dataSet);
  54. }
  55. public static Workbook exportBigExcel(ExportParams entity, List<ExcelExportEntity> excelParams,
  56. Collection<?> dataSet) {
  57. ExcelBatchExportService batchService = ExcelBatchExportService
  58. .getExcelBatchExportService(entity, excelParams);
  59. return batchService.appendData(dataSet);
  60. }
  61. public static void closeExportBigExcel() {
  62. ExcelBatchExportService batchService = ExcelBatchExportService.getCurrentExcelBatchExportService();
  63. if(batchService != null) {
  64. batchService.closeExportBigExcel();
  65. }
  66. }
  67. /**
  68. * @param entity
  69. * 表格标题属性
  70. * @param pojoClass
  71. * Excel对象Class
  72. * @param dataSet
  73. * Excel对象数据List
  74. */
  75. public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass,
  76. Collection<?> dataSet) {
  77. Workbook workbook = getWorkbook(entity.getType(),dataSet.size());
  78. new ExcelExportService().createSheet(workbook, entity, pojoClass, dataSet);
  79. return workbook;
  80. }
  81. private static Workbook getWorkbook(ExcelType type, int size) {
  82. if (ExcelType.HSSF.equals(type)) {
  83. return new HSSFWorkbook();
  84. } else if (size < 100000) {
  85. return new XSSFWorkbook();
  86. } else {
  87. return new SXSSFWorkbook();
  88. }
  89. }
  90. /**
  91. * 根据Map创建对应的Excel
  92. * @param entity
  93. * 表格标题属性
  94. * @param entityList
  95. * Map对象列表
  96. * @param dataSet
  97. * Excel对象数据List
  98. */
  99. public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList,
  100. Collection<?> dataSet) {
  101. Workbook workbook = getWorkbook(entity.getType(),dataSet.size());;
  102. new ExcelExportService().createSheetForMap(workbook, entity, entityList, dataSet);
  103. return workbook;
  104. }
  105. /**
  106. * 一个excel 创建多个sheet
  107. *
  108. * @param list
  109. * 多个Map key title 对应表格Title key entity 对应表格对应实体 key data
  110. * Collection 数据
  111. * @return
  112. */
  113. public static Workbook exportExcel(List<Map<String, Object>> list, ExcelType type) {
  114. Workbook workbook = getWorkbook(type,0);
  115. for (Map<String, Object> map : list) {
  116. ExcelExportService service = new ExcelExportService();
  117. service.createSheet(workbook, (ExportParams) map.get("title"),
  118. (Class<?>) map.get("entity"), (Collection<?>) map.get("data"));
  119. }
  120. return workbook;
  121. }
  122. /**
  123. * 导出文件通过模板解析,不推荐这个了,推荐全部通过模板来执行处理
  124. *
  125. * @param params
  126. * 导出参数类
  127. * @param pojoClass
  128. * 对应实体
  129. * @param dataSet
  130. * 实体集合
  131. * @param map
  132. * 模板集合
  133. * @return
  134. */
  135. @Deprecated
  136. public static Workbook exportExcel(TemplateExportParams params, Class<?> pojoClass,
  137. Collection<?> dataSet, Map<String, Object> map) {
  138. return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, pojoClass, dataSet,
  139. map);
  140. }
  141. /**
  142. * 导出文件通过模板解析只有模板,没有集合
  143. *
  144. * @param params
  145. * 导出参数类
  146. * @param map
  147. * 模板集合
  148. * @return
  149. */
  150. public static Workbook exportExcel(TemplateExportParams params, Map<String, Object> map) {
  151. return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, null, null, map);
  152. }
  153. /**
  154. * 导出文件通过模板解析只有模板,没有集合
  155. * 每个sheet对应一个map,导出到处,key是sheet的NUM
  156. * @param params
  157. * 导出参数类
  158. * @param map
  159. * 模板集合
  160. * @return
  161. */
  162. public static Workbook exportExcel(Map<Integer, Map<String, Object>> map,
  163. TemplateExportParams params) {
  164. return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, map);
  165. }
  166. }