From 4fc315d38bc16002192922d6b3c5c74e3109cbd7 Mon Sep 17 00:00:00 2001 From: jueyue Date: Wed, 7 Feb 2018 14:40:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5,fe=E7=9A=84=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../afterturn/easypoi/entity/ImageEntity.java | 19 +++++++ .../template/ExcelExportOfTemplateUtil.java | 23 ++++++-- .../cn/afterturn/easypoi/util/PoiElUtil.java | 56 +++++++++---------- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/entity/ImageEntity.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/entity/ImageEntity.java index bb1ea67..3a28b9b 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/entity/ImageEntity.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/entity/ImageEntity.java @@ -41,6 +41,10 @@ public class ImageEntity { // 图片信息 private byte[] data; + private int rowspan = 1; + private int colspan = 1; + + public ImageEntity() { } @@ -98,4 +102,19 @@ public class ImageEntity { this.width = width; } + public int getRowspan() { + return rowspan; + } + + public void setRowspan(int rowspan) { + this.rowspan = rowspan; + } + + public int getColspan() { + return colspan; + } + + public void setColspan(int colspan) { + this.colspan = colspan; + } } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java index e49d6ad..17168bf 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java @@ -407,10 +407,15 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { oldString = oldString.replaceFirst(NUMBER_SYMBOL, ""); } Object obj = PoiPublicUtil.getRealValue(oldString, map); - //如何是数值 类型,就按照数值类型进行设置 - if (obj instanceof ImageEntity) {// 如果是图片就设置为图片 + //如何是数值 类型,就按照数值类型进行设置// 如果是图片就设置为图片 + if (obj instanceof ImageEntity) { ImageEntity img = (ImageEntity)obj; cell.setCellValue(""); + if (img.getRowspan()>1 || img.getColspan() > 1){ + img.setHeight(0); + cell.getSheet().addMergedRegion(new CellRangeAddress(cell.getRowIndex(), + cell.getRowIndex() + img.getRowspan() - 1, cell.getColumnIndex(), cell.getColumnIndex() + img.getColspan() -1)); + } createImageCell(cell,img.getHeight(),img.getUrl(),img.getData()); }else if (isNumber && StringUtils.isNotBlank(obj.toString())) { cell.setCellValue(Double.parseDouble(obj.toString())); @@ -464,7 +469,9 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { rowspan, colspan, mergedRegionHelper); rowIndex += rowspan - 1; } - if (isShift && datas.size() * rowspan > 1 && cell.getRowIndex() + rowspan < cell.getRow().getSheet().getLastRowNum()) { + //不论后面有没有数据,都应该执行的是插入操作 + // && cell.getRowIndex() + rowspan < cell.getRow().getSheet().getLastRowNum() + if (isShift && datas.size() * rowspan > 1) { createRowNoRow(cell.getRowIndex() + rowspan,cell.getRow().getSheet().getLastRowNum(),(datas.size() - 1) * rowspan); cell.getRow().getSheet().shiftRows(cell.getRowIndex() + rowspan, cell.getRow().getSheet().getLastRowNum(), (datas.size() - 1) * rowspan, true, true); @@ -548,6 +555,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { continue; } String val = null; + Object obj = null; //是不是常量 if (StringUtils.isEmpty(params.getName())) { val = params.getConstValue(); @@ -558,9 +566,14 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { tempStr = tempStr.replaceFirst(NUMBER_SYMBOL, ""); } map.put(teplateParams.getTempParams(), t); - val = eval(tempStr, map).toString(); + obj = eval(tempStr, map); + val = obj.toString(); } - if (isNumber && StringUtils.isNotEmpty(val)) { + if (obj != null && obj instanceof ImageEntity) { + ImageEntity img = (ImageEntity)obj; + row.getCell(ci).setCellValue(""); + createImageCell(row.getCell(ci),img.getHeight(),img.getUrl(),img.getData()); + }else if (isNumber && StringUtils.isNotEmpty(val)) { row.getCell(ci).setCellValue(Double.parseDouble(val)); row.getCell(ci).setCellType(Cell.CELL_TYPE_NUMERIC); } else { diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java index a54a699..cd7cf8e 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java @@ -1,6 +1,6 @@ /** * 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 @@ -26,25 +26,25 @@ import cn.afterturn.easypoi.exception.excel.ExcelExportException; */ public final class PoiElUtil { - public static final String LENGTH = "le:"; - public static final String FOREACH = "fe:"; - public static final String FOREACH_NOT_CREATE = "!fe:"; - public static final String FOREACH_AND_SHIFT = "$fe:"; - public static final String FOREACH_COL = "#fe:"; - public static final String FOREACH_COL_VALUE = "v_fe:"; - public static final String START_STR = "{{"; - public static final String END_STR = "}}"; - public static final String WRAP = "]]"; - public static final String NUMBER_SYMBOL = "n:"; - public static final String FORMAT_DATE = "fd:"; - public static final String FORMAT_NUMBER = "fn:"; - public static final String SUM = "sum:"; - public static final String IF_DELETE = "!if:"; - public static final String EMPTY = ""; - public static final String CONST = "'"; - public static final String NULL = "&NULL&"; - public static final String LEFT_BRACKET = "("; - public static final String RIGHT_BRACKET = ")"; + public static final String LENGTH = "le:"; + public static final String FOREACH = "fe:"; + public static final String FOREACH_NOT_CREATE = "!fe:"; + public static final String FOREACH_AND_SHIFT = "$fe:"; + public static final String FOREACH_COL = "#fe:"; + public static final String FOREACH_COL_VALUE = "v_fe:"; + public static final String START_STR = "{{"; + public static final String END_STR = "}}"; + public static final String WRAP = "]]"; + public static final String NUMBER_SYMBOL = "n:"; + public static final String FORMAT_DATE = "fd:"; + public static final String FORMAT_NUMBER = "fn:"; + public static final String SUM = "sum:"; + public static final String IF_DELETE = "!if:"; + public static final String EMPTY = ""; + public static final String CONST = "'"; + public static final String NULL = "&NULL&"; + public static final String LEFT_BRACKET = "("; + public static final String RIGHT_BRACKET = ")"; private PoiElUtil() { } @@ -54,7 +54,7 @@ public final class PoiElUtil { * @param text * @param map * @return - * @throws Exception + * @throws Exception */ public static Object eval(String text, Map map) throws Exception { String tempText = new String(text); @@ -71,7 +71,7 @@ public final class PoiElUtil { * @param text * @param map * @return - * @throws Exception + * @throws Exception */ public static Object innerEval(String text, Map map) throws Exception { if (text.indexOf("?") != -1 && text.indexOf(":") != -1) { @@ -100,7 +100,7 @@ public final class PoiElUtil { * @param text * @param map * @return - * @throws Exception + * @throws Exception */ private static Object ifDelete(String text, Map map) throws Exception { //把多个空格变成一个空格 @@ -150,7 +150,7 @@ public final class PoiElUtil { * @param text * @param map * @return - * @throws Exception + * @throws Exception */ private static Object formatNumber(String text, Map map) throws Exception { String[] key = getKey(FORMAT_NUMBER, text).split(";"); @@ -166,7 +166,7 @@ public final class PoiElUtil { * @param text * @param map * @return - * @throws Exception + * @throws Exception */ private static Object formatDate(String text, Map map) throws Exception { String[] key = getKey(FORMAT_DATE, text).split(";"); @@ -181,7 +181,7 @@ public final class PoiElUtil { * 计算这个的长度 * @param text * @param map - * @throws Exception + * @throws Exception */ private static Object length(String text, Map map) throws Exception { String key = getKey(LENGTH, text); @@ -226,7 +226,7 @@ public final class PoiElUtil { /** * 三目运算 * @return - * @throws Exception + * @throws Exception */ private static Object trinocular(String text, Map map) throws Exception { //把多个空格变成一个空格 @@ -250,7 +250,7 @@ public final class PoiElUtil { for (int i = 2; i < keys.length; i++) { trinocular += ":" + keys[i]; } - second = eval(trinocular, map); + second = eval(trinocular, map); } }else{ first = eval(keys[0].trim(), map);