@@ -45,8 +45,8 @@ public class FileLoaderImpl implements IFileLoader { | |||
if (url.startsWith("http")) { | |||
URL urlObj = new URL(url); | |||
URLConnection urlConnection = urlObj.openConnection(); | |||
urlConnection.setConnectTimeout(30); | |||
urlConnection.setReadTimeout(60); | |||
urlConnection.setConnectTimeout(30 * 1000); | |||
urlConnection.setReadTimeout(60 * 1000); | |||
urlConnection.setDoInput(true); | |||
fileis = urlConnection.getInputStream(); | |||
} else { | |||
@@ -224,8 +224,8 @@ public class CsvImportService extends ImportBaseService { | |||
private void saveFieldValue(CsvImportParams params, Object object, String cell, | |||
Map<String, ExcelImportEntity> excelParams, String titleString) throws Exception { | |||
if (cell.startsWith(params.getTextMark()) && cell.endsWith(params.getTextMark())) { | |||
cell = cell.replaceFirst(cell, params.getTextMark()); | |||
cell = cell.substring(0, cell.lastIndexOf(params.getTextMark())); | |||
//FIXED 字符串截取时考虑的情况不全面导致的BUG | |||
cell = cell.substring(params.getTextMark().length(), cell.lastIndexOf(params.getTextMark())); | |||
} | |||
Object value = cellValueServer.getValue(params.getDataHandler(), object, cell, excelParams, | |||
titleString, params.getDictHandler()); | |||
@@ -45,6 +45,9 @@ public final class ExcelExportUtil { | |||
} | |||
/** | |||
* 大数据量导出,方便导出时集合不用一次生成,可以多次生成,多次调用, | |||
* 调用完成或需要调用关闭函数(closeExportBigExcel) | |||
* | |||
* @param entity | |||
* 表格标题属性 | |||
* @param pojoClass | |||
@@ -59,6 +62,14 @@ public final class ExcelExportUtil { | |||
return batchService.appendData(dataSet); | |||
} | |||
/** | |||
* 大数据量导出,方便导出时集合不用一次生成,可以多次生成,多次调用, | |||
* 调用完成或需要调用关闭函数(closeExportBigExcel) | |||
* @param entity | |||
* @param excelParams | |||
* @param dataSet | |||
* @return | |||
*/ | |||
public static Workbook exportBigExcel(ExportParams entity, List<ExcelExportEntity> excelParams, | |||
Collection<?> dataSet) { | |||
ExcelBatchExportService batchService = ExcelBatchExportService | |||
@@ -66,6 +77,9 @@ public final class ExcelExportUtil { | |||
return batchService.appendData(dataSet); | |||
} | |||
/** | |||
* 执行完成上述方法后,请关闭 | |||
*/ | |||
public static void closeExportBigExcel() { | |||
ExcelBatchExportService batchService = ExcelBatchExportService.getCurrentExcelBatchExportService(); | |||
if(batchService != null) { | |||
@@ -115,7 +129,7 @@ public final class ExcelExportUtil { | |||
} | |||
/** | |||
* 一个excel 创建多个sheet | |||
* 根据Map创建对应的Excel(一个excel 创建多个sheet) | |||
* | |||
* @param list | |||
* 多个Map key title 对应表格Title key entity 对应表格对应实体 key data | |||
@@ -68,10 +68,11 @@ public class ExportParams extends ExcelBaseParams { | |||
*/ | |||
private int freezeCol; | |||
/** | |||
* 表头颜色 | |||
* 表头颜色 & 标题颜色 | |||
*/ | |||
private short color = HSSFColor.HSSFColorPredefined.WHITE.getIndex(); | |||
/** | |||
* 第二行标题颜色 | |||
* 属性说明行的颜色 例如:HSSFColor.SKY_BLUE.index 默认 | |||
*/ | |||
private short headerColor = HSSFColor.HSSFColorPredefined.SKY_BLUE.getIndex(); | |||
@@ -114,7 +115,7 @@ public class ExportParams extends ExcelBaseParams { | |||
* 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符 | |||
* 全局设置,优先使用 | |||
*/ | |||
public short height = 0; | |||
private short height = 0; | |||
public ExportParams() { | |||
@@ -0,0 +1,72 @@ | |||
package cn.afterturn.easypoi.excel.entity; | |||
/** | |||
* 统计对象 | |||
* | |||
* @author JueYue | |||
*/ | |||
public class TemplateSumEntity { | |||
/** | |||
* CELL的值 | |||
*/ | |||
private String cellValue; | |||
/** | |||
* 需要计算的KEY | |||
*/ | |||
private String sumKey; | |||
/** | |||
* 列 | |||
*/ | |||
private int col; | |||
/** | |||
* 行 | |||
*/ | |||
private int row; | |||
/** | |||
* 最后值 | |||
*/ | |||
private double value; | |||
public String getCellValue() { | |||
return cellValue; | |||
} | |||
public void setCellValue(String cellValue) { | |||
this.cellValue = cellValue; | |||
} | |||
public String getSumKey() { | |||
return sumKey; | |||
} | |||
public void setSumKey(String sumKey) { | |||
this.sumKey = sumKey; | |||
} | |||
public int getCol() { | |||
return col; | |||
} | |||
public void setCol(int col) { | |||
this.col = col; | |||
} | |||
public int getRow() { | |||
return row; | |||
} | |||
public void setRow(int row) { | |||
this.row = row; | |||
} | |||
public double getValue() { | |||
return value; | |||
} | |||
public void setValue(double value) { | |||
this.value = value; | |||
} | |||
} |
@@ -13,5 +13,6 @@ package cn.afterturn.easypoi.excel.entity.vo; | |||
public interface BaseEntityTypeConstants { | |||
public final static Integer STRING_TYPE = 1; | |||
public final static Integer DOUBLE_TYPE = 10; | |||
public final static Integer IMAGE_TYPE = 3; | |||
public final static Integer IMAGE_TYPE = 2; | |||
public final static Integer FUNCTION_TYPE = 3; | |||
} |
@@ -139,6 +139,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0; | |||
titleHeight = index; | |||
setCellWith(excelParams, sheet); | |||
setColumnHidden(excelParams,sheet); | |||
rowHeight = getRowHeight(excelParams); | |||
setCurrentIndex(1); | |||
} catch (Exception e) { | |||
@@ -173,7 +174,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
public void closeExportBigExcel() { | |||
if (entity.getFreezeCol() != 0) { | |||
sheet.createFreezePane(entity.getFreezeCol(), 0, entity.getFreezeCol(), 0); | |||
sheet.createFreezePane(entity.getFreezeCol(), titleHeight, entity.getFreezeCol(), titleHeight); | |||
} | |||
mergeCells(sheet, excelParams, titleHeight); | |||
// 创建合计信息 | |||
@@ -82,7 +82,7 @@ public class ExcelExportService extends BaseExportService { | |||
for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { | |||
ExcelExportEntity entity = excelParams.get(i); | |||
// 加入换了groupName或者结束就,就把之前的那个换行 | |||
if (StringUtils.isBlank(entity.getGroupName()) || i = 0 || !entity.getGroupName().equals(excelParams.get(i - 1).getGroupName())) { | |||
if (StringUtils.isBlank(entity.getGroupName()) || i == 0 || !entity.getGroupName().equals(excelParams.get(i - 1).getGroupName())) { | |||
if (groupCellLength > 1) { | |||
sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex - groupCellLength, cellIndex - 1)); | |||
} | |||
@@ -13,22 +13,6 @@ | |||
*/ | |||
package cn.afterturn.easypoi.excel.export.base; | |||
import org.apache.commons.lang3.StringUtils; | |||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |||
import org.apache.poi.hssf.usermodel.HSSFClientAnchor; | |||
import org.apache.poi.hssf.usermodel.HSSFRichTextString; | |||
import org.apache.poi.ss.usermodel.*; | |||
import org.apache.poi.ss.util.CellRangeAddress; | |||
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; | |||
import org.apache.poi.xssf.usermodel.XSSFRichTextString; | |||
import java.text.DecimalFormat; | |||
import java.util.Collection; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import cn.afterturn.easypoi.cache.ImageCache; | |||
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | |||
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||
@@ -40,6 +24,16 @@ import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | |||
import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | |||
import cn.afterturn.easypoi.util.PoiMergeCellUtil; | |||
import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
import org.apache.commons.lang3.StringUtils; | |||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |||
import org.apache.poi.hssf.usermodel.HSSFClientAnchor; | |||
import org.apache.poi.hssf.usermodel.HSSFRichTextString; | |||
import org.apache.poi.ss.usermodel.*; | |||
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; | |||
import org.apache.poi.xssf.usermodel.XSSFRichTextString; | |||
import java.text.DecimalFormat; | |||
import java.util.*; | |||
/** | |||
* 提供POI基础操作服务 | |||
@@ -67,37 +61,31 @@ public abstract class BaseExportService extends ExportCommonService { | |||
short rowHeight, int cellNum) { | |||
try { | |||
ExcelExportEntity entity; | |||
Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); | |||
Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); | |||
if (rowHeight != -1) { | |||
row.setHeight(rowHeight); | |||
} | |||
int maxHeight = 1; | |||
int maxHeight = 1, listMaxHeight = 1; | |||
// 合并需要合并的单元格 | |||
int margeCellNum = cellNum; | |||
int indexKey = createIndexCell(row, index, excelParams.get(0)); | |||
int indexKey = createIndexCell(row, index, excelParams.get(0)); | |||
cellNum += indexKey; | |||
for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||
entity = excelParams.get(k); | |||
if (entity.getList() != null) { | |||
Collection<?> list = getListCellValue(entity, t); | |||
int listC = 0; | |||
Collection<?> list = getListCellValue(entity, t); | |||
int listIndex = 0, tmpListHeight = 0; | |||
if (list != null && list.size() > 0) { | |||
int tempCellNum = 0; | |||
for (Object obj : list) { | |||
int[] temp = createCells(patriarch, index + maxHeight - 1, obj, entity.getList(), sheet, workbook, rowHeight, cellNum); | |||
int[] temp = createCells(patriarch, index + listIndex, obj, entity.getList(), sheet, workbook, rowHeight, cellNum); | |||
tempCellNum = temp[1]; | |||
maxHeight += temp[0]; | |||
/*createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), | |||
sheet, workbook, rowHeight); | |||
listC++;*/ | |||
tmpListHeight += temp[0]; | |||
listIndex++; | |||
} | |||
cellNum = tempCellNum; | |||
maxHeight--; | |||
listMaxHeight = Math.max(listMaxHeight, tmpListHeight); | |||
} | |||
/* cellNum += entity.getList().size(); | |||
if (list != null && list.size() > maxHeight) { | |||
maxHeight = list.size(); | |||
}*/ | |||
} else { | |||
Object value = getCellValue(entity, t); | |||
@@ -126,6 +114,10 @@ public abstract class BaseExportService extends ExportCommonService { | |||
} | |||
} | |||
} | |||
maxHeight += listMaxHeight - 1; | |||
if(indexKey == 1 && excelParams.get(1).isNeedMerge()) { | |||
excelParams.get(0).setNeedMerge(true); | |||
} | |||
for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||
entity = excelParams.get(k); | |||
if (entity.getList() != null) { | |||
@@ -153,7 +145,7 @@ public abstract class BaseExportService extends ExportCommonService { | |||
*/ | |||
public void createImageCell(Drawing patriarch, ExcelExportEntity entity, Row row, int i, | |||
String imagePath, Object obj) throws Exception { | |||
Cell cell = row.createCell(i); | |||
Cell cell = row.createCell(i); | |||
byte[] value = null; | |||
if (entity.getExportImageType() != 1) { | |||
value = (byte[]) (entity.getMethods() != null | |||
@@ -191,6 +183,32 @@ public abstract class BaseExportService extends ExportCommonService { | |||
} | |||
/** | |||
* 图片类型的Cell | |||
*/ | |||
public void createImageCell(Cell cell, double height, int rowspan, int colspan, | |||
String imagePath, byte[] data) throws Exception { | |||
if (height > cell.getRow().getHeight()) { | |||
cell.getRow().setHeight((short) height); | |||
} | |||
ClientAnchor anchor; | |||
if (type.equals(ExcelType.HSSF)) { | |||
anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + colspan), | |||
cell.getRow().getRowNum() + rowspan); | |||
} else { | |||
anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + colspan), | |||
cell.getRow().getRowNum() + rowspan); | |||
} | |||
if (StringUtils.isNotEmpty(imagePath)) { | |||
data = ImageCache.getImage(imagePath); | |||
} | |||
if (data != null) { | |||
PoiExcelGraphDataUtil.getDrawingPatriarch(cell.getSheet()).createPicture(anchor, | |||
cell.getSheet().getWorkbook().addPicture(data, getImageType(data))); | |||
} | |||
} | |||
private int createIndexCell(Row row, int index, ExcelExportEntity excelExportEntity) { | |||
if (excelExportEntity.getName() != null && "序号".equals(excelExportEntity.getName()) && excelExportEntity.getFormat() != null | |||
&& excelExportEntity.getFormat().equals(PoiBaseConstants.IS_ADD_INDEX)) { | |||
@@ -209,7 +227,7 @@ public abstract class BaseExportService extends ExportCommonService { | |||
List<ExcelExportEntity> excelParams, Sheet sheet, | |||
Workbook workbook, short rowHeight) throws Exception { | |||
ExcelExportEntity entity; | |||
Row row; | |||
Row row; | |||
if (sheet.getRow(index) == null) { | |||
row = sheet.createRow(index); | |||
if (rowHeight != -1) { | |||
@@ -285,7 +303,7 @@ public abstract class BaseExportService extends ExportCommonService { | |||
try { | |||
cell.setCellValue(Double.parseDouble(text)); | |||
} catch (NumberFormatException e) { | |||
cell.setCellType(Cell.CELL_TYPE_STRING); | |||
cell.setCellType(CellType.STRING); | |||
cell.setCellValue(text); | |||
} | |||
} | |||
@@ -304,7 +322,7 @@ public abstract class BaseExportService extends ExportCommonService { | |||
if (LOGGER.isDebugEnabled()) { | |||
LOGGER.debug("add statistics data ,size is {}", statistics.size()); | |||
} | |||
Row row = sheet.createRow(sheet.getLastRowNum() + 1); | |||
Row row = sheet.createRow(sheet.getLastRowNum() + 1); | |||
Set<Integer> keys = statistics.keySet(); | |||
createStringCell(row, 0, "合计", styles, null); | |||
for (Integer key : keys) { | |||
@@ -39,6 +39,7 @@ import java.lang.reflect.Method; | |||
import java.lang.reflect.ParameterizedType; | |||
import java.text.DecimalFormat; | |||
import java.text.SimpleDateFormat; | |||
import java.time.Instant; | |||
import java.time.LocalDate; | |||
import java.time.LocalDateTime; | |||
import java.time.ZoneId; | |||
@@ -86,7 +87,10 @@ public class ExportCommonService { | |||
temp = format.parse(value.toString()); | |||
} else if (value instanceof Date) { | |||
temp = (Date) value; | |||
} else if (value instanceof LocalDate) { | |||
} else if (value instanceof Instant) { | |||
Instant instant = (Instant)value; | |||
temp = Date.from(instant); | |||
} else if (value instanceof LocalDate) { | |||
LocalDate localDate = (LocalDate)value; | |||
temp = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); | |||
} else if(value instanceof LocalDateTime){ | |||
@@ -28,6 +28,7 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import cn.afterturn.easypoi.excel.entity.TemplateSumEntity; | |||
import cn.afterturn.easypoi.util.*; | |||
import org.apache.commons.lang3.StringUtils; | |||
import org.apache.poi.ss.usermodel.*; | |||
@@ -45,7 +46,6 @@ import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; | |||
import cn.afterturn.easypoi.excel.export.base.BaseExportService; | |||
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; | |||
import cn.afterturn.easypoi.excel.export.template.TemplateSumHandler.TemplateSumEntity; | |||
import cn.afterturn.easypoi.excel.html.helper.MergedRegionHelper; | |||
import cn.afterturn.easypoi.exception.excel.ExcelExportException; | |||
import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | |||
@@ -320,10 +320,13 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
} | |||
//修改需要处理的统计值 | |||
hanlderSumCell(sheet); | |||
handlerSumCell(sheet); | |||
//修复因为调用shiftRows而被破坏的合并单元格 | |||
mergedRegionHelper.mergeOtherCell(sheet); | |||
} | |||
private void hanlderSumCell(Sheet sheet) { | |||
private void handlerSumCell(Sheet sheet) { | |||
for (TemplateSumEntity sumEntity : templateSumHandler.getDataList()) { | |||
Cell cell = sheet.getRow(sumEntity.getRow()).getCell(sumEntity.getCol()); | |||
cell.setCellValue(cell.getStringCellValue() | |||
@@ -463,7 +466,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
PoiMergeCellUtil.addMergedRegion(cell.getSheet(), cell.getRowIndex(), | |||
cell.getRowIndex() + img.getRowspan() - 1, cell.getColumnIndex(), cell.getColumnIndex() + img.getColspan() - 1); | |||
} | |||
createImageCell(cell, img.getHeight(), img.getUrl(), img.getData()); | |||
createImageCell(cell, img.getHeight(), img.getRowspan(), img.getColspan(), img.getUrl(), img.getData()); | |||
} else if (isNumber && StringUtils.isNotBlank(obj.toString())) { | |||
cell.setCellValue(Double.parseDouble(obj.toString())); | |||
cell.setCellType(CellType.NUMERIC); | |||
@@ -576,7 +579,12 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
if (obj != null && obj instanceof ImageEntity) { | |||
ImageEntity img = (ImageEntity) obj; | |||
row.getCell(ci).setCellValue(""); | |||
createImageCell(row.getCell(ci), img.getHeight(), img.getUrl(), img.getData()); | |||
if (img.getRowspan()>1 || img.getColspan() > 1){ | |||
img.setHeight(0); | |||
row.getCell(ci).getSheet().addMergedRegion(new CellRangeAddress(row.getCell(ci).getRowIndex(), | |||
row.getCell(ci).getRowIndex() + img.getRowspan() - 1, row.getCell(ci).getColumnIndex(), row.getCell(ci).getColumnIndex() + img.getColspan() -1)); | |||
} | |||
createImageCell(row.getCell(ci), img.getHeight(),img.getRowspan(),img.getColspan(), img.getUrl(), img.getData()); | |||
} else if (isNumber && StringUtils.isNotEmpty(val)) { | |||
row.getCell(ci).setCellValue(Double.parseDouble(val)); | |||
row.getCell(ci).setCellType(CellType.NUMERIC); | |||
@@ -596,7 +604,8 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
setMergedRegionStyle(row, ci, params); | |||
//合并对应单元格 | |||
if ((params.getRowspan() != 1 || params.getColspan() != 1) | |||
&& !mergedRegionHelper.isMergedRegion(row.getRowNum() + 1, ci)) { | |||
&& !mergedRegionHelper.isMergedRegion(row.getRowNum() + 1, ci) | |||
&& PoiCellUtil.isMergedRegion(row.getSheet(), row.getRowNum(), ci)) { | |||
PoiMergeCellUtil.addMergedRegion(row.getSheet(), row.getRowNum(), | |||
row.getRowNum() + params.getRowspan() - 1, ci, | |||
ci + params.getColspan() - 1); | |||
@@ -7,6 +7,7 @@ import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import cn.afterturn.easypoi.excel.entity.TemplateSumEntity; | |||
import org.apache.commons.lang3.StringUtils; | |||
import org.apache.poi.ss.usermodel.Cell; | |||
import org.apache.poi.ss.usermodel.Row; | |||
@@ -127,72 +128,5 @@ public class TemplateSumHandler { | |||
return -1; | |||
} | |||
/** | |||
* 统计对象 | |||
* @author JueYue | |||
*/ | |||
protected class TemplateSumEntity { | |||
/** | |||
* CELL的值 | |||
*/ | |||
private String cellValue; | |||
/** | |||
* 需要计算的KEY | |||
*/ | |||
private String sumKey; | |||
/** | |||
* 列 | |||
*/ | |||
private int col; | |||
/** | |||
* 行 | |||
*/ | |||
private int row; | |||
/** | |||
* 最后值 | |||
*/ | |||
private double value; | |||
public String getCellValue() { | |||
return cellValue; | |||
} | |||
public void setCellValue(String cellValue) { | |||
this.cellValue = cellValue; | |||
} | |||
public String getSumKey() { | |||
return sumKey; | |||
} | |||
public void setSumKey(String sumKey) { | |||
this.sumKey = sumKey; | |||
} | |||
public int getCol() { | |||
return col; | |||
} | |||
public void setCol(int col) { | |||
this.col = col; | |||
} | |||
public int getRow() { | |||
return row; | |||
} | |||
public void setRow(int row) { | |||
this.row = row; | |||
} | |||
public double getValue() { | |||
return value; | |||
} | |||
public void setValue(double value) { | |||
this.value = value; | |||
} | |||
} | |||
} |
@@ -5,6 +5,7 @@ import java.util.HashSet; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import cn.afterturn.easypoi.util.PoiCellUtil; | |||
import cn.afterturn.easypoi.util.PoiMergeCellUtil; | |||
import org.apache.poi.ss.usermodel.Sheet; | |||
import org.apache.poi.ss.util.CellRangeAddress; | |||
@@ -123,4 +124,23 @@ public class MergedRegionHelper { | |||
} | |||
} | |||
} | |||
/** | |||
* 把破坏的单元格合并(POI的shiftRows方法会把移动的行的合并单元格拆分) | |||
* @return | |||
*/ | |||
public void mergeOtherCell(Sheet sheet) { | |||
Set<String> tempMergeKeySet = mergedCache.keySet(); | |||
for(String mergeKey : tempMergeKeySet) { | |||
String[] row_col = mergeKey.split("_"); | |||
if(!PoiCellUtil.isMergedRegion(sheet, Integer.valueOf(row_col[0]) - 1, Integer.valueOf(row_col[1]))) { | |||
Integer[] mergeCellValue = mergedCache.get(mergeKey); | |||
sheet.addMergedRegion(new CellRangeAddress(Integer.valueOf(row_col[0]) - 1, | |||
Integer.valueOf(row_col[0]) - 1 + mergeCellValue[0] - 1, Integer.valueOf(row_col[1]), | |||
Integer.valueOf(row_col[1]) + mergeCellValue[1] - 1)); | |||
} | |||
} | |||
} | |||
} |
@@ -38,6 +38,8 @@ import java.sql.Time; | |||
import java.sql.Timestamp; | |||
import java.text.ParseException; | |||
import java.text.SimpleDateFormat; | |||
import java.time.LocalDate; | |||
import java.time.ZoneId; | |||
import java.util.Arrays; | |||
import java.util.Date; | |||
import java.util.List; | |||
@@ -70,15 +72,10 @@ public class CellValueService { | |||
Object result = null; | |||
if ("class java.util.Date".equals(classFullName) || "class java.sql.Date".equals(classFullName) | |||
|| ("class java.sql.Time").equals(classFullName) | |||
|| ("class java.time.Instant").equals(classFullName) | |||
|| ("class java.time.LocalDate").equals(classFullName) | |||
|| ("class java.time.LocalDateTime").equals(classFullName) | |||
|| ("class java.sql.Timestamp").equals(classFullName)) { | |||
/* | |||
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { | |||
// 日期格式 | |||
result = cell.getDateCellValue(); | |||
} else { | |||
cell.setCellType(Cell.CELL_TYPE_STRING); | |||
result = getDateData(entity, cell.getStringCellValue()); | |||
}*/ | |||
//FIX: 单元格yyyyMMdd数字时候使用 cell.getDateCellValue() 解析出的日期错误 | |||
if (CellType.NUMERIC == cell.getCellType() && DateUtil.isCellDateFormatted(cell)) { | |||
result = DateUtil.getJavaDate(cell.getNumericCellValue()); | |||
@@ -96,7 +93,13 @@ public class CellValueService { | |||
return null; | |||
} | |||
} | |||
if (("class java.sql.Date").equals(classFullName)) { | |||
if (("class java.time.Instant").equals(classFullName)) { | |||
result = ((Date) result).toInstant(); | |||
} else if (("class java.time.LocalDate").equals(classFullName)) { | |||
result = ((Date) result).toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); | |||
} else if (("class java.time.LocalDateTime").equals(classFullName)) { | |||
result = ((Date) result).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); | |||
} else if (("class java.sql.Date").equals(classFullName)) { | |||
result = new java.sql.Date(((Date) result).getTime()); | |||
} else if (("class java.sql.Time").equals(classFullName)) { | |||
result = new Time(((Date) result).getTime()); | |||
@@ -197,43 +200,10 @@ public class CellValueService { | |||
* @param titleString | |||
* @param dictHandler | |||
*/ | |||
public Object getValue(IExcelDataHandler<?> dataHandler, Object object, String cell, | |||
public Object getValue(IExcelDataHandler<?> dataHandler, Object object, Object cell, | |||
Map<String, ExcelImportEntity> excelParams, | |||
String titleString, IExcelDictHandler dictHandler) throws Exception { | |||
ExcelImportEntity entity = excelParams.get(titleString); | |||
String classFullName = "class java.lang.Object"; | |||
Class clazz = null; | |||
if (!(object instanceof Map)) { | |||
Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0 | |||
? entity.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod(); | |||
Type[] ts = setMethod.getGenericParameterTypes(); | |||
classFullName = ts[0].toString(); | |||
clazz = (Class) ts[0]; | |||
} | |||
Object result = cell; | |||
if (entity != null) { | |||
result = handlerSuffix(entity.getSuffix(), result); | |||
result = replaceValue(entity.getReplace(), result); | |||
if (dictHandler != null && StringUtils.isNoneBlank(entity.getDict())) { | |||
result = dictHandler.toValue(entity.getDict(), object, entity.getName(), result); | |||
} | |||
} | |||
result = handlerValue(dataHandler, object, result, titleString); | |||
return getValueByType(classFullName, result, entity, clazz); | |||
} | |||
/** | |||
* 获取cell的值 | |||
* | |||
* @param object | |||
* @param cell | |||
* @param excelParams | |||
* @param titleString | |||
* @param dictHandler | |||
*/ | |||
public Object getValue(IExcelDataHandler<?> dataHandler, Object object, Cell cell, | |||
Map<String, ExcelImportEntity> excelParams, | |||
String titleString, IExcelDictHandler dictHandler) throws Exception { | |||
ExcelImportEntity entity = excelParams.get(titleString); | |||
String classFullName = "class java.lang.Object"; | |||
Class clazz = null; | |||
@@ -244,13 +214,18 @@ public class CellValueService { | |||
classFullName = ts[0].toString(); | |||
clazz = (Class) ts[0]; | |||
} | |||
Object result = getCellValue(classFullName, cell, entity); | |||
Object result = null; | |||
if(cell instanceof Cell){ | |||
result = getCellValue(classFullName, (Cell) cell, entity); | |||
}else{ | |||
result = cell; | |||
} | |||
if (entity != null) { | |||
result = handlerSuffix(entity.getSuffix(), result); | |||
result = replaceValue(entity.getReplace(), result); | |||
result = replaceValue(entity.getReplace(), result); | |||
if (dictHandler != null && StringUtils.isNoneBlank(entity.getDict())) { | |||
dictHandler.toValue(entity.getDict(), object, entity.getName(), result); | |||
result = dictHandler.toValue(entity.getDict(), object, entity.getName(), result); | |||
} | |||
} | |||
result = handlerValue(dataHandler, object, result, titleString); | |||
@@ -19,6 +19,7 @@ import cn.afterturn.easypoi.excel.entity.params.ExcelCollectionParams; | |||
import cn.afterturn.easypoi.excel.entity.params.ExcelImportEntity; | |||
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; | |||
import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult; | |||
import cn.afterturn.easypoi.excel.entity.vo.BaseEntityTypeConstants; | |||
import cn.afterturn.easypoi.excel.imports.base.ImportBaseService; | |||
import cn.afterturn.easypoi.excel.imports.recursive.ExcelImportForkJoinWork; | |||
import cn.afterturn.easypoi.exception.excel.ExcelImportException; | |||
@@ -66,11 +67,12 @@ public class ExcelImportService extends ImportBaseService { | |||
private List<Row> successRow; | |||
private List<Row> failRow; | |||
private List failCollection = new ArrayList(); | |||
private List failCollection; | |||
public ExcelImportService() { | |||
successRow = new ArrayList<Row>(); | |||
failRow = new ArrayList<Row>(); | |||
failCollection = new ArrayList(); | |||
this.cellValueServer = new CellValueService(); | |||
} | |||
@@ -102,9 +104,9 @@ public class ExcelImportService extends ImportBaseService { | |||
Cell cell = row.getCell(i); | |||
String titleString = (String) titlemap.get(i); | |||
if (param.getExcelParams().containsKey(titleString)) { | |||
if (param.getExcelParams().get(titleString).getType() == 2) { | |||
if (param.getExcelParams().get(titleString).getType() == BaseEntityTypeConstants.IMAGE_TYPE) { | |||
picId = row.getRowNum() + "_" + i; | |||
saveImage(object, picId, param.getExcelParams(), titleString, pictures, params); | |||
saveImage(entity, picId, param.getExcelParams(), titleString, pictures, params); | |||
} else { | |||
try { | |||
saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row); | |||
@@ -158,7 +160,8 @@ public class ExcelImportService extends ImportBaseService { | |||
List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>(); | |||
String targetId = null; | |||
i18nHandler = params.getI18nHandler(); | |||
if (!Map.class.equals(pojoClass)) { | |||
boolean isMap = Map.class.equals(pojoClass); | |||
if (!isMap) { | |||
Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); | |||
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | |||
if (etarget != null) { | |||
@@ -207,6 +210,13 @@ public class ExcelImportService extends ImportBaseService { | |||
if (sheet.getLastRowNum() - row.getRowNum() < params.getLastOfInvalidRow()) { | |||
break; | |||
} | |||
/* 如果当前行的单元格都是无效的,那就继续下一行 */ | |||
if (row.getLastCellNum()<0) { | |||
continue; | |||
} | |||
if(isMap) { | |||
((Map) object).put("excelRowNum", row.getRowNum()); | |||
} | |||
errorMsg = new StringBuilder(); | |||
// 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象 | |||
// keyIndex 如果为空就不处理,仍然处理这一行 | |||
@@ -224,9 +234,9 @@ public class ExcelImportService extends ImportBaseService { | |||
for (Integer cn : keys) { | |||
Cell cell = row.getCell(cn); | |||
String titleString = (String) titlemap.get(cn); | |||
if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) { | |||
if (excelParams.containsKey(titleString) || isMap) { | |||
if (excelParams.get(titleString) != null | |||
&& excelParams.get(titleString).getType() == 2) { | |||
&& excelParams.get(titleString).getType() == BaseEntityTypeConstants.IMAGE_TYPE) { | |||
picId = row.getRowNum() + "_" + cn; | |||
saveImage(object, picId, excelParams, titleString, pictures, | |||
params); | |||
@@ -251,7 +261,7 @@ public class ExcelImportService extends ImportBaseService { | |||
for (ExcelCollectionParams param : excelCollection) { | |||
addListContinue(object, param, row, titlemap, targetId, pictures, params, errorMsg); | |||
} | |||
if (verifyingDataValidity(object, row, params, pojoClass, errorMsg)) { | |||
if (verifyingDataValidity(object, row, params, isMap, errorMsg)) { | |||
collection.add(object); | |||
} else { | |||
failCollection.add(object); | |||
@@ -276,7 +286,7 @@ public class ExcelImportService extends ImportBaseService { | |||
* 校验数据合法性 | |||
*/ | |||
public boolean verifyingDataValidity(Object object, Row row, ImportParams params, | |||
Class<?> pojoClass, StringBuilder fieldErrorMsg) { | |||
boolean isMap, StringBuilder fieldErrorMsg) { | |||
boolean isAdd = true; | |||
Cell cell = null; | |||
if (params.isNeedVerify()) { | |||
@@ -326,6 +336,9 @@ public class ExcelImportService extends ImportBaseService { | |||
if (cell != null) { | |||
cell.setCellStyle(errorCellStyle); | |||
failRow.add(row); | |||
if(isMap) { | |||
((Map) object).put("excelErrorMsg", cell.getStringCellValue()); | |||
} | |||
} else { | |||
successRow.add(row); | |||
} | |||
@@ -242,6 +242,7 @@ public final class PoiPublicUtil { | |||
|| "java.lang".equals(fieldType.getPackage().getName()) | |||
|| "java.math".equals(fieldType.getPackage().getName()) | |||
|| "java.sql".equals(fieldType.getPackage().getName()) | |||
|| "java.time".equals(fieldType.getPackage().getName()) | |||
|| "java.util".equals(fieldType.getPackage().getName())) { | |||
isBaseClass = true; | |||
} | |||
@@ -51,7 +51,7 @@ | |||
<guava.version>16.0.1</guava.version> | |||
<commons-lang.version>3.2.1</commons-lang.version> | |||
<slf4j.version>1.6.1</slf4j.version> | |||
<spring.version>5.0.6.RELEASE</spring.version> | |||
<spring.version>5.1.7.RELEASE</spring.version> | |||
</properties> | |||
<dependencyManagement> | |||
<dependencies> | |||