Browse Source

勉强支持模板统计了

4.1.3.A
jueyue 8 years ago
parent
commit
49da995473
3 changed files with 119 additions and 51 deletions
  1. +18
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelForEachParams.java
  2. +60
    -38
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java
  3. +41
    -13
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/TemplateSumHanlder.java

+ 18
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelForEachParams.java View File

@@ -39,6 +39,8 @@ public class ExcelForEachParams implements Serializable {
* 行合并 * 行合并
*/ */
private int rowspan = 1; private int rowspan = 1;
private boolean needSum;
public ExcelForEachParams() { public ExcelForEachParams() {
@@ -49,6 +51,14 @@ public class ExcelForEachParams implements Serializable {
this.cellStyle = cellStyle; this.cellStyle = cellStyle;
this.height = height; this.height = height;
} }
public ExcelForEachParams(String name, CellStyle cellStyle, short height,boolean needSum) {
this.name = name;
this.cellStyle = cellStyle;
this.height = height;
this.needSum = needSum;
}
public String getName() { public String getName() {
return name; return name;
@@ -98,4 +108,12 @@ public class ExcelForEachParams implements Serializable {
this.rowspan = rowspan; this.rowspan = rowspan;
} }
public boolean isNeedSum() {
return needSum;
}
public void setNeedSum(boolean needSum) {
this.needSum = needSum;
}
} }

+ 60
- 38
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java View File

@@ -43,6 +43,7 @@ import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.entity.params.ExcelForEachParams; import org.jeecgframework.poi.excel.entity.params.ExcelForEachParams;
import org.jeecgframework.poi.excel.export.base.ExcelExportBase; import org.jeecgframework.poi.excel.export.base.ExcelExportBase;
import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler; import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler;
import org.jeecgframework.poi.excel.export.template.TemplateSumHanlder.TemplateSumEntity;
import org.jeecgframework.poi.excel.html.helper.MergedRegionHelper; import org.jeecgframework.poi.excel.html.helper.MergedRegionHelper;
import org.jeecgframework.poi.exception.excel.ExcelExportException; import org.jeecgframework.poi.exception.excel.ExcelExportException;
import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum;
@@ -79,6 +80,8 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
*/ */
private MergedRegionHelper mergedRegionHelper; private MergedRegionHelper mergedRegionHelper;
private TemplateSumHanlder templateSumHanlder;
/** /**
* 往Sheet 填充正常数据,根据表头信息 使用导入的部分逻辑,坐对象映射 * 往Sheet 填充正常数据,根据表头信息 使用导入的部分逻辑,坐对象映射
* *
@@ -188,7 +191,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
wb.setSheetName(i, params.getSheetName()[i]); wb.setSheetName(i, params.getSheetName()[i]);
} }
tempCreateCellSet.clear(); tempCreateCellSet.clear();
parseTemplate(wb.getSheetAt(i), map,params.isColForEach());
parseTemplate(wb.getSheetAt(i), map, params.isColForEach());
} }
if (dataSet != null) { if (dataSet != null) {
// step 4. 正常的数据填充 // step 4. 正常的数据填充
@@ -243,11 +246,13 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
} }
private void parseTemplate(Sheet sheet, Map<String, Object> map, boolean colForeach) throws Exception {
private void parseTemplate(Sheet sheet, Map<String, Object> map,
boolean colForeach) throws Exception {
deleteCell(sheet, map); deleteCell(sheet, map);
mergedRegionHelper = new MergedRegionHelper(sheet); mergedRegionHelper = new MergedRegionHelper(sheet);
if(colForeach){
colForeach(sheet, map);
templateSumHanlder = new TemplateSumHanlder(sheet);
if (colForeach) {
colForeach(sheet, map);
} }
Row row = null; Row row = null;
int index = 0; int index = 0;
@@ -263,6 +268,17 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
} }
} }
} }
//修改需要处理的统计值
hanlderSumCell(sheet);
}
private void hanlderSumCell(Sheet sheet) {
for (TemplateSumEntity sumEntity : templateSumHanlder.getDataList()) {
Cell cell = sheet.getRow(sumEntity.getRow()).getCell(sumEntity.getCol());
cell.setCellValue(cell.getStringCellValue()
.replace("sum:(" + sumEntity.getSumKey() + ")", sumEntity.getValue() + ""));
}
} }
/** /**
@@ -271,7 +287,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
* @param map * @param map
*/ */
private void colForeach(Sheet sheet, Map<String, Object> map) throws Exception { private void colForeach(Sheet sheet, Map<String, Object> map) throws Exception {
Row row = null;
Row row = null;
Cell cell = null; Cell cell = null;
int index = 0; int index = 0;
while (index <= sheet.getLastRowNum()) { while (index <= sheet.getLastRowNum()) {
@@ -286,13 +302,13 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(Cell.CELL_TYPE_STRING);
String text = cell.getStringCellValue(); String text = cell.getStringCellValue();
if (text.contains(FOREACH_COL) || text.contains(FOREACH_COL_VALUE)) { if (text.contains(FOREACH_COL) || text.contains(FOREACH_COL_VALUE)) {
foreachCol(cell,map,text);
foreachCol(cell, map, text);
} }
} }
} }
} }
}
}
/** /**
* 循环列表 * 循环列表
* @param cell * @param cell
@@ -300,34 +316,34 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
* @param name * @param name
* @throws Exception * @throws Exception
*/ */
private void foreachCol(Cell cell, Map<String, Object> map,
String name) throws Exception {
boolean isCreate = name.contains(FOREACH_COL_VALUE);
name = name.replace(FOREACH_COL_VALUE, EMPTY).replace(FOREACH_COL, EMPTY).replace(START_STR, EMPTY);
String[] keys = name.replaceAll("\\s{1,}", " ").trim().split(" ");
Collection<?> datas = (Collection<?>) PoiPublicUtil.getParamsValue(keys[0], map);
Object[] columnsInfo = getAllDataColumns(cell, name.replace(keys[0], EMPTY),
mergedRegionHelper);
if (datas == null) {
return;
}
Iterator<?> its = datas.iterator();
int rowspan = (Integer) columnsInfo[0], colspan = (Integer) columnsInfo[1];
@SuppressWarnings("unchecked")
List<ExcelForEachParams> columns = (List<ExcelForEachParams>) columnsInfo[2];
while (its.hasNext()) {
Object t = its.next();
setForEeachRowCellValue(true, cell.getRow(), cell.getColumnIndex(), t, columns, map, rowspan,
colspan, mergedRegionHelper);
cell = cell.getRow().getCell(cell.getColumnIndex()+colspan);
}
if(isCreate) {
cell = cell.getRow().getCell(cell.getColumnIndex()-1);
cell.setCellValue(cell.getStringCellValue()+END_STR);
}
private void foreachCol(Cell cell, Map<String, Object> map, String name) throws Exception {
boolean isCreate = name.contains(FOREACH_COL_VALUE);
name = name.replace(FOREACH_COL_VALUE, EMPTY).replace(FOREACH_COL, EMPTY).replace(START_STR,
EMPTY);
String[] keys = name.replaceAll("\\s{1,}", " ").trim().split(" ");
Collection<?> datas = (Collection<?>) PoiPublicUtil.getParamsValue(keys[0], map);
Object[] columnsInfo = getAllDataColumns(cell, name.replace(keys[0], EMPTY),
mergedRegionHelper);
if (datas == null) {
return;
}
Iterator<?> its = datas.iterator();
int rowspan = (Integer) columnsInfo[0], colspan = (Integer) columnsInfo[1];
@SuppressWarnings("unchecked")
List<ExcelForEachParams> columns = (List<ExcelForEachParams>) columnsInfo[2];
while (its.hasNext()) {
Object t = its.next();
setForEeachRowCellValue(true, cell.getRow(), cell.getColumnIndex(), t, columns, map,
rowspan, colspan, mergedRegionHelper);
cell = cell.getRow().getCell(cell.getColumnIndex() + colspan);
}
if (isCreate) {
cell = cell.getRow().getCell(cell.getColumnIndex() - 1);
cell.setCellValue(cell.getStringCellValue() + END_STR);
}
} }
/**
/**
* 先判断删除,省得影响效率 * 先判断删除,省得影响效率
* @param sheet * @param sheet
* @param map * @param map
@@ -401,7 +417,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
} }
//判断foreach 这种方法 //判断foreach 这种方法
if (oldString != null && oldString.contains(FOREACH)) { if (oldString != null && oldString.contains(FOREACH)) {
addListDataToExcel(cell, map, oldString.trim());
addListDataToExcel(cell, map, oldString.trim());
} }
} }
@@ -446,9 +462,10 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
} }
if (isShift && datas.size() * rowspan > 1) { if (isShift && datas.size() * rowspan > 1) {
cell.getRow().getSheet().shiftRows(cell.getRowIndex() + rowspan, cell.getRow().getSheet().shiftRows(cell.getRowIndex() + rowspan,
cell.getRow().getSheet().getLastRowNum(), (datas.size() - 1) * rowspan , true, true);
/* cell.getRow().getSheet().shiftRows(cell.getRowIndex() + 1,
cell.getRow().getSheet().getLastRowNum(), (datas.size() - 1) * rowspan, true, true);
/* cell.getRow().getSheet().shiftRows(cell.getRowIndex() + 1,
cell.getRow().getSheet().getLastRowNum(), datas.size() * rowspan - 1, true, true);*/ cell.getRow().getSheet().getLastRowNum(), datas.size() * rowspan - 1, true, true);*/
templateSumHanlder.shiftRows(cell.getRowIndex(),(datas.size() - 1) * rowspan);
} }
while (its.hasNext()) { while (its.hasNext()) {
Object t = its.next(); Object t = its.next();
@@ -546,6 +563,10 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
} }
} }
row.getCell(ci).setCellStyle(columns.get(i).getCellStyle()); row.getCell(ci).setCellStyle(columns.get(i).getCellStyle());
//判断这个属性是不是需要统计
if (params.isNeedSum()) {
templateSumHanlder.addValueOfKey(params.getName(), val);
}
//如果合并单元格,就把这个单元格的样式和之前的保持一致 //如果合并单元格,就把这个单元格的样式和之前的保持一致
setMergedRegionStyle(row, ci, columns.get(i)); setMergedRegionStyle(row, ci, columns.get(i));
//合并对应单元格 //合并对应单元格
@@ -690,6 +711,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
params.setRowspan(colAndrow[0]); params.setRowspan(colAndrow[0]);
params.setColspan(colAndrow[1]); params.setColspan(colAndrow[1]);
} }
params.setNeedSum(templateSumHanlder.isSumKey(params.getName()));
return params; return params;
} }
@@ -739,7 +761,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
wb.setSheetName(i, params.getSheetName()[i]); wb.setSheetName(i, params.getSheetName()[i]);
} }
tempCreateCellSet.clear(); tempCreateCellSet.clear();
parseTemplate(wb.getSheetAt(i), map.get(i),params.isColForEach());
parseTemplate(wb.getSheetAt(i), map.get(i), params.isColForEach());
} }
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage(), e); LOGGER.error(e.getMessage(), e);


+ 41
- 13
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/TemplateSumHanlder.java View File

@@ -1,8 +1,13 @@
package org.jeecgframework.poi.excel.export.template; package org.jeecgframework.poi.excel.export.template;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
@@ -19,12 +24,16 @@ import static org.jeecgframework.poi.util.PoiElUtil.*;
*/ */
public class TemplateSumHanlder { public class TemplateSumHanlder {
private List<TemplateSumEntity> sumList = new ArrayList<TemplateSumEntity>();
private Map<String, TemplateSumEntity> sumMap = new HashMap<String, TemplateSumEntity>();
public TemplateSumHanlder(Sheet sheet) {
getAllSumCell(sheet);
}
/** /**
* 统计计算所有的统计单元格 * 统计计算所有的统计单元格
*/ */
public void getAllSumCell(Sheet sheet) {
private void getAllSumCell(Sheet sheet) {
Row row = null; Row row = null;
int index = 0; int index = 0;
while (index <= sheet.getLastRowNum()) { while (index <= sheet.getLastRowNum()) {
@@ -49,10 +58,14 @@ public class TemplateSumHanlder {
entity.setSumKey(getSumKey(cellValue, index++)); entity.setSumKey(getSumKey(cellValue, index++));
entity.setCol(cell.getColumnIndex()); entity.setCol(cell.getColumnIndex());
entity.setRow(cell.getRowIndex()); entity.setRow(cell.getRowIndex());
sumList.add(entity);
sumMap.put(entity.getSumKey(), entity);
} }
} }
public boolean isSumKey(String key) {
return sumMap.containsKey(key);
}
/** /**
* SUM:(key) * SUM:(key)
* *
@@ -61,19 +74,34 @@ public class TemplateSumHanlder {
* @return * @return
*/ */
private String getSumKey(String cellValue, int index) { private String getSumKey(String cellValue, int index) {
return cellValue.substring(index + 4, cellValue.indexOf(")", index));
return cellValue.substring(index + 5, cellValue.indexOf(")", index));
}
public void addValueOfKey(String key, String val) {
if (StringUtils.isNoneEmpty(key))
sumMap.get(key).setValue(sumMap.get(key).getValue() + Double.valueOf(val));
}
public List<TemplateSumEntity> getDataList() {
return new ArrayList<TemplateSumEntity>(sumMap.values());
} }
public void addListSizeToSumEntity(){
public void addListSizeToSumEntity() {
} }
/** /**
* *
* @param rowIndex * @param rowIndex
* @param size
*/ */
public void findForeachList(int rowIndex){
public void shiftRows(int rowIndex, int size) {
for (TemplateSumEntity entity : getDataList()) {
if (entity.getRow() > rowIndex) {
entity.setRow(entity.getRow() + size);
}
}
} }
private static int indexOfIgnoreCase(String str, String searchStr, int startPos) { private static int indexOfIgnoreCase(String str, String searchStr, int startPos) {
@@ -123,7 +151,7 @@ public class TemplateSumHanlder {
/** /**
* 最后值 * 最后值
*/ */
private Object value;
private double value;
public String getCellValue() { public String getCellValue() {
return cellValue; return cellValue;
@@ -157,11 +185,11 @@ public class TemplateSumHanlder {
this.row = row; this.row = row;
} }
public Object getValue() {
public double getValue() {
return value; return value;
} }
public void setValue(Object value) {
public void setValue(double value) {
this.value = value; this.value = value;
} }


Loading…
Cancel
Save