diff --git a/README.md b/README.md index e2b9be9..ed72131 100644 --- a/README.md +++ b/README.md @@ -143,8 +143,8 @@ EasyPoi 模板 表达式支持 - fn: 格式化数字 {{fn:(obj;###.00)}} - fe: 遍历数据,创建row - !fe: 遍历数据不创建row -- !if: 这一列是否加入,最好是第一个个有效行,删除可能营销其他数据 {{!if:(test)}} -- 单引号表示常量值 '' +- !if: 删除当前列 {{!if:(test)}} +- 单引号表示常量值 '' 比如'1' 那么输出的就是 1 --------------------------- diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelTemplateParams.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelTemplateParams.java new file mode 100644 index 0000000..994e82d --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelTemplateParams.java @@ -0,0 +1,65 @@ +package org.jeecgframework.poi.excel.entity.params; + +import java.io.Serializable; + +import org.apache.poi.ss.usermodel.CellStyle; + +/** + * 模板便利是的参数 + * @author JueYue + * @date 2015年4月29日 下午9:22:48 + */ +public class ExcelTemplateParams implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 1L; + /** + * key + */ + private String name; + /** + * 模板的cellStyle + */ + private CellStyle cellStyle; + /** + * 行高 + */ + private short height; + + public ExcelTemplateParams() { + + } + + public ExcelTemplateParams(String name, CellStyle cellStyle, short height) { + this.name = name; + this.cellStyle = cellStyle; + this.height = height; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public CellStyle getCellStyle() { + return cellStyle; + } + + public void setCellStyle(CellStyle cellStyle) { + this.cellStyle = cellStyle; + } + + public short getHeight() { + return height; + } + + public void setHeight(short height) { + this.height = height; + } + +} diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java index d8ada13..6c797eb 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java @@ -38,6 +38,7 @@ import org.jeecgframework.poi.excel.annotation.ExcelTarget; import org.jeecgframework.poi.excel.entity.TemplateExportParams; import org.jeecgframework.poi.excel.entity.enmus.ExcelType; import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; +import org.jeecgframework.poi.excel.entity.params.ExcelTemplateParams; import org.jeecgframework.poi.excel.export.base.ExcelExportBase; import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler; import org.jeecgframework.poi.exception.excel.ExcelExportException; @@ -353,7 +354,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { .replace(START_STR, EMPTY); String[] keys = name.replaceAll("\\s{1,}", " ").trim().split(" "); Collection datas = (Collection) PoiPublicUtil.getParamsValue(keys[0], map); - List columns = getAllDataColumns(cell, name.replace(keys[0], EMPTY)); + List columns = getAllDataColumns(cell, name.replace(keys[0], EMPTY)); if (datas == null) { return; } @@ -363,6 +364,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { //处理当前行 if (its.hasNext()) { Object t = its.next(); + cell.getRow().setHeight(columns.get(0).getHeight()); setForEeachCellValue(isCreate, cell.getRow(), cell.getColumnIndex(), t, columns, map); } while (its.hasNext()) { @@ -375,20 +377,21 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { row = cell.getRow().getSheet().createRow(rowIndex - 1); } } + row.setHeight(columns.get(0).getHeight()); setForEeachCellValue(isCreate, row, cell.getColumnIndex(), t, columns, map); } } private void setForEeachCellValue(boolean isCreate, Row row, int columnIndex, Object t, - List columns, Map map) - throws Exception { + List columns, Map map) + throws Exception { for (int i = 0, max = columnIndex + columns.size(); i < max; i++) { if (row.getCell(i) == null) row.createCell(i); } for (int i = 0, max = columns.size(); i < max; i++) { boolean isNumber = false; - String tempStr = new String(columns.get(i)); + String tempStr = new String(columns.get(i).getName()); if (isNumber(tempStr)) { isNumber = true; tempStr = tempStr.replace(NUMBER_SYMBOL, ""); @@ -398,8 +401,10 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { if (isNumber) { row.getCell(i + columnIndex).setCellValue(Double.parseDouble(val)); row.getCell(i + columnIndex).setCellType(Cell.CELL_TYPE_NUMERIC); + row.getCell(i + columnIndex).setCellStyle(columns.get(i).getCellStyle()); } else { row.getCell(i + columnIndex).setCellValue(val); + row.getCell(i + columnIndex).setCellStyle(columns.get(i).getCellStyle()); } tempCreateCellSet.add(row.getRowNum() + "_" + (i + columnIndex)); } @@ -412,14 +417,16 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { * @param name * @return */ - private List getAllDataColumns(Cell cell, String name) { - List columns = new ArrayList(); + private List getAllDataColumns(Cell cell, String name) { + List columns = new ArrayList(); if (name.contains(END_STR)) { - columns.add(name.replace(END_STR, EMPTY)); + columns.add(new ExcelTemplateParams(name.replace(END_STR, EMPTY), cell.getCellStyle(), + cell.getRow().getHeight())); cell.setCellValue(""); return columns; } - columns.add(name.trim()); + columns.add(new ExcelTemplateParams(name.trim(), cell.getCellStyle(), cell.getRow() + .getHeight())); int index = cell.getColumnIndex(); Cell tempCell; while (true) { @@ -439,11 +446,13 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { //把读取过的cell 置为空 cell.setCellValue(""); if (cellStringString.contains(END_STR)) { - columns.add(cellStringString.trim().replace(END_STR, "")); + columns.add(new ExcelTemplateParams(cellStringString.trim().replace(END_STR, ""), + tempCell.getCellStyle(), tempCell.getRow().getHeight())); break; } else { if (cellStringString.trim().contains(teplateParams.getTempParams())) { - columns.add(cellStringString.trim()); + columns.add(new ExcelTemplateParams(cellStringString.trim(), tempCell + .getCellStyle(), tempCell.getRow().getHeight())); } else { //最后一行被删除了 break; diff --git a/easypoi-test/src/main/java/org/jeecgframework/poi/test/excel/doc/foreach.xlsx b/easypoi-test/src/main/java/org/jeecgframework/poi/test/excel/doc/foreach.xlsx index bc13fc6..a1af1b2 100644 Binary files a/easypoi-test/src/main/java/org/jeecgframework/poi/test/excel/doc/foreach.xlsx and b/easypoi-test/src/main/java/org/jeecgframework/poi/test/excel/doc/foreach.xlsx differ