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.

184 line
7.8 KiB

  1. package cn.afterturn.easypoi.excel.export;
  2. import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
  3. import cn.afterturn.easypoi.excel.entity.ExportParams;
  4. import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
  5. import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
  6. import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
  7. import cn.afterturn.easypoi.exception.excel.ExcelExportException;
  8. import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum;
  9. import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil;
  10. import cn.afterturn.easypoi.util.PoiPublicUtil;
  11. import org.apache.poi.ss.usermodel.Drawing;
  12. import org.apache.poi.ss.usermodel.Sheet;
  13. import org.apache.poi.ss.usermodel.Workbook;
  14. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  15. import java.lang.reflect.Field;
  16. import java.util.*;
  17. /**
  18. * 提供批次插入服务
  19. * @author JueYue
  20. * 2016年8月29日
  21. */
  22. public class ExcelBatchExportService extends ExcelExportService {
  23. private static ThreadLocal<ExcelBatchExportService> THREAD_LOCAL = new ThreadLocal<ExcelBatchExportService>();
  24. private Workbook workbook;
  25. private Sheet sheet;
  26. private List<ExcelExportEntity> excelParams;
  27. private ExportParams entity;
  28. private int titleHeight;
  29. private Drawing patriarch;
  30. private short rowHeight;
  31. private int index;
  32. public void init(ExportParams entity, Class<?> pojoClass) {
  33. List<ExcelExportEntity> excelParams = createExcelExportEntityList(entity, pojoClass);
  34. init(entity, excelParams);
  35. }
  36. public void init(ExportParams entity, List<ExcelExportEntity> excelParams) {
  37. LOGGER.debug("ExcelBatchExportServer only support SXSSFWorkbook");
  38. entity.setType(ExcelType.XSSF);
  39. workbook = new SXSSFWorkbook();
  40. this.entity = entity;
  41. this.excelParams = excelParams;
  42. super.type = entity.getType();
  43. createSheet(workbook, entity, excelParams);
  44. if (entity.getMaxNum() == 0) {
  45. entity.setMaxNum(1000000);
  46. }
  47. insertDataToSheet(workbook, entity, excelParams, null, sheet);
  48. }
  49. public List<ExcelExportEntity> createExcelExportEntityList(ExportParams entity, Class<?> pojoClass) {
  50. try {
  51. List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
  52. if (entity.isAddIndex()) {
  53. excelParams.add(indexExcelEntity(entity));
  54. }
  55. // 得到所有字段
  56. Field[] fileds = PoiPublicUtil.getClassFields(pojoClass);
  57. ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
  58. String targetId = etarget == null ? null : etarget.value();
  59. getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass,
  60. null, null);
  61. sortAllParams(excelParams);
  62. return excelParams;
  63. } catch (Exception e) {
  64. throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e);
  65. }
  66. }
  67. public void createSheet(Workbook workbook, ExportParams entity, List<ExcelExportEntity> excelParams) {
  68. if (LOGGER.isDebugEnabled()) {
  69. LOGGER.debug("Excel export start ,List<ExcelExportEntity> is {}", excelParams);
  70. LOGGER.debug("Excel version is {}",
  71. entity.getType().equals(ExcelType.HSSF) ? "03" : "07");
  72. }
  73. if (workbook == null || entity == null || excelParams == null) {
  74. throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
  75. }
  76. try {
  77. try {
  78. sheet = workbook.createSheet(entity.getSheetName());
  79. } catch (Exception e) {
  80. // 重复遍历,出现了重名现象,创建非指定的名称Sheet
  81. sheet = workbook.createSheet();
  82. }
  83. } catch (Exception e) {
  84. throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e);
  85. }
  86. }
  87. public Workbook appendData(Collection<?> dataSet) {
  88. if (sheet.getLastRowNum() + dataSet.size() > entity.getMaxNum()) {
  89. sheet = workbook.createSheet();
  90. index = 0;
  91. }
  92. Iterator<?> its = dataSet.iterator();
  93. while (its.hasNext()) {
  94. Object t = its.next();
  95. try {
  96. index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight);
  97. } catch (Exception e) {
  98. LOGGER.error(e.getMessage(), e);
  99. throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e);
  100. }
  101. }
  102. return workbook;
  103. }
  104. @Override
  105. protected void insertDataToSheet(Workbook workbook, ExportParams entity,
  106. List<ExcelExportEntity> entityList, Collection<?> dataSet,
  107. Sheet sheet) {
  108. try {
  109. dataHandler = entity.getDataHandler();
  110. if (dataHandler != null && dataHandler.getNeedHandlerFields() != null) {
  111. needHandlerList = Arrays.asList(dataHandler.getNeedHandlerFields());
  112. }
  113. // 创建表格样式
  114. setExcelExportStyler((IExcelExportStyler) entity.getStyle()
  115. .getConstructor(Workbook.class).newInstance(workbook));
  116. patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet);
  117. List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
  118. if (entity.isAddIndex()) {
  119. excelParams.add(indexExcelEntity(entity));
  120. }
  121. excelParams.addAll(entityList);
  122. sortAllParams(excelParams);
  123. this.index = entity.isCreateHeadRows()
  124. ? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0;
  125. titleHeight = index;
  126. setCellWith(excelParams, sheet);
  127. rowHeight = getRowHeight(excelParams);
  128. setCurrentIndex(1);
  129. } catch (Exception e) {
  130. LOGGER.error(e.getMessage(), e);
  131. throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
  132. }
  133. }
  134. public static ExcelBatchExportService getExcelBatchExportService(ExportParams entity,
  135. Class<?> pojoClass) {
  136. if (THREAD_LOCAL.get() == null) {
  137. ExcelBatchExportService batchServer = new ExcelBatchExportService();
  138. batchServer.init(entity, pojoClass);
  139. THREAD_LOCAL.set(batchServer);
  140. }
  141. return THREAD_LOCAL.get();
  142. }
  143. public static ExcelBatchExportService getExcelBatchExportService(ExportParams entity,
  144. List<ExcelExportEntity> excelParams) {
  145. if (THREAD_LOCAL.get() == null) {
  146. ExcelBatchExportService batchServer = new ExcelBatchExportService();
  147. batchServer.init(entity, excelParams);
  148. THREAD_LOCAL.set(batchServer);
  149. }
  150. return THREAD_LOCAL.get();
  151. }
  152. public static ExcelBatchExportService getCurrentExcelBatchExportService() {
  153. return THREAD_LOCAL.get();
  154. }
  155. public void closeExportBigExcel() {
  156. if (entity.getFreezeCol() != 0) {
  157. sheet.createFreezePane(entity.getFreezeCol(), 0, entity.getFreezeCol(), 0);
  158. }
  159. mergeCells(sheet, excelParams, titleHeight);
  160. // 创建合计信息
  161. addStatisticsRow(getExcelExportStyler().getStyles(true, null), sheet);
  162. THREAD_LOCAL.remove();
  163. }
  164. }