diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/ExcelExportServer.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/ExcelExportServer.java index ed72e91..893bd86 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/ExcelExportServer.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/ExcelExportServer.java @@ -167,7 +167,7 @@ public class ExcelExportServer extends BaseExportServer { ? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0; int titleHeight = index; setCellWith(excelParams, sheet); - short rowHeight = entity.getHeight() > 0? entity.getHeight() : getRowHeight(excelParams); + short rowHeight = entity.getHeight() > 0 ? entity.getHeight() : getRowHeight(excelParams); setCurrentIndex(1); Iterator its = dataSet.iterator(); List tempList = new ArrayList(); @@ -226,15 +226,18 @@ public class ExcelExportServer extends BaseExportServer { 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())) { - if(groupCellLength > 0){ - sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex, cellIndex - groupCellLength)); - groupCellLength = 0; - } createStringCell(row, cellIndex, entity.getName(), titleStyle, entity); } if (entity.getList() != null) { @@ -254,7 +257,7 @@ public class ExcelExportServer extends BaseExportServer { } cellIndex++; } - if(groupCellLength > 0){ + if (groupCellLength > 1) { sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex - groupCellLength, cellIndex - 1)); } return rows; diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/styler/AbstractExcelExportStyler.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/styler/AbstractExcelExportStyler.java index 526889e..ae4215c 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/styler/AbstractExcelExportStyler.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/styler/AbstractExcelExportStyler.java @@ -1,13 +1,13 @@ /** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) - * - * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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 * - * Unless required by applicable law or agreed to in writing, software + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 @@ -16,6 +16,7 @@ package cn.afterturn.easypoi.excel.export.styler; import org.apache.poi.ss.usermodel.BuiltinFormats; +import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Workbook; @@ -24,18 +25,18 @@ import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; /** * 抽象接口提供两个公共方法 - * @author JueYue - * 2015年1月9日 下午5:48:55 + * + * @author JueYue 2015年1月9日 下午5:48:55 */ public abstract class AbstractExcelExportStyler implements IExcelExportStyler { //单行 - protected CellStyle stringNoneStyle; - protected CellStyle stringNoneWrapStyle; + protected CellStyle stringNoneStyle; + protected CellStyle stringNoneWrapStyle; //间隔行 - protected CellStyle stringSeptailStyle; - protected CellStyle stringSeptailWrapStyle; + protected CellStyle stringSeptailStyle; + protected CellStyle stringSeptailWrapStyle; - protected Workbook workbook; + protected Workbook workbook; protected static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT"); @@ -61,6 +62,11 @@ public abstract class AbstractExcelExportStyler implements IExcelExportStyler { return stringSeptailStyle; } + @Override + public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) { + return getStyles(dataRow % 2 == 1, entity); + } + public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) { return null; } @@ -69,6 +75,7 @@ public abstract class AbstractExcelExportStyler implements IExcelExportStyler { return null; } + @Override public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) { return null; diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/styler/IExcelExportStyler.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/styler/IExcelExportStyler.java index 637768e..d7e0582 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/styler/IExcelExportStyler.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/styler/IExcelExportStyler.java @@ -1,13 +1,13 @@ /** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) - * - * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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 * - * Unless required by applicable law or agreed to in writing, software + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 @@ -15,6 +15,7 @@ */ package cn.afterturn.easypoi.excel.export.styler; +import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; @@ -22,38 +23,38 @@ import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; /** * Excel导出样式接口 - * @author JueYue - * 2015年1月9日 下午5:32:30 + * + * @author JueYue 2015年1月9日 下午5:32:30 */ public interface IExcelExportStyler { /** * 列表头样式 - * @param headerColor - * @return */ public CellStyle getHeaderStyle(short headerColor); /** * 标题样式 - * @param color - * @return */ public CellStyle getTitleStyle(short color); /** * 获取样式方法 - * @param parity - * @param entity - * @return */ + @Deprecated public CellStyle getStyles(boolean parity, ExcelExportEntity entity); + /** + * 获取样式方法 + * + * @param dataRow 数据行 + * @param obj 对象 + * @param data 数据 + */ + public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data); + /** * 模板使用的样式设置 - * @param isSingle - * @param excelForEachParams - * @return */ public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams);