@@ -70,6 +70,11 @@ | |||||
<artifactId>commons-lang3</artifactId> | <artifactId>commons-lang3</artifactId> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>ognl</groupId> | |||||
<artifactId>ognl</artifactId> | |||||
</dependency> | |||||
<!--日志 --> | <!--日志 --> | ||||
<!-- slf4j --> | <!-- slf4j --> | ||||
<dependency> | <dependency> | ||||
@@ -17,6 +17,7 @@ package cn.afterturn.easypoi.excel.entity; | |||||
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler; | import cn.afterturn.easypoi.handler.inter.IExcelDataHandler; | ||||
import cn.afterturn.easypoi.handler.inter.IExcelDictHandler; | import cn.afterturn.easypoi.handler.inter.IExcelDictHandler; | ||||
import cn.afterturn.easypoi.handler.inter.IExcelI18nHandler; | |||||
/** | /** | ||||
* 基础参数 | * 基础参数 | ||||
@@ -35,6 +36,10 @@ public class ExcelBaseParams { | |||||
* 字段处理类 | * 字段处理类 | ||||
*/ | */ | ||||
private IExcelDictHandler dictHandler; | private IExcelDictHandler dictHandler; | ||||
/** | |||||
* 国际化处理类 | |||||
*/ | |||||
private IExcelI18nHandler i18nHandler; | |||||
public IExcelDataHandler getDataHandler() { | public IExcelDataHandler getDataHandler() { | ||||
return dataHandler; | return dataHandler; | ||||
@@ -51,4 +56,12 @@ public class ExcelBaseParams { | |||||
public void setDictHandler(IExcelDictHandler dictHandler) { | public void setDictHandler(IExcelDictHandler dictHandler) { | ||||
this.dictHandler = dictHandler; | this.dictHandler = dictHandler; | ||||
} | } | ||||
public IExcelI18nHandler getI18nHandler() { | |||||
return i18nHandler; | |||||
} | |||||
public void setI18nHandler(IExcelI18nHandler i18nHandler) { | |||||
this.i18nHandler = i18nHandler; | |||||
} | |||||
} | } |
@@ -154,6 +154,7 @@ public class ExcelExportService extends BaseExportService { | |||||
needHandlerList = Arrays.asList(dataHandler.getNeedHandlerFields()); | needHandlerList = Arrays.asList(dataHandler.getNeedHandlerFields()); | ||||
} | } | ||||
dictHandler = entity.getDictHandler(); | dictHandler = entity.getDictHandler(); | ||||
i18nHandler = entity.getI18nHandler(); | |||||
// 创建表格样式 | // 创建表格样式 | ||||
setExcelExportStyler((IExcelExportStyler) entity.getStyle() | setExcelExportStyler((IExcelExportStyler) entity.getStyle() | ||||
.getConstructor(Workbook.class).newInstance(workbook)); | .getConstructor(Workbook.class).newInstance(workbook)); | ||||
@@ -31,6 +31,7 @@ import java.util.List; | |||||
import java.util.Map; | import java.util.Map; | ||||
import cn.afterturn.easypoi.handler.inter.IExcelDictHandler; | import cn.afterturn.easypoi.handler.inter.IExcelDictHandler; | ||||
import cn.afterturn.easypoi.handler.inter.IExcelI18nHandler; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | ||||
import org.apache.commons.lang3.math.NumberUtils; | import org.apache.commons.lang3.math.NumberUtils; | ||||
@@ -61,6 +62,7 @@ public class ExportCommonService { | |||||
protected IExcelDataHandler dataHandler; | protected IExcelDataHandler dataHandler; | ||||
protected IExcelDictHandler dictHandler; | protected IExcelDictHandler dictHandler; | ||||
protected IExcelI18nHandler i18nHandler; | |||||
protected List<String> needHandlerList; | protected List<String> needHandlerList; | ||||
@@ -152,6 +154,9 @@ public class ExportCommonService { | |||||
PoiPublicUtil.getClassFields(clz), list, clz, null, null); | PoiPublicUtil.getClassFields(clz), list, clz, null, null); | ||||
excelEntity = new ExcelExportEntity(); | excelEntity = new ExcelExportEntity(); | ||||
excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); | excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); | ||||
if (i18nHandler == null) { | |||||
excelEntity.setName(i18nHandler.getLocaleName(excelEntity.getName())); | |||||
} | |||||
excelEntity.setOrderNum(Integer | excelEntity.setOrderNum(Integer | ||||
.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); | .valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); | ||||
excelEntity | excelEntity | ||||
@@ -258,6 +263,10 @@ public class ExportCommonService { | |||||
} else { | } else { | ||||
excelEntity.setGroupName(excel.groupName()); | excelEntity.setGroupName(excel.groupName()); | ||||
} | } | ||||
if (i18nHandler == null) { | |||||
excelEntity.setName(i18nHandler.getLocaleName(excelEntity.getName())); | |||||
excelEntity.setGroupName(i18nHandler.getLocaleName(excelEntity.getGroupName())); | |||||
} | |||||
} | } | ||||
/** | /** | ||||
@@ -107,12 +107,18 @@ public class MergedRegionHelper { | |||||
String[] temp = key.split("_"); | String[] temp = key.split("_"); | ||||
if (Integer.parseInt(temp[0]) >= rowIndex) { | if (Integer.parseInt(temp[0]) >= rowIndex) { | ||||
Integer[] data = mergedCache.get(key); | Integer[] data = mergedCache.get(key); | ||||
mergedCache.put((Integer.parseInt(temp[0]) + size) + "_" + temp[1], mergedCache.get(key)); | |||||
// 还原合并单元格 | |||||
sheet.addMergedRegion(new CellRangeAddress( | |||||
Integer.parseInt(temp[0]) + size - 1, Integer.parseInt(temp[0]) + data[0] + size - 2, | |||||
Integer.parseInt(temp[1]), Integer.parseInt(temp[1]) + data[1] - 1 | |||||
)); | |||||
String newKey = (Integer.parseInt(temp[0]) + size) + "_" + temp[1]; | |||||
if(!mergedCache.containsKey(newKey)){ | |||||
mergedCache.put(newKey, mergedCache.get(key)); | |||||
try { | |||||
// 还原合并单元格 | |||||
sheet.addMergedRegion(new CellRangeAddress( | |||||
Integer.parseInt(temp[0]) + size - 1, Integer.parseInt(temp[0]) + data[0] + size - 2, | |||||
Integer.parseInt(temp[1]), Integer.parseInt(temp[1]) + data[1] - 1 | |||||
)); | |||||
} catch (Exception e) { | |||||
} | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -111,7 +111,8 @@ public class ExcelImportService extends ImportBaseService { | |||||
((IExcelDataModel) entity).setRowNum(row.getRowNum()); | ((IExcelDataModel) entity).setRowNum(row.getRowNum()); | ||||
} | } | ||||
String picId; | String picId; | ||||
boolean isUsed = false;// 是否需要加上这个对象 | |||||
// 是否需要加上这个对象 | |||||
boolean isUsed = false; | |||||
for (int i = row.getFirstCellNum(); i < titlemap.size(); i++) { | for (int i = row.getFirstCellNum(); i < titlemap.size(); i++) { | ||||
Cell cell = row.getCell(i); | Cell cell = row.getCell(i); | ||||
String titleString = (String) titlemap.get(i); | String titleString = (String) titlemap.get(i); | ||||
@@ -164,6 +165,7 @@ public class ExcelImportService extends ImportBaseService { | |||||
Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>(); | Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>(); | ||||
List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>(); | List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>(); | ||||
String targetId = null; | String targetId = null; | ||||
i18nHandler = params.getI18nHandler(); | |||||
if (!Map.class.equals(pojoClass)) { | if (!Map.class.equals(pojoClass)) { | ||||
Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); | Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); | ||||
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ||||
@@ -380,73 +382,81 @@ public class ExcelImportService extends ImportBaseService { | |||||
} | } | ||||
List<T> result = new ArrayList<T>(); | List<T> result = new ArrayList<T>(); | ||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||||
byte[] buffer = new byte[1024]; | |||||
int len; | |||||
while ((len = inputstream.read(buffer)) > -1) { | |||||
baos.write(buffer, 0, len); | |||||
} | |||||
baos.flush(); | |||||
InputStream userIs = new ByteArrayInputStream(baos.toByteArray()); | |||||
if (LOGGER.isDebugEnabled()) { | |||||
LOGGER.debug("Excel clone success"); | |||||
} | |||||
Workbook book = WorkbookFactory.create(userIs); | |||||
boolean isXSSFWorkbook = !(book instanceof HSSFWorkbook); | |||||
if (LOGGER.isDebugEnabled()) { | |||||
LOGGER.debug("Workbook create success"); | |||||
} | |||||
ExcelImportResult importResult = new ExcelImportResult(); | |||||
createErrorCellStyle(book); | |||||
Map<String, PictureData> pictures; | |||||
for (int i = params.getStartSheetIndex(); i < params.getStartSheetIndex() | |||||
+ params.getSheetNum(); i++) { | |||||
if (LOGGER.isDebugEnabled()) { | |||||
LOGGER.debug(" start to read excel by is ,startTime is {}", System.currentTimeMillis()); | |||||
} | |||||
if (isXSSFWorkbook) { | |||||
pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), | |||||
(XSSFWorkbook) book); | |||||
} else { | |||||
pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), | |||||
(HSSFWorkbook) book); | |||||
ExcelImportResult importResult; | |||||
try { | |||||
byte[] buffer = new byte[1024]; | |||||
int len; | |||||
while ((len = inputstream.read(buffer)) > -1) { | |||||
baos.write(buffer, 0, len); | |||||
} | } | ||||
baos.flush(); | |||||
InputStream userIs = new ByteArrayInputStream(baos.toByteArray()); | |||||
if (LOGGER.isDebugEnabled()) { | if (LOGGER.isDebugEnabled()) { | ||||
LOGGER.debug(" end to read excel by is ,endTime is {}", System.currentTimeMillis()); | |||||
LOGGER.debug("Excel clone success"); | |||||
} | } | ||||
result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); | |||||
Workbook book = WorkbookFactory.create(userIs); | |||||
boolean isXSSFWorkbook = !(book instanceof HSSFWorkbook); | |||||
if (LOGGER.isDebugEnabled()) { | if (LOGGER.isDebugEnabled()) { | ||||
LOGGER.debug(" end to read excel list by sheet ,endTime is {}", System.currentTimeMillis()); | |||||
LOGGER.debug("Workbook create success"); | |||||
} | } | ||||
if (params.isReadSingleCell()) { | |||||
readSingleCell(importResult, book.getSheetAt(i), params); | |||||
importResult = new ExcelImportResult(); | |||||
createErrorCellStyle(book); | |||||
Map<String, PictureData> pictures; | |||||
for (int i = params.getStartSheetIndex(); i < params.getStartSheetIndex() | |||||
+ params.getSheetNum(); i++) { | |||||
if (LOGGER.isDebugEnabled()) { | if (LOGGER.isDebugEnabled()) { | ||||
LOGGER.debug(" read Key-Value ,endTime is {}", System.currentTimeMillis()); | |||||
LOGGER.debug(" start to read excel by is ,startTime is {}", System.currentTimeMillis()); | |||||
} | |||||
if (isXSSFWorkbook) { | |||||
pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), | |||||
(XSSFWorkbook) book); | |||||
} else { | |||||
pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), | |||||
(HSSFWorkbook) book); | |||||
} | |||||
if (LOGGER.isDebugEnabled()) { | |||||
LOGGER.debug(" end to read excel by is ,endTime is {}", System.currentTimeMillis()); | |||||
} | |||||
result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); | |||||
if (LOGGER.isDebugEnabled()) { | |||||
LOGGER.debug(" end to read excel list by sheet ,endTime is {}", System.currentTimeMillis()); | |||||
} | |||||
if (params.isReadSingleCell()) { | |||||
readSingleCell(importResult, book.getSheetAt(i), params); | |||||
if (LOGGER.isDebugEnabled()) { | |||||
LOGGER.debug(" read Key-Value ,endTime is {}", System.currentTimeMillis()); | |||||
} | |||||
} | } | ||||
} | } | ||||
if (params.isNeedSave()) { | |||||
saveThisExcel(params, pojoClass, isXSSFWorkbook, book); | |||||
} | |||||
importResult.setList(result); | |||||
if (needMore) { | |||||
InputStream successIs = new ByteArrayInputStream(baos.toByteArray()); | |||||
try { | |||||
Workbook successBook = WorkbookFactory.create(successIs); | |||||
importResult.setWorkbook(removeSuperfluousRows(successBook, failRow, params)); | |||||
importResult.setFailWorkbook(removeSuperfluousRows(book, successRow, params)); | |||||
importResult.setFailList(failCollection); | |||||
importResult.setVerfiyFail(verfiyFail); | |||||
} finally { | |||||
successIs.close(); | |||||
} | |||||
} | |||||
} finally { | |||||
IOUtils.closeQuietly(baos); | |||||
} | } | ||||
if (params.isNeedSave()) { | |||||
saveThisExcel(params, pojoClass, isXSSFWorkbook, book); | |||||
} | |||||
importResult.setList(result); | |||||
if (needMore) { | |||||
InputStream successIs = new ByteArrayInputStream(baos.toByteArray()); | |||||
Workbook successBook = WorkbookFactory.create(successIs); | |||||
importResult.setWorkbook(removeSuperfluousRows(successBook, failRow, params)); | |||||
importResult.setFailWorkbook(removeSuperfluousRows(book, successRow, params)); | |||||
importResult.setFailList(failCollection); | |||||
importResult.setVerfiyFail(verfiyFail); | |||||
successIs.close(); | |||||
} | |||||
baos.close(); | |||||
return importResult; | return importResult; | ||||
} | } | ||||
private Workbook removeSuperfluousRows(Workbook book, List<Row> rowList, ImportParams params) { | private Workbook removeSuperfluousRows(Workbook book, List<Row> rowList, ImportParams params) { | ||||
for (int i = params.getStartSheetIndex(); i < params.getStartSheetIndex() | for (int i = params.getStartSheetIndex(); i < params.getStartSheetIndex() | ||||
+ params.getSheetNum(); i++) { | + params.getSheetNum(); i++) { | ||||
for (int j = 0; j < rowList.size(); j++) { | |||||
for (int j = rowList.size() - 1; j >= 0; j--) { | |||||
if (rowList.get(j).getRowNum() < rowList.get(j).getSheet().getLastRowNum()) { | if (rowList.get(j).getRowNum() < rowList.get(j).getSheet().getLastRowNum()) { | ||||
book.getSheetAt(i).shiftRows(rowList.get(j).getRowNum() + 1, rowList.get(j).getSheet().getLastRowNum(), -1); | book.getSheetAt(i).shiftRows(rowList.get(j).getRowNum() + 1, rowList.get(j).getSheet().getLastRowNum(), -1); | ||||
} else if (rowList.get(j).getRowNum() == rowList.get(j).getSheet().getLastRowNum()) { | } else if (rowList.get(j).getRowNum() == rowList.get(j).getSheet().getLastRowNum()) { | ||||
@@ -29,6 +29,7 @@ import java.util.List; | |||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.Set; | import java.util.Set; | ||||
import cn.afterturn.easypoi.handler.inter.IExcelI18nHandler; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | ||||
import org.apache.poi.ss.usermodel.Workbook; | import org.apache.poi.ss.usermodel.Workbook; | ||||
@@ -52,6 +53,9 @@ import cn.afterturn.easypoi.util.PoiReflectorUtil; | |||||
*/ | */ | ||||
public class ImportBaseService { | public class ImportBaseService { | ||||
protected IExcelI18nHandler i18nHandler; | |||||
/** | /** | ||||
* 把这个注解解析放到类型对象中 | * 把这个注解解析放到类型对象中 | ||||
* | * | ||||
@@ -84,6 +88,9 @@ public class ImportBaseService { | |||||
if(excelEntityAnn != null && excelEntityAnn.show()){ | if(excelEntityAnn != null && excelEntityAnn.show()){ | ||||
excelEntity.setName(excelEntityAnn.name() + "_" + excelEntity.getName()); | excelEntity.setName(excelEntityAnn.name() + "_" + excelEntity.getName()); | ||||
} | } | ||||
if (i18nHandler == null) { | |||||
excelEntity.setName(i18nHandler.getLocaleName(excelEntity.getName())); | |||||
} | |||||
excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(field.getName())); | excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(field.getName())); | ||||
if (StringUtils.isNotEmpty(excel.importFormat())) { | if (StringUtils.isNotEmpty(excel.importFormat())) { | ||||
excelEntity.setFormat(excel.importFormat()); | excelEntity.setFormat(excel.importFormat()); | ||||
@@ -0,0 +1,17 @@ | |||||
package cn.afterturn.easypoi.handler.inter; | |||||
/** | |||||
* @author jueyue on 18-2-2. | |||||
* @version 3.0.4 | |||||
*/ | |||||
public interface IExcelI18nHandler { | |||||
/** | |||||
* 获取当前名称 | |||||
* | |||||
* @param name 注解配置的 | |||||
* @return 返回国际化的名字 | |||||
*/ | |||||
public String getLocaleName(String name); | |||||
} |
@@ -180,6 +180,13 @@ | |||||
<version>1.8.3</version> | <version>1.8.3</version> | ||||
</dependency> | </dependency> | ||||
<!-- 表达式处理 --> | |||||
<dependency> | |||||
<groupId>ognl</groupId> | |||||
<artifactId>ognl</artifactId> | |||||
<version>3.2.6</version> | |||||
</dependency> | |||||
<!-- 模块版本 --> | <!-- 模块版本 --> | ||||
<dependency> | <dependency> | ||||
<groupId>cn.afterturn</groupId> | <groupId>cn.afterturn</groupId> | ||||