| @@ -105,7 +105,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| while (its.hasNext()) { | |||
| Object t = its.next(); | |||
| try { | |||
| index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight); | |||
| index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight, 0)[0]; | |||
| } catch (Exception e) { | |||
| LOGGER.error(e.getMessage(), e); | |||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); | |||
| @@ -22,6 +22,7 @@ import java.util.Collection; | |||
| import java.util.Iterator; | |||
| import java.util.List; | |||
| import cn.afterturn.easypoi.util.PoiMergeCellUtil; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.poi.ss.usermodel.CellStyle; | |||
| import org.apache.poi.ss.usermodel.Drawing; | |||
| @@ -48,7 +49,9 @@ import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
| */ | |||
| public class ExcelExportService extends BaseExportService { | |||
| // 最大行数,超过自动多Sheet | |||
| /** | |||
| * 最大行数,超过自动多Sheet | |||
| */ | |||
| private static int MAX_NUM = 60000; | |||
| protected int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook, | |||
| @@ -57,11 +60,71 @@ public class ExcelExportService extends BaseExportService { | |||
| if (entity.getTitle() != null) { | |||
| rows += createTitle2Row(entity, sheet, workbook, fieldLength); | |||
| } | |||
| rows += createHeaderRow(entity, sheet, workbook, rows, excelParams); | |||
| createHeaderRow(entity, sheet, workbook, rows, excelParams, 0); | |||
| rows += getRowNums(excelParams, true); | |||
| sheet.createFreezePane(0, rows, 0, rows); | |||
| return rows; | |||
| } | |||
| /** | |||
| * 创建表头 | |||
| */ | |||
| private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index, | |||
| List<ExcelExportEntity> excelParams, int cellIndex) { | |||
| Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); | |||
| int rows = getRowNums(excelParams, true); | |||
| row.setHeight(title.getHeaderHeight()); | |||
| Row listRow = null; | |||
| if (rows >= 2) { | |||
| listRow = sheet.createRow(index + 1); | |||
| listRow.setHeight(title.getHeaderHeight()); | |||
| } | |||
| int groupCellLength = 0; | |||
| CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor()); | |||
| for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { | |||
| ExcelExportEntity entity = excelParams.get(i); | |||
| // 加入换了groupName或者结束就,就把之前的那个换行 | |||
| if (StringUtils.isBlank(entity.getGroupName()) || !entity.getGroupName().equals(excelParams.get(i - 1).getGroupName())) { | |||
| if (groupCellLength > 1) { | |||
| sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex - groupCellLength, cellIndex - 1)); | |||
| } | |||
| groupCellLength = 0; | |||
| } | |||
| if (StringUtils.isNotBlank(entity.getGroupName())) { | |||
| createStringCell(row, cellIndex, entity.getGroupName(), titleStyle, entity); | |||
| createStringCell(listRow, cellIndex, entity.getName(), titleStyle, entity); | |||
| groupCellLength++; | |||
| } else if (StringUtils.isNotBlank(entity.getName())) { | |||
| createStringCell(row, cellIndex, entity.getName(), titleStyle, entity); | |||
| } | |||
| if (entity.getList() != null) { | |||
| // 保持原来的 | |||
| int tempCellIndex = cellIndex; | |||
| cellIndex = createHeaderRow(title, sheet, workbook, rows == 1 ? index : index + 1, entity.getList(), cellIndex); | |||
| List<ExcelExportEntity> sTitel = entity.getList(); | |||
| if (StringUtils.isNotBlank(entity.getName()) && sTitel.size() > 1) { | |||
| PoiMergeCellUtil.addMergedRegion(sheet, index, index, tempCellIndex, tempCellIndex + sTitel.size() - 1); | |||
| } | |||
| /*for (int j = 0, size = sTitel.size(); j < size; j++) { | |||
| createStringCell(rows == 2 ? listRow : row, cellIndex, sTitel.get(j).getName(), | |||
| titleStyle, entity); | |||
| cellIndex++; | |||
| }*/ | |||
| cellIndex--; | |||
| } else if (rows > 1 && StringUtils.isBlank(entity.getGroupName())) { | |||
| createStringCell(listRow, cellIndex, "", titleStyle, entity); | |||
| PoiMergeCellUtil.addMergedRegion(sheet, index, index + rows - 1, cellIndex, cellIndex); | |||
| } | |||
| cellIndex++; | |||
| } | |||
| if (groupCellLength > 1) { | |||
| PoiMergeCellUtil.addMergedRegion(sheet, index, index, cellIndex - groupCellLength, cellIndex - 1); | |||
| } | |||
| return cellIndex; | |||
| } | |||
| /** | |||
| * 创建 表头改变 | |||
| */ | |||
| @@ -76,7 +139,7 @@ public class ExcelExportService extends BaseExportService { | |||
| createStringCell(row, i, "", | |||
| getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); | |||
| } | |||
| sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, fieldWidth)); | |||
| PoiMergeCellUtil.addMergedRegion(sheet, 0, 0, 0, fieldWidth); | |||
| if (entity.getSecondTitle() != null) { | |||
| row = sheet.createRow(1); | |||
| row.setHeight(entity.getSecondTitleHeight()); | |||
| @@ -87,7 +150,7 @@ public class ExcelExportService extends BaseExportService { | |||
| createStringCell(row, i, "", | |||
| getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); | |||
| } | |||
| sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, fieldWidth)); | |||
| PoiMergeCellUtil.addMergedRegion(sheet, 1, 1, 0, fieldWidth); | |||
| return 2; | |||
| } | |||
| return 1; | |||
| @@ -176,7 +239,7 @@ public class ExcelExportService extends BaseExportService { | |||
| List<Object> tempList = new ArrayList<Object>(); | |||
| while (its.hasNext()) { | |||
| Object t = its.next(); | |||
| index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight); | |||
| index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight, 0)[0]; | |||
| tempList.add(t); | |||
| if (index >= MAX_NUM) { | |||
| break; | |||
| @@ -211,60 +274,5 @@ public class ExcelExportService extends BaseExportService { | |||
| } | |||
| } | |||
| /** | |||
| * 创建表头 | |||
| */ | |||
| private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index, | |||
| List<ExcelExportEntity> excelParams) { | |||
| Row row = sheet.createRow(index); | |||
| int rows = getRowNums(excelParams); | |||
| row.setHeight(title.getHeaderHeight()); | |||
| Row listRow = null; | |||
| if (rows == 2) { | |||
| listRow = sheet.createRow(index + 1); | |||
| listRow.setHeight(title.getHeaderHeight()); | |||
| } | |||
| int cellIndex = 0; | |||
| int groupCellLength = 0; | |||
| CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor()); | |||
| for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { | |||
| ExcelExportEntity entity = excelParams.get(i); | |||
| // 加入换了groupName或者结束就,就把之前的那个换行 | |||
| if (StringUtils.isBlank(entity.getGroupName()) || !entity.getGroupName().equals(excelParams.get(i - 1).getGroupName())) { | |||
| if (groupCellLength > 1) { | |||
| sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex - groupCellLength, cellIndex - 1)); | |||
| } | |||
| groupCellLength = 0; | |||
| } | |||
| if (StringUtils.isNotBlank(entity.getGroupName())) { | |||
| createStringCell(row, cellIndex, entity.getGroupName(), titleStyle, entity); | |||
| createStringCell(listRow, cellIndex, entity.getName(), titleStyle, entity); | |||
| groupCellLength++; | |||
| } else if (StringUtils.isNotBlank(entity.getName())) { | |||
| createStringCell(row, cellIndex, entity.getName(), titleStyle, entity); | |||
| } | |||
| if (entity.getList() != null) { | |||
| List<ExcelExportEntity> sTitel = entity.getList(); | |||
| if (StringUtils.isNotBlank(entity.getName()) && sTitel.size() > 1) { | |||
| sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex, cellIndex + sTitel.size() - 1)); | |||
| } | |||
| for (int j = 0, size = sTitel.size(); j < size; j++) { | |||
| createStringCell(rows == 2 ? listRow : row, cellIndex, sTitel.get(j).getName(), | |||
| titleStyle, entity); | |||
| cellIndex++; | |||
| } | |||
| cellIndex--; | |||
| } else if (rows == 2 && StringUtils.isBlank(entity.getGroupName())) { | |||
| createStringCell(listRow, cellIndex, "", titleStyle, entity); | |||
| sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex)); | |||
| } | |||
| cellIndex++; | |||
| } | |||
| if (groupCellLength > 1) { | |||
| sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex - groupCellLength, cellIndex - 1)); | |||
| } | |||
| return rows; | |||
| } | |||
| } | |||
| @@ -1,11 +1,11 @@ | |||
| /** | |||
| * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | |||
| * | |||
| * <p> | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |||
| * in compliance with the License. You may obtain a copy of the License at | |||
| * | |||
| * <p> | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * <p> | |||
| * Unless required by applicable law or agreed to in writing, software distributed under the License | |||
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |||
| * or implied. See the License for the specific language governing permissions and limitations under | |||
| @@ -69,16 +69,18 @@ public abstract class BaseExportService extends ExportCommonService { | |||
| /** | |||
| * 创建 最主要的 Cells | |||
| */ | |||
| public int createCells(Drawing patriarch, int index, Object t, | |||
| List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, | |||
| short rowHeight) { | |||
| public int[] createCells(Drawing patriarch, int index, Object t, | |||
| List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, | |||
| short rowHeight, int cellNum) { | |||
| try { | |||
| ExcelExportEntity entity; | |||
| Row row = sheet.createRow(index); | |||
| Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); | |||
| if (rowHeight != -1) { | |||
| row.setHeight(rowHeight); | |||
| } | |||
| int maxHeight = 1, cellNum = 0; | |||
| int maxHeight = 1; | |||
| // 合并需要合并的单元格 | |||
| int margeCellNum = cellNum; | |||
| int indexKey = createIndexCell(row, index, excelParams.get(0)); | |||
| cellNum += indexKey; | |||
| for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||
| @@ -87,16 +89,22 @@ public abstract class BaseExportService extends ExportCommonService { | |||
| Collection<?> list = getListCellValue(entity, t); | |||
| int listC = 0; | |||
| if (list != null && list.size() > 0) { | |||
| int tempCellNum = 0; | |||
| for (Object obj : list) { | |||
| createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), | |||
| int[] temp = createCells(patriarch, index + maxHeight - 1, obj, entity.getList(), sheet, workbook, rowHeight, cellNum); | |||
| tempCellNum = temp[1]; | |||
| maxHeight += temp[0]; | |||
| /*createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), | |||
| sheet, workbook, rowHeight); | |||
| listC++; | |||
| listC++;*/ | |||
| } | |||
| cellNum = tempCellNum; | |||
| maxHeight--; | |||
| } | |||
| cellNum += entity.getList().size(); | |||
| /* cellNum += entity.getList().size(); | |||
| if (list != null && list.size() > maxHeight) { | |||
| maxHeight = list.size(); | |||
| } | |||
| }*/ | |||
| } else { | |||
| Object value = getCellValue(entity, t); | |||
| @@ -125,24 +133,23 @@ public abstract class BaseExportService extends ExportCommonService { | |||
| } | |||
| } | |||
| } | |||
| // 合并需要合并的单元格 | |||
| cellNum = 0; | |||
| for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||
| entity = excelParams.get(k); | |||
| if (entity.getList() != null) { | |||
| cellNum += entity.getList().size(); | |||
| margeCellNum += entity.getList().size(); | |||
| } else if (entity.isNeedMerge() && maxHeight > 1) { | |||
| for (int i = index + 1; i < index + maxHeight; i++) { | |||
| sheet.getRow(i).createCell(cellNum); | |||
| sheet.getRow(i).getCell(cellNum).setCellStyle(getStyles(false, entity)); | |||
| sheet.getRow(i).createCell(margeCellNum); | |||
| sheet.getRow(i).getCell(margeCellNum).setCellStyle(getStyles(false, entity)); | |||
| } | |||
| PoiMergeCellUtil.addMergedRegion(sheet,index, index + maxHeight - 1, cellNum, cellNum); | |||
| cellNum++; | |||
| PoiMergeCellUtil.addMergedRegion(sheet, index, index + maxHeight - 1, margeCellNum, margeCellNum); | |||
| margeCellNum++; | |||
| } | |||
| } | |||
| return maxHeight; | |||
| return new int[]{maxHeight, cellNum}; | |||
| } catch (Exception e) { | |||
| LOGGER.error("excel cell export error ,data is :{}", ReflectionToStringBuilder.toString(t)); | |||
| LOGGER.error(e.getMessage(), e); | |||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); | |||
| } | |||
| @@ -396,17 +403,17 @@ public abstract class BaseExportService extends ExportCommonService { | |||
| } | |||
| public void setColumnHidden(List<ExcelExportEntity> excelParams, Sheet sheet){ | |||
| public void setColumnHidden(List<ExcelExportEntity> excelParams, Sheet sheet) { | |||
| int index = 0; | |||
| for (int i = 0; i < excelParams.size(); i++) { | |||
| if (excelParams.get(i).getList() != null) { | |||
| List<ExcelExportEntity> list = excelParams.get(i).getList(); | |||
| for (int j = 0; j < list.size(); j++) { | |||
| sheet.setColumnHidden(index,list.get(j).isColumnHidden()); | |||
| sheet.setColumnHidden(index, list.get(j).isColumnHidden()); | |||
| index++; | |||
| } | |||
| } else { | |||
| sheet.setColumnHidden(index,excelParams.get(i).isColumnHidden());; | |||
| sheet.setColumnHidden(index, excelParams.get(i).isColumnHidden()); | |||
| index++; | |||
| } | |||
| } | |||
| @@ -154,7 +154,7 @@ public class ExportCommonService { | |||
| PoiPublicUtil.getClassFields(clz), list, clz, null, null); | |||
| excelEntity = new ExcelExportEntity(); | |||
| excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); | |||
| if (i18nHandler == null) { | |||
| if (i18nHandler != null) { | |||
| excelEntity.setName(i18nHandler.getLocaleName(excelEntity.getName())); | |||
| } | |||
| excelEntity.setOrderNum(Integer | |||
| @@ -211,8 +211,8 @@ public class ExportCommonService { | |||
| if (StringUtils.isNotEmpty(entity.getSuffix()) && value != null) { | |||
| value = value + entity.getSuffix(); | |||
| } | |||
| if (value != null && StringUtils.isNotEmpty(entity.getEnumExportField())){ | |||
| value = PoiReflectorUtil.fromCache(value.getClass()).getValue(value,entity.getEnumExportField()); | |||
| if (value != null && StringUtils.isNotEmpty(entity.getEnumExportField())) { | |||
| value = PoiReflectorUtil.fromCache(value.getClass()).getValue(value, entity.getEnumExportField()); | |||
| } | |||
| return value == null ? "" : value.toString(); | |||
| } | |||
| @@ -263,7 +263,7 @@ public class ExportCommonService { | |||
| } else { | |||
| excelEntity.setGroupName(excel.groupName()); | |||
| } | |||
| if (i18nHandler == null) { | |||
| if (i18nHandler != null) { | |||
| excelEntity.setName(i18nHandler.getLocaleName(excelEntity.getName())); | |||
| excelEntity.setGroupName(i18nHandler.getLocaleName(excelEntity.getGroupName())); | |||
| } | |||
| @@ -383,21 +383,25 @@ public class ExportCommonService { | |||
| public int getFieldLength(List<ExcelExportEntity> excelParams) { | |||
| int length = -1;// 从0开始计算单元格的 | |||
| for (ExcelExportEntity entity : excelParams) { | |||
| length += entity.getList() != null ? entity.getList().size() : 1; | |||
| if (entity.getList() != null) { | |||
| length += getFieldLength(entity.getList()) + 1; | |||
| } else { | |||
| length++; | |||
| } | |||
| } | |||
| return length; | |||
| } | |||
| /** | |||
| * 判断表头是只有一行还是两行 | |||
| * 判断表头是只有一行还是多行 | |||
| */ | |||
| public int getRowNums(List<ExcelExportEntity> excelParams) { | |||
| public int getRowNums(List<ExcelExportEntity> excelParams,boolean isDeep) { | |||
| for (int i = 0; i < excelParams.size(); i++) { | |||
| if (excelParams.get(i).getList() != null | |||
| && StringUtils.isNotBlank(excelParams.get(i).getName())) { | |||
| return 2; | |||
| return isDeep? 1 + getRowNums(excelParams.get(i).getList() , isDeep) : 2; | |||
| } | |||
| if (StringUtils.isNoneEmpty(excelParams.get(i).getGroupName())) { | |||
| if (StringUtils.isNotEmpty(excelParams.get(i).getGroupName())) { | |||
| return 2; | |||
| } | |||
| } | |||
| @@ -120,7 +120,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| Iterator<?> its = dataSet.iterator(); | |||
| while (its.hasNext()) { | |||
| Object t = its.next(); | |||
| index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight); | |||
| index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight, 0)[0]; | |||
| } | |||
| // 合并同类项 | |||
| mergeCells(sheet, excelParams, titleHeight); | |||
| @@ -88,7 +88,7 @@ public class ImportBaseService { | |||
| if(excelEntityAnn != null && excelEntityAnn.show()){ | |||
| excelEntity.setName(excelEntityAnn.name() + "_" + excelEntity.getName()); | |||
| } | |||
| if (i18nHandler == null) { | |||
| if (i18nHandler != null) { | |||
| excelEntity.setName(i18nHandler.getLocaleName(excelEntity.getName())); | |||
| } | |||
| excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(field.getName())); | |||
| @@ -137,7 +137,7 @@ public class SaxRowRead extends ImportBaseService implements ISaxRowRead { | |||
| } | |||
| } else { | |||
| if (object != null && hanlder != null) { | |||
| hanlder.hanlder(object); | |||
| hanlder.handler(object); | |||
| } | |||
| object = PoiPublicUtil.createObject(pojoClass, targetId); | |||
| SaxReadCellEntity entity; | |||
| @@ -26,6 +26,6 @@ public interface IExcelReadRowHandler<T> { | |||
| * 处理解析对象 | |||
| * @param t | |||
| */ | |||
| public void hanlder(T t); | |||
| public void handler(T t); | |||
| } | |||
| @@ -251,7 +251,7 @@ public class PdfExportServer extends ExportCommonService { | |||
| */ | |||
| private int createTitleRow(PdfExportParams title, PdfPTable table, | |||
| List<ExcelExportEntity> excelParams) { | |||
| int rows = getRowNums(excelParams); | |||
| int rows = getRowNums(excelParams, false); | |||
| for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { | |||
| ExcelExportEntity entity = excelParams.get(i); | |||
| if (entity.getList() != null) { | |||
| @@ -100,7 +100,7 @@ public final class PoiMergeCellUtil { | |||
| if (mergeDataMap.size() > 0) { | |||
| for (Integer index : mergeDataMap.keySet()) { | |||
| if (mergeDataMap.get(index).getEndRow() > mergeDataMap.get(index).getStartRow()) { | |||
| PoiMergeCellUtil.addMergedRegion(sheet,mergeDataMap.get(index).getStartRow(), | |||
| PoiMergeCellUtil.addMergedRegion(sheet, mergeDataMap.get(index).getStartRow(), | |||
| mergeDataMap.get(index).getEndRow(), index, index); | |||
| } | |||
| } | |||
| @@ -127,7 +127,7 @@ public final class PoiMergeCellUtil { | |||
| mergeDataMap.get(index).setEndRow(rowNum); | |||
| } else { | |||
| if (mergeDataMap.get(index).getEndRow() > mergeDataMap.get(index).getStartRow()) { | |||
| PoiMergeCellUtil.addMergedRegion(sheet,mergeDataMap.get(index).getStartRow(), | |||
| PoiMergeCellUtil.addMergedRegion(sheet, mergeDataMap.get(index).getStartRow(), | |||
| mergeDataMap.get(index).getEndRow(), index, index); | |||
| } | |||
| mergeDataMap.put(index, createMergeEntity(text, rowNum, cell, delys)); | |||
| @@ -149,7 +149,7 @@ public final class PoiMergeCellUtil { | |||
| if (mergeDataMap.containsKey(index) | |||
| && mergeDataMap.get(index).getEndRow() != mergeDataMap.get(index).getStartRow()) { | |||
| try { | |||
| PoiMergeCellUtil.addMergedRegion(sheet,mergeDataMap.get(index).getStartRow(), | |||
| PoiMergeCellUtil.addMergedRegion(sheet, mergeDataMap.get(index).getStartRow(), | |||
| mergeDataMap.get(index).getEndRow(), index, index); | |||
| } catch (Exception e) { | |||
| @@ -215,6 +215,9 @@ public final class PoiMergeCellUtil { | |||
| try { | |||
| sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); | |||
| } catch (Exception e) { | |||
| LOGGER.error("发生了一次合并单元格错误,{},{},{},{}", new Integer[]{ | |||
| firstRow, lastRow, firstCol, lastCol | |||
| }); | |||
| // 忽略掉合并的错误,不打印异常 | |||
| LOGGER.debug(e.getMessage(), e); | |||
| } | |||