@@ -129,6 +129,57 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
mergeCells(sheet, excelParams, titleHeight); | mergeCells(sheet, excelParams, titleHeight); | ||||
} | } | ||||
/** | |||||
* 利用foreach循环输出数据 | |||||
* @param cell | |||||
* @param map | |||||
* @param name | |||||
* @throws Exception | |||||
*/ | |||||
private void addListDataToExcel(Cell cell, Map<String, Object> map, | |||||
String name) throws Exception { | |||||
boolean isCreate = !name.contains(FOREACH_NOT_CREATE); | |||||
boolean isShift = name.contains(FOREACH_AND_SHIFT); | |||||
name = name.replace(FOREACH_NOT_CREATE, EMPTY).replace(FOREACH_AND_SHIFT, EMPTY) | |||||
.replace(FOREACH, 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]; | |||||
Row row = null; | |||||
int rowIndex = cell.getRow().getRowNum() + 1; | |||||
//处理当前行 | |||||
if (its.hasNext()) { | |||||
Object t = its.next(); | |||||
setForEeachRowCellValue(isCreate, cell.getRow(), cell.getColumnIndex(), t, columns, map, | |||||
rowspan, colspan, mergedRegionHelper); | |||||
rowIndex += rowspan - 1; | |||||
} | |||||
//不论后面有没有数据,都应该执行的是插入操作 | |||||
// && 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); | |||||
templateSumHandler.shiftRows(cell.getRowIndex(),(datas.size() - 1) * rowspan); | |||||
} | |||||
while (its.hasNext()) { | |||||
Object t = its.next(); | |||||
row = createRow(rowIndex, cell.getSheet(), isCreate, rowspan); | |||||
setForEeachRowCellValue(isCreate, row, cell.getColumnIndex(), t, columns, map, rowspan, | |||||
colspan, mergedRegionHelper); | |||||
rowIndex += rowspan; | |||||
} | |||||
} | |||||
/** | /** | ||||
* 下移数据 | * 下移数据 | ||||
* @param dataSet | * @param dataSet | ||||
@@ -436,56 +487,6 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
|| text.contains(" " + NUMBER_SYMBOL); | || text.contains(" " + NUMBER_SYMBOL); | ||||
} | } | ||||
/** | |||||
* 利用foreach循环输出数据 | |||||
* @param cell | |||||
* @param map | |||||
* @param name | |||||
* @throws Exception | |||||
*/ | |||||
private void addListDataToExcel(Cell cell, Map<String, Object> map, | |||||
String name) throws Exception { | |||||
boolean isCreate = !name.contains(FOREACH_NOT_CREATE); | |||||
boolean isShift = name.contains(FOREACH_AND_SHIFT); | |||||
name = name.replace(FOREACH_NOT_CREATE, EMPTY).replace(FOREACH_AND_SHIFT, EMPTY) | |||||
.replace(FOREACH, 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]; | |||||
Row row = null; | |||||
int rowIndex = cell.getRow().getRowNum() + 1; | |||||
//处理当前行 | |||||
if (its.hasNext()) { | |||||
Object t = its.next(); | |||||
setForEeachRowCellValue(isCreate, cell.getRow(), cell.getColumnIndex(), t, columns, map, | |||||
rowspan, colspan, mergedRegionHelper); | |||||
rowIndex += rowspan - 1; | |||||
} | |||||
//不论后面有没有数据,都应该执行的是插入操作 | |||||
// && 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); | |||||
templateSumHandler.shiftRows(cell.getRowIndex(),(datas.size() - 1) * rowspan); | |||||
} | |||||
while (its.hasNext()) { | |||||
Object t = its.next(); | |||||
row = createRow(rowIndex, cell.getSheet(), isCreate, rowspan); | |||||
setForEeachRowCellValue(isCreate, row, cell.getColumnIndex(), t, columns, map, rowspan, | |||||
colspan, mergedRegionHelper); | |||||
rowIndex += rowspan; | |||||
} | |||||
} | |||||
private void createRowNoRow(int startRow, int lastRowNum, int i1) { | private void createRowNoRow(int startRow, int lastRowNum, int i1) { | ||||
} | } | ||||
@@ -1,12 +1,12 @@ | |||||
/** | /** | ||||
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | ||||
* | |||||
* <p> | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
* You may obtain a copy of the License at | * You may obtain a copy of the License at | ||||
* | |||||
* <p> | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | * http://www.apache.org/licenses/LICENSE-2.0 | ||||
* | |||||
* <p> | |||||
* Unless required by applicable law or agreed to in writing, software | * Unless required by applicable law or agreed to in writing, software | ||||
* distributed under the License is distributed on an "AS IS" BASIS, | * distributed under the License is distributed on an "AS IS" BASIS, | ||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
@@ -30,6 +30,7 @@ import java.util.Map; | |||||
import cn.afterturn.easypoi.handler.inter.IExcelDictHandler; | import cn.afterturn.easypoi.handler.inter.IExcelDictHandler; | ||||
import cn.afterturn.easypoi.util.PoiReflectorUtil; | import cn.afterturn.easypoi.util.PoiReflectorUtil; | ||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import org.apache.commons.lang3.time.DateUtils; | |||||
import org.apache.poi.ss.usermodel.Cell; | import org.apache.poi.ss.usermodel.Cell; | ||||
import org.apache.poi.ss.usermodel.CellType; | import org.apache.poi.ss.usermodel.CellType; | ||||
import org.apache.poi.ss.usermodel.DateUtil; | import org.apache.poi.ss.usermodel.DateUtil; | ||||
@@ -205,7 +206,7 @@ public class CellValueService { | |||||
result = handlerSuffix(entity.getSuffix(), result); | result = handlerSuffix(entity.getSuffix(), result); | ||||
result = replaceValue(entity.getReplace(), result); | result = replaceValue(entity.getReplace(), result); | ||||
result = replaceValue(entity.getReplace(), result); | result = replaceValue(entity.getReplace(), result); | ||||
if(dictHandler != null && StringUtils.isNoneBlank(entity.getDict())){ | |||||
if (dictHandler != null && StringUtils.isNoneBlank(entity.getDict())) { | |||||
dictHandler.toValue(entity.getDict(), object, entity.getName(), result); | dictHandler.toValue(entity.getDict(), object, entity.getName(), result); | ||||
} | } | ||||
} | } | ||||
@@ -267,7 +268,12 @@ public class CellValueService { | |||||
if (result == null || StringUtils.isBlank(result.toString())) { | if (result == null || StringUtils.isBlank(result.toString())) { | ||||
return null; | return null; | ||||
} | } | ||||
if ("class java.util.Date".equals(classFullName)) { | |||||
if ("class java.util.Date".equals(classFullName) && result instanceof Date) { | |||||
return result; | |||||
} else if ("class java.util.Date".equals(classFullName) && result instanceof String) { | |||||
return DateUtils.parseDate(result.toString(), entity.getFormat()); | |||||
} | |||||
if ("class java.sql.Date".equals(classFullName) && result instanceof java.sql.Date) { | |||||
return result; | return result; | ||||
} | } | ||||
if ("class java.lang.Boolean".equals(classFullName) || "boolean".equals(classFullName)) { | if ("class java.lang.Boolean".equals(classFullName) || "boolean".equals(classFullName)) { | ||||
@@ -311,9 +317,9 @@ public class CellValueService { | |||||
} | } | ||||
if (clazz != null && clazz.isEnum()) { | if (clazz != null && clazz.isEnum()) { | ||||
if (StringUtils.isNotEmpty(entity.getEnumImportMethod())) { | if (StringUtils.isNotEmpty(entity.getEnumImportMethod())) { | ||||
return PoiReflectorUtil.fromCache(clazz).execEnumStaticMethod(entity.getEnumImportMethod(),result); | |||||
return PoiReflectorUtil.fromCache(clazz).execEnumStaticMethod(entity.getEnumImportMethod(), result); | |||||
} else { | } else { | ||||
return Enum.valueOf(clazz,result.toString()); | |||||
return Enum.valueOf(clazz, result.toString()); | |||||
} | } | ||||
} | } | ||||
return result; | return result; | ||||
@@ -19,6 +19,7 @@ import java.math.BigDecimal; | |||||
import java.util.Date; | import java.util.Date; | ||||
import java.util.List; | import java.util.List; | ||||
import org.apache.commons.lang3.StringUtils; | |||||
import org.apache.poi.hssf.usermodel.HSSFDateUtil; | import org.apache.poi.hssf.usermodel.HSSFDateUtil; | ||||
import org.apache.poi.xssf.model.SharedStringsTable; | import org.apache.poi.xssf.model.SharedStringsTable; | ||||
import org.apache.poi.xssf.usermodel.XSSFRichTextString; | import org.apache.poi.xssf.usermodel.XSSFRichTextString; | ||||
@@ -119,14 +120,18 @@ public class SheetHandler extends DefaultHandler { | |||||
rowlist.add(curCol, new SaxReadCellEntity(CellValueType.String, value)); | rowlist.add(curCol, new SaxReadCellEntity(CellValueType.String, value)); | ||||
} | } | ||||
curCol++; | curCol++; | ||||
} else if ("row".equals(name)) {//如果标签名称为 row ,这说明已到行尾,调用 optRows() 方法 | |||||
//如果标签名称为 row ,这说明已到行尾,调用 optRows() 方法 | |||||
} else if("c".equals(name) && StringUtils.isEmpty(lastContents)){ | |||||
rowlist.add(curCol,new SaxReadCellEntity(CellValueType.String,"")); | |||||
curCol++; | |||||
} else if ("row".equals(name)) { | |||||
read.parse(curRow, rowlist); | read.parse(curRow, rowlist); | ||||
rowlist.clear(); | rowlist.clear(); | ||||
curRow++; | curRow++; | ||||
curCol = 0; | curCol = 0; | ||||
} | } | ||||
} | |||||
} | |||||
@Override | @Override | ||||
public void characters(char[] ch, int start, int length) throws SAXException { | public void characters(char[] ch, int start, int length) throws SAXException { | ||||