Excel导入导出工具优化,支持多Sheet。
This commit is contained in:
@@ -25,14 +25,13 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
|
||||||
import com.jeesite.common.io.FileUtils;
|
import com.jeesite.common.io.FileUtils;
|
||||||
import com.jeesite.common.text.DiffMatchPatch.Diff;
|
import com.jeesite.common.text.DiffMatchPatch.Diff;
|
||||||
import com.jeesite.common.text.DiffMatchPatch.LinesToCharsResult;
|
import com.jeesite.common.text.DiffMatchPatch.LinesToCharsResult;
|
||||||
import com.jeesite.common.text.DiffMatchPatch.Patch;
|
import com.jeesite.common.text.DiffMatchPatch.Patch;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public class DiffMatchPatchTest {
|
public class DiffMatchPatchTest {
|
||||||
|
|
||||||
private DiffMatchPatch dmp;
|
private DiffMatchPatch dmp;
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,441 +1,457 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||||
*/
|
*/
|
||||||
package com.jeesite.common.utils.excel;
|
package com.jeesite.common.utils.excel;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||||
import org.apache.poi.ss.formula.eval.ErrorEval;
|
import org.apache.poi.ss.formula.eval.ErrorEval;
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
import org.apache.poi.ss.usermodel.CellValue;
|
import org.apache.poi.ss.usermodel.CellValue;
|
||||||
import org.apache.poi.ss.usermodel.DateUtil;
|
import org.apache.poi.ss.usermodel.DateUtil;
|
||||||
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||||
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;
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.jeesite.common.callback.MethodCallback;
|
import com.jeesite.common.callback.MethodCallback;
|
||||||
import com.jeesite.common.collect.ListUtils;
|
import com.jeesite.common.collect.ListUtils;
|
||||||
import com.jeesite.common.lang.DateUtils;
|
import com.jeesite.common.lang.DateUtils;
|
||||||
import com.jeesite.common.lang.ObjectUtils;
|
import com.jeesite.common.lang.ObjectUtils;
|
||||||
import com.jeesite.common.lang.StringUtils;
|
import com.jeesite.common.lang.StringUtils;
|
||||||
import com.jeesite.common.reflect.ReflectUtils;
|
import com.jeesite.common.reflect.ReflectUtils;
|
||||||
import com.jeesite.common.utils.excel.annotation.ExcelField;
|
import com.jeesite.common.utils.excel.annotation.ExcelField;
|
||||||
import com.jeesite.common.utils.excel.annotation.ExcelField.Type;
|
import com.jeesite.common.utils.excel.annotation.ExcelField.Type;
|
||||||
import com.jeesite.common.utils.excel.annotation.ExcelFields;
|
import com.jeesite.common.utils.excel.annotation.ExcelFields;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 导入Excel文件(支持“XLS”和“XLSX”格式)
|
* 导入Excel文件(支持“XLS”和“XLSX”格式)
|
||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
* @version 2014-8-19
|
* @version 2014-8-19
|
||||||
*/
|
*/
|
||||||
public class ExcelImport {
|
public class ExcelImport {
|
||||||
|
|
||||||
private static Logger log = LoggerFactory.getLogger(ExcelImport.class);
|
private static Logger log = LoggerFactory.getLogger(ExcelImport.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工作薄对象
|
* 工作薄对象
|
||||||
*/
|
*/
|
||||||
private Workbook wb;
|
private Workbook wb;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工作表对象
|
* 工作表对象
|
||||||
*/
|
*/
|
||||||
private Sheet sheet;
|
private Sheet sheet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 标题行数
|
* 标题行数
|
||||||
*/
|
*/
|
||||||
private int headerNum;
|
private int headerNum;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数
|
* 构造函数
|
||||||
* @param path 导入文件对象,读取第一个工作表
|
* @param path 导入文件对象,读取第一个工作表
|
||||||
* @throws InvalidFormatException
|
* @throws InvalidFormatException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public ExcelImport(File file) throws InvalidFormatException, IOException {
|
public ExcelImport(File file) throws InvalidFormatException, IOException {
|
||||||
this(file, 0, 0);
|
this(file, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数
|
* 构造函数
|
||||||
* @param path 导入文件对象,读取第一个工作表
|
* @param path 导入文件对象,读取第一个工作表
|
||||||
* @param headerNum 标题行数,数据行号=标题行数+1
|
* @param headerNum 标题行数,数据行号=标题行数+1
|
||||||
* @throws InvalidFormatException
|
* @throws InvalidFormatException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public ExcelImport(File file, int headerNum)
|
public ExcelImport(File file, int headerNum)
|
||||||
throws InvalidFormatException, IOException {
|
throws InvalidFormatException, IOException {
|
||||||
this(file, headerNum, 0);
|
this(file, headerNum, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数
|
* 构造函数
|
||||||
* @param path 导入文件对象
|
* @param path 导入文件对象
|
||||||
* @param headerNum 标题行数,数据行号=标题行数+1
|
* @param headerNum 标题行数,数据行号=标题行数+1
|
||||||
* @param sheetIndexOrName 工作表编号或名称,从0开始
|
* @param sheetIndexOrName 工作表编号或名称,从0开始
|
||||||
* @throws InvalidFormatException
|
* @throws InvalidFormatException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public ExcelImport(File file, int headerNum, Object sheetIndexOrName)
|
public ExcelImport(File file, int headerNum, Object sheetIndexOrName)
|
||||||
throws InvalidFormatException, IOException {
|
throws InvalidFormatException, IOException {
|
||||||
this(file.getName(), new FileInputStream(file), headerNum, sheetIndexOrName);
|
this(file.getName(), new FileInputStream(file), headerNum, sheetIndexOrName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数
|
* 构造函数
|
||||||
* @param file 导入文件对象
|
* @param file 导入文件对象
|
||||||
* @param headerNum 标题行数,数据行号=标题行数+1
|
* @param headerNum 标题行数,数据行号=标题行数+1
|
||||||
* @param sheetIndexOrName 工作表编号或名称,从0开始
|
* @param sheetIndexOrName 工作表编号或名称,从0开始
|
||||||
* @throws InvalidFormatException
|
* @throws InvalidFormatException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public ExcelImport(MultipartFile multipartFile, int headerNum, Object sheetIndexOrName)
|
public ExcelImport(MultipartFile multipartFile, int headerNum, Object sheetIndexOrName)
|
||||||
throws InvalidFormatException, IOException {
|
throws InvalidFormatException, IOException {
|
||||||
this(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndexOrName);
|
this(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndexOrName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数
|
* 构造函数
|
||||||
* @param path 导入文件对象
|
* @param path 导入文件对象
|
||||||
* @param headerNum 标题行数,数据行号=标题行数+1
|
* @param headerNum 标题行数,数据行号=标题行数+1
|
||||||
* @param sheetIndexOrName 工作表编号或名称
|
* @param sheetIndexOrName 工作表编号或名称
|
||||||
* @throws InvalidFormatException
|
* @throws InvalidFormatException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public ExcelImport(String fileName, InputStream is, int headerNum, Object sheetIndexOrName)
|
public ExcelImport(String fileName, InputStream is, int headerNum, Object sheetIndexOrName)
|
||||||
throws InvalidFormatException, IOException {
|
throws InvalidFormatException, IOException {
|
||||||
if (StringUtils.isBlank(fileName)){
|
if (StringUtils.isBlank(fileName)){
|
||||||
throw new ExcelException("导入文档为空!");
|
throw new ExcelException("导入文档为空!");
|
||||||
}else if(fileName.toLowerCase().endsWith("xls")){
|
}else if(fileName.toLowerCase().endsWith("xls")){
|
||||||
this.wb = new HSSFWorkbook(is);
|
this.wb = new HSSFWorkbook(is);
|
||||||
}else if(fileName.toLowerCase().endsWith("xlsx")){
|
}else if(fileName.toLowerCase().endsWith("xlsx")){
|
||||||
this.wb = new XSSFWorkbook(is);
|
this.wb = new XSSFWorkbook(is);
|
||||||
}else{
|
}else{
|
||||||
throw new ExcelException("文档格式不正确!");
|
throw new ExcelException("文档格式不正确!");
|
||||||
}
|
}
|
||||||
if (sheetIndexOrName instanceof Integer || sheetIndexOrName instanceof Long){
|
this.setSheet(sheetIndexOrName, headerNum);
|
||||||
this.sheet = this.wb.getSheetAt(ObjectUtils.toInteger(sheetIndexOrName));
|
log.debug("Initialize success.");
|
||||||
}else{
|
}
|
||||||
this.sheet = this.wb.getSheet(ObjectUtils.toString(sheetIndexOrName));
|
|
||||||
}
|
/**
|
||||||
if (this.sheet == null){
|
* 添加到 annotationList
|
||||||
throw new ExcelException("没有找到工作表!");
|
*/
|
||||||
}
|
private void addAnnotation(List<Object[]> annotationList, ExcelField ef, Object fOrM, Type type, String... groups){
|
||||||
this.headerNum = headerNum;
|
// if (ef != null && (ef.type()==0 || ef.type()==type)){
|
||||||
log.debug("Initialize success.");
|
if (ef != null && (ef.type() == Type.ALL || ef.type() == type)){
|
||||||
}
|
if (groups!=null && groups.length>0){
|
||||||
|
boolean inGroup = false;
|
||||||
/**
|
for (String g : groups){
|
||||||
* 添加到 annotationList
|
if (inGroup){
|
||||||
*/
|
break;
|
||||||
private void addAnnotation(List<Object[]> annotationList, ExcelField ef, Object fOrM, Type type, String... groups){
|
}
|
||||||
// if (ef != null && (ef.type()==0 || ef.type()==type)){
|
for (String efg : ef.groups()){
|
||||||
if (ef != null && (ef.type() == Type.ALL || ef.type() == type)){
|
if (StringUtils.equals(g, efg)){
|
||||||
if (groups!=null && groups.length>0){
|
inGroup = true;
|
||||||
boolean inGroup = false;
|
annotationList.add(new Object[]{ef, fOrM});
|
||||||
for (String g : groups){
|
break;
|
||||||
if (inGroup){
|
}
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
for (String efg : ef.groups()){
|
}else{
|
||||||
if (StringUtils.equals(g, efg)){
|
annotationList.add(new Object[]{ef, fOrM});
|
||||||
inGroup = true;
|
}
|
||||||
annotationList.add(new Object[]{ef, fOrM});
|
}
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
}
|
* 获取当前工作薄
|
||||||
}else{
|
* @author ThinkGem
|
||||||
annotationList.add(new Object[]{ef, fOrM});
|
*/
|
||||||
}
|
public Workbook getWorkbook() {
|
||||||
}
|
return wb;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取行对象
|
* 设置当前工作表和标题行数
|
||||||
* @param rownum
|
* @author ThinkGem
|
||||||
* @return
|
*/
|
||||||
*/
|
public void setSheet(Object sheetIndexOrName, int headerNum) {
|
||||||
public Row getRow(int rownum){
|
if (sheetIndexOrName instanceof Integer || sheetIndexOrName instanceof Long){
|
||||||
return this.sheet.getRow(rownum);
|
this.sheet = this.wb.getSheetAt(ObjectUtils.toInteger(sheetIndexOrName));
|
||||||
}
|
}else{
|
||||||
|
this.sheet = this.wb.getSheet(ObjectUtils.toString(sheetIndexOrName));
|
||||||
/**
|
}
|
||||||
* 获取数据行号
|
if (this.sheet == null){
|
||||||
* @return
|
throw new ExcelException("没有找到‘"+sheetIndexOrName+"’工作表!");
|
||||||
*/
|
}
|
||||||
public int getDataRowNum(){
|
this.headerNum = headerNum;
|
||||||
return headerNum;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* 获取行对象
|
||||||
* 获取最后一个数据行号
|
* @param rownum
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public int getLastDataRowNum(){
|
public Row getRow(int rownum){
|
||||||
//return this.sheet.getLastRowNum()+headerNum;
|
return this.sheet.getRow(rownum);
|
||||||
return this.sheet.getLastRowNum() + 1;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* 获取数据行号
|
||||||
* 获取最后一个列号
|
* @return
|
||||||
* @return
|
*/
|
||||||
*/
|
public int getDataRowNum(){
|
||||||
public int getLastCellNum(){
|
return headerNum;
|
||||||
Row row = this.getRow(headerNum);
|
}
|
||||||
return row == null ? 0 : row.getLastCellNum();
|
|
||||||
}
|
/**
|
||||||
|
* 获取最后一个数据行号
|
||||||
/**
|
* @return
|
||||||
* 获取单元格值
|
*/
|
||||||
* @param row 获取的行
|
public int getLastDataRowNum(){
|
||||||
* @param column 获取单元格列号
|
//return this.sheet.getLastRowNum() + headerNum;
|
||||||
* @return 单元格值
|
return this.sheet.getLastRowNum() + 1;
|
||||||
*/
|
}
|
||||||
public Object getCellValue(Row row, int column){
|
|
||||||
Object val = "";
|
/**
|
||||||
try{
|
* 获取最后一个列号
|
||||||
Cell cell = row.getCell(column);
|
* @return
|
||||||
if (cell != null){
|
*/
|
||||||
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
|
public int getLastCellNum(){
|
||||||
val = new DecimalFormat("0").format(cell.getNumericCellValue());
|
Row row = this.getRow(headerNum);
|
||||||
}else if (cell.getCellType() == Cell.CELL_TYPE_STRING){
|
return row == null ? 0 : row.getLastCellNum();
|
||||||
val = cell.getStringCellValue();
|
}
|
||||||
}else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
|
|
||||||
try {
|
/**
|
||||||
val = cell.getStringCellValue();
|
* 获取单元格值
|
||||||
} catch (Exception e) {
|
* @param row 获取的行
|
||||||
FormulaEvaluator evaluator = cell.getSheet().getWorkbook()
|
* @param column 获取单元格列号
|
||||||
.getCreationHelper().createFormulaEvaluator();
|
* @return 单元格值
|
||||||
evaluator.evaluateFormulaCell(cell);
|
*/
|
||||||
CellValue cellValue = evaluator.evaluate(cell);
|
public Object getCellValue(Row row, int column){
|
||||||
switch (cellValue.getCellType()) {
|
Object val = "";
|
||||||
case Cell.CELL_TYPE_NUMERIC:
|
try{
|
||||||
val = cellValue.getNumberValue();
|
Cell cell = row.getCell(column);
|
||||||
break;
|
if (cell != null){
|
||||||
case Cell.CELL_TYPE_STRING:
|
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
|
||||||
val = cellValue.getStringValue();
|
val = new DecimalFormat("0").format(cell.getNumericCellValue());
|
||||||
break;
|
}else if (cell.getCellType() == Cell.CELL_TYPE_STRING){
|
||||||
case Cell.CELL_TYPE_BOOLEAN:
|
val = cell.getStringCellValue();
|
||||||
val = cellValue.getBooleanValue();
|
}else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
|
||||||
break;
|
try {
|
||||||
case Cell.CELL_TYPE_ERROR:
|
val = cell.getStringCellValue();
|
||||||
val = ErrorEval.getText(cellValue.getErrorValue());
|
} catch (Exception e) {
|
||||||
break;
|
FormulaEvaluator evaluator = cell.getSheet().getWorkbook()
|
||||||
default:
|
.getCreationHelper().createFormulaEvaluator();
|
||||||
val = cell.getCellFormula();
|
evaluator.evaluateFormulaCell(cell);
|
||||||
}
|
CellValue cellValue = evaluator.evaluate(cell);
|
||||||
}
|
switch (cellValue.getCellType()) {
|
||||||
}else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
|
case Cell.CELL_TYPE_NUMERIC:
|
||||||
val = cell.getBooleanCellValue();
|
val = cellValue.getNumberValue();
|
||||||
}else if (cell.getCellType() == Cell.CELL_TYPE_ERROR){
|
break;
|
||||||
val = cell.getErrorCellValue();
|
case Cell.CELL_TYPE_STRING:
|
||||||
}
|
val = cellValue.getStringValue();
|
||||||
}
|
break;
|
||||||
}catch (Exception e) {
|
case Cell.CELL_TYPE_BOOLEAN:
|
||||||
return val;
|
val = cellValue.getBooleanValue();
|
||||||
}
|
break;
|
||||||
return val;
|
case Cell.CELL_TYPE_ERROR:
|
||||||
}
|
val = ErrorEval.getText(cellValue.getErrorValue());
|
||||||
|
break;
|
||||||
/**
|
default:
|
||||||
* 获取导入数据列表
|
val = cell.getCellFormula();
|
||||||
* @param cls 导入对象类型
|
}
|
||||||
* @param groups 导入分组
|
}
|
||||||
*/
|
}else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
|
||||||
public <E> List<E> getDataList(Class<E> cls, String... groups) throws InstantiationException, IllegalAccessException{
|
val = cell.getBooleanCellValue();
|
||||||
return getDataList(cls, false, groups);
|
}else if (cell.getCellType() == Cell.CELL_TYPE_ERROR){
|
||||||
}
|
val = cell.getErrorCellValue();
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* 获取导入数据列表
|
}catch (Exception e) {
|
||||||
* @param cls 导入对象类型
|
return val;
|
||||||
* @param isThrowException 遇见错误是否抛出异常
|
}
|
||||||
* @param groups 导入分组
|
return val;
|
||||||
*/
|
}
|
||||||
public <E> List<E> getDataList(Class<E> cls, final boolean isThrowException, String... groups) throws InstantiationException, IllegalAccessException{
|
|
||||||
return getDataList(cls, new MethodCallback() {
|
/**
|
||||||
@Override
|
* 获取导入数据列表
|
||||||
public Object execute(Object... params) {
|
* @param cls 导入对象类型
|
||||||
if (isThrowException){
|
* @param groups 导入分组
|
||||||
Exception ex = (Exception)params[0];
|
*/
|
||||||
int rowNum = (int)params[1];
|
public <E> List<E> getDataList(Class<E> cls, String... groups) throws InstantiationException, IllegalAccessException{
|
||||||
int columnNum = (int)params[2];
|
return getDataList(cls, false, groups);
|
||||||
throw new ExcelException("Get cell value ["+rowNum+","+columnNum+"]", ex);
|
}
|
||||||
}
|
|
||||||
return null;
|
/**
|
||||||
}
|
* 获取导入数据列表
|
||||||
}, groups);
|
* @param cls 导入对象类型
|
||||||
}
|
* @param isThrowException 遇见错误是否抛出异常
|
||||||
/**
|
* @param groups 导入分组
|
||||||
* 获取导入数据列表
|
*/
|
||||||
* @param cls 导入对象类型
|
public <E> List<E> getDataList(Class<E> cls, final boolean isThrowException, String... groups) throws InstantiationException, IllegalAccessException{
|
||||||
* @param isThrowException 遇见错误是否抛出异常
|
return getDataList(cls, new MethodCallback() {
|
||||||
* @param groups 导入分组
|
@Override
|
||||||
*/
|
public Object execute(Object... params) {
|
||||||
public <E> List<E> getDataList(Class<E> cls, MethodCallback exceptionCallback, String... groups) throws InstantiationException, IllegalAccessException{
|
if (isThrowException){
|
||||||
List<Object[]> annotationList = ListUtils.newArrayList();
|
Exception ex = (Exception)params[0];
|
||||||
// Get annotation field
|
int rowNum = (int)params[1];
|
||||||
Field[] fs = cls.getDeclaredFields();
|
int columnNum = (int)params[2];
|
||||||
for (Field f : fs){
|
throw new ExcelException("Get cell value ["+rowNum+","+columnNum+"]", ex);
|
||||||
ExcelFields efs = f.getAnnotation(ExcelFields.class);
|
}
|
||||||
if (efs != null && efs.value() != null){
|
return null;
|
||||||
for (ExcelField ef : efs.value()){
|
}
|
||||||
addAnnotation(annotationList, ef, f, Type.IMPORT, groups);
|
}, groups);
|
||||||
}
|
}
|
||||||
}
|
/**
|
||||||
ExcelField ef = f.getAnnotation(ExcelField.class);
|
* 获取导入数据列表
|
||||||
addAnnotation(annotationList, ef, f, Type.IMPORT, groups);
|
* @param cls 导入对象类型
|
||||||
}
|
* @param isThrowException 遇见错误是否抛出异常
|
||||||
// Get annotation method
|
* @param groups 导入分组
|
||||||
Method[] ms = cls.getDeclaredMethods();
|
*/
|
||||||
for (Method m : ms){
|
public <E> List<E> getDataList(Class<E> cls, MethodCallback exceptionCallback, String... groups) throws InstantiationException, IllegalAccessException{
|
||||||
ExcelFields efs = m.getAnnotation(ExcelFields.class);
|
List<Object[]> annotationList = ListUtils.newArrayList();
|
||||||
if (efs != null && efs.value() != null){
|
// Get annotation field
|
||||||
for (ExcelField ef : efs.value()){
|
Field[] fs = cls.getDeclaredFields();
|
||||||
addAnnotation(annotationList, ef, m, Type.IMPORT, groups);
|
for (Field f : fs){
|
||||||
}
|
ExcelFields efs = f.getAnnotation(ExcelFields.class);
|
||||||
}
|
if (efs != null && efs.value() != null){
|
||||||
ExcelField ef = m.getAnnotation(ExcelField.class);
|
for (ExcelField ef : efs.value()){
|
||||||
addAnnotation(annotationList, ef, m, Type.IMPORT, groups);
|
addAnnotation(annotationList, ef, f, Type.IMPORT, groups);
|
||||||
}
|
}
|
||||||
// Field sorting
|
}
|
||||||
Collections.sort(annotationList, new Comparator<Object[]>() {
|
ExcelField ef = f.getAnnotation(ExcelField.class);
|
||||||
@Override
|
addAnnotation(annotationList, ef, f, Type.IMPORT, groups);
|
||||||
public int compare(Object[] o1, Object[] o2) {
|
}
|
||||||
return new Integer(((ExcelField)o1[0]).sort()).compareTo(
|
// Get annotation method
|
||||||
new Integer(((ExcelField)o2[0]).sort()));
|
Method[] ms = cls.getDeclaredMethods();
|
||||||
};
|
for (Method m : ms){
|
||||||
});
|
ExcelFields efs = m.getAnnotation(ExcelFields.class);
|
||||||
//log.debug("Import column count:"+annotationList.size());
|
if (efs != null && efs.value() != null){
|
||||||
// Get excel data
|
for (ExcelField ef : efs.value()){
|
||||||
List<E> dataList = ListUtils.newArrayList();
|
addAnnotation(annotationList, ef, m, Type.IMPORT, groups);
|
||||||
for (int i = this.getDataRowNum(); i < this.getLastDataRowNum(); i++) {
|
}
|
||||||
E e = (E)cls.newInstance();
|
}
|
||||||
Row row = this.getRow(i);
|
ExcelField ef = m.getAnnotation(ExcelField.class);
|
||||||
StringBuilder sb = new StringBuilder();
|
addAnnotation(annotationList, ef, m, Type.IMPORT, groups);
|
||||||
for (int j = 0; j < annotationList.size(); j++){//Object[] os : annotationList){
|
}
|
||||||
Object[] os = annotationList.get(j);
|
// Field sorting
|
||||||
ExcelField ef = (ExcelField)os[0];
|
Collections.sort(annotationList, new Comparator<Object[]>() {
|
||||||
int column = (ef.column() != -1) ? ef.column() : j;
|
@Override
|
||||||
Object val = this.getCellValue(row, column);
|
public int compare(Object[] o1, Object[] o2) {
|
||||||
if (val != null){
|
return new Integer(((ExcelField)o1[0]).sort()).compareTo(
|
||||||
// If is dict type, get dict value
|
new Integer(((ExcelField)o2[0]).sort()));
|
||||||
if (StringUtils.isNotBlank(ef.dictType())){
|
};
|
||||||
try{
|
});
|
||||||
Class<?> dictUtils = Class.forName("com.jeesite.modules.sys.utils.DictUtils");
|
//log.debug("Import column count:"+annotationList.size());
|
||||||
val = dictUtils.getMethod("getDictValue", String.class, String.class,
|
// Get excel data
|
||||||
String.class).invoke(null, val.toString(), ef.dictType(), "");
|
List<E> dataList = ListUtils.newArrayList();
|
||||||
} catch (Exception ex) {
|
for (int i = this.getDataRowNum(); i < this.getLastDataRowNum(); i++) {
|
||||||
log.info("Get cell value ["+i+","+column+"] error: " + ex.toString());
|
E e = (E)cls.newInstance();
|
||||||
val = null;
|
Row row = this.getRow(i);
|
||||||
}
|
StringBuilder sb = new StringBuilder();
|
||||||
//val = DictUtils.getDictValue(val.toString(), ef.dictType(), "");
|
for (int j = 0; j < annotationList.size(); j++){//Object[] os : annotationList){
|
||||||
//log.debug("Dictionary type value: ["+i+","+colunm+"] " + val);
|
Object[] os = annotationList.get(j);
|
||||||
}
|
ExcelField ef = (ExcelField)os[0];
|
||||||
// Get param type and type cast
|
int column = (ef.column() != -1) ? ef.column() : j;
|
||||||
Class<?> valType = Class.class;
|
Object val = this.getCellValue(row, column);
|
||||||
if (os[1] instanceof Field){
|
if (val != null){
|
||||||
valType = ((Field)os[1]).getType();
|
// If is dict type, get dict value
|
||||||
}else if (os[1] instanceof Method){
|
if (StringUtils.isNotBlank(ef.dictType())){
|
||||||
Method method = ((Method)os[1]);
|
try{
|
||||||
if ("get".equals(method.getName().substring(0, 3))){
|
Class<?> dictUtils = Class.forName("com.jeesite.modules.sys.utils.DictUtils");
|
||||||
valType = method.getReturnType();
|
val = dictUtils.getMethod("getDictValue", String.class, String.class,
|
||||||
}else if("set".equals(method.getName().substring(0, 3))){
|
String.class).invoke(null, val.toString(), ef.dictType(), "");
|
||||||
valType = ((Method)os[1]).getParameterTypes()[0];
|
} catch (Exception ex) {
|
||||||
}
|
log.info("Get cell value ["+i+","+column+"] error: " + ex.toString());
|
||||||
}
|
val = null;
|
||||||
//log.debug("Import value type: ["+i+","+column+"] " + valType);
|
}
|
||||||
try {
|
//val = DictUtils.getDictValue(val.toString(), ef.dictType(), "");
|
||||||
if (valType == String.class){
|
//log.debug("Dictionary type value: ["+i+","+colunm+"] " + val);
|
||||||
String s = String.valueOf(val.toString());
|
}
|
||||||
if(StringUtils.endsWith(s, ".0")){
|
// Get param type and type cast
|
||||||
val = StringUtils.substringBefore(s, ".0");
|
Class<?> valType = Class.class;
|
||||||
}else{
|
if (os[1] instanceof Field){
|
||||||
val = String.valueOf(val.toString());
|
valType = ((Field)os[1]).getType();
|
||||||
}
|
}else if (os[1] instanceof Method){
|
||||||
}else if (valType == Integer.class){
|
Method method = ((Method)os[1]);
|
||||||
val = Double.valueOf(val.toString()).intValue();
|
if ("get".equals(method.getName().substring(0, 3))){
|
||||||
}else if (valType == Long.class){
|
valType = method.getReturnType();
|
||||||
val = Double.valueOf(val.toString()).longValue();
|
}else if("set".equals(method.getName().substring(0, 3))){
|
||||||
}else if (valType == Double.class){
|
valType = ((Method)os[1]).getParameterTypes()[0];
|
||||||
val = Double.valueOf(val.toString());
|
}
|
||||||
}else if (valType == Float.class){
|
}
|
||||||
val = Float.valueOf(val.toString());
|
//log.debug("Import value type: ["+i+","+column+"] " + valType);
|
||||||
}else if (valType == Date.class){
|
try {
|
||||||
if (val instanceof String){
|
if (valType == String.class){
|
||||||
val = DateUtils.parseDate(val);
|
String s = String.valueOf(val.toString());
|
||||||
}else{
|
if(StringUtils.endsWith(s, ".0")){
|
||||||
// POI Excel 日期格式转换
|
val = StringUtils.substringBefore(s, ".0");
|
||||||
val = DateUtil.getJavaDate((Double)val);
|
}else{
|
||||||
}
|
val = String.valueOf(val.toString());
|
||||||
}else{
|
}
|
||||||
if (ef.fieldType() != Class.class){
|
}else if (valType == Integer.class){
|
||||||
val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
|
val = Double.valueOf(val.toString()).intValue();
|
||||||
}else{
|
}else if (valType == Long.class){
|
||||||
val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
|
val = Double.valueOf(val.toString()).longValue();
|
||||||
"fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
|
}else if (valType == Double.class){
|
||||||
}
|
val = Double.valueOf(val.toString());
|
||||||
}
|
}else if (valType == Float.class){
|
||||||
} catch (Exception ex) {
|
val = Float.valueOf(val.toString());
|
||||||
log.info("Get cell value ["+i+","+column+"] error: " + ex.toString());
|
}else if (valType == Date.class){
|
||||||
val = null;
|
if (val instanceof String){
|
||||||
// 参数:Exception ex, int rowNum, int columnNum
|
val = DateUtils.parseDate(val);
|
||||||
exceptionCallback.execute(ex, i, column);
|
}else{
|
||||||
}
|
// POI Excel 日期格式转换
|
||||||
// set entity value
|
val = DateUtil.getJavaDate((Double)val);
|
||||||
if (os[1] instanceof Field){
|
}
|
||||||
ReflectUtils.invokeSetter(e, ((Field)os[1]).getName(), val);
|
}else{
|
||||||
}else if (os[1] instanceof Method){
|
if (ef.fieldType() != Class.class){
|
||||||
String mthodName = ((Method)os[1]).getName();
|
val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
|
||||||
if ("get".equals(mthodName.substring(0, 3))){
|
}else{
|
||||||
mthodName = "set"+StringUtils.substringAfter(mthodName, "get");
|
val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
|
||||||
}
|
"fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
|
||||||
ReflectUtils.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val});
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception ex) {
|
||||||
sb.append(val+", ");
|
log.info("Get cell value ["+i+","+column+"] error: " + ex.toString());
|
||||||
}
|
val = null;
|
||||||
dataList.add(e);
|
// 参数:Exception ex, int rowNum, int columnNum
|
||||||
log.debug("Read success: ["+i+"] "+sb.toString());
|
exceptionCallback.execute(ex, i, column);
|
||||||
}
|
}
|
||||||
return dataList;
|
// set entity value
|
||||||
}
|
if (os[1] instanceof Field){
|
||||||
|
ReflectUtils.invokeSetter(e, ((Field)os[1]).getName(), val);
|
||||||
// /**
|
}else if (os[1] instanceof Method){
|
||||||
// * 导入测试
|
String mthodName = ((Method)os[1]).getName();
|
||||||
// */
|
if ("get".equals(mthodName.substring(0, 3))){
|
||||||
// public static void main(String[] args) throws Throwable {
|
mthodName = "set"+StringUtils.substringAfter(mthodName, "get");
|
||||||
//
|
}
|
||||||
// ImportExcel ei = new ImportExcel("target/export.xlsx", 1);
|
ReflectUtils.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val});
|
||||||
//
|
}
|
||||||
// for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) {
|
}
|
||||||
// Row row = ei.getRow(i);
|
sb.append(val+", ");
|
||||||
// for (int j = 0; j < ei.getLastCellNum(); j++) {
|
}
|
||||||
// Object val = ei.getCellValue(row, j);
|
dataList.add(e);
|
||||||
// System.out.print(val+", ");
|
log.debug("Read success: ["+i+"] "+sb.toString());
|
||||||
// }
|
}
|
||||||
// System.out.print("\n");
|
return dataList;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// }
|
// /**
|
||||||
|
// * 导入测试
|
||||||
}
|
// */
|
||||||
|
// public static void main(String[] args) throws Throwable {
|
||||||
|
//
|
||||||
|
// ImportExcel ei = new ImportExcel("target/export.xlsx", 1);
|
||||||
|
//
|
||||||
|
// for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) {
|
||||||
|
// Row row = ei.getRow(i);
|
||||||
|
// for (int j = 0; j < ei.getLastCellNum(); j++) {
|
||||||
|
// Object val = ei.getCellValue(row, j);
|
||||||
|
// System.out.print(val+", ");
|
||||||
|
// }
|
||||||
|
// System.out.print("\n");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -1,27 +1,26 @@
|
|||||||
<% var productName = @Global.getConfig('productName'); %>
|
<% var productName = @Global.getConfig('productName'); %>
|
||||||
<nav class="navbar navbar-static-top">
|
<nav class="navbar navbar-static-top">
|
||||||
<div class="logo" data-toggle="push-menu" title="${productName}">
|
<div class="logo" data-toggle="push-menu" title="${productName}">
|
||||||
<b>${productName}</b>
|
<b>${productName}</b>
|
||||||
<small> <i class="fa fa-bars"></i></small>
|
<small> <i class="fa fa-bars"></i></small>
|
||||||
</div>
|
</div>
|
||||||
<%/*<!--%><a href="javascript:" class="sidebar-toggle" data-toggle="push-menu" role="button">
|
<%/*<!--%><a href="javascript:" class="sidebar-toggle" data-toggle="push-menu" role="button">
|
||||||
<span class="sr-only">菜单切换</span><span class="icon-bar"></span>
|
<span class="sr-only">菜单切换</span><span class="icon-bar"></span>
|
||||||
<span class="icon-bar"></span><span class="icon-bar"></span>
|
<span class="icon-bar"></span><span class="icon-bar"></span>
|
||||||
</a><%-->*/%>
|
</a><%-->*/%>
|
||||||
<div class="navbar-custom-menu">
|
<div class="navbar-custom-menu">
|
||||||
<ul class="nav navbar-nav">
|
<ul class="nav navbar-nav">
|
||||||
<% if(__info_type == '0'){ %>
|
<% if(__info_type == '0'){ %>
|
||||||
<li><a href="javascript:" data-href="http://jeesite.com" class="addTabPage"><i class="fa fa-diamond"></i> 官方网站</a></li>
|
<li><a href="javascript:" data-href="http://jeesite.com" class="addTabPage"><i class="fa fa-diamond"></i> 官方网站</a></li>
|
||||||
<li><a href="javascript:" data-href="http://blog.jeesite.com" class="addTabPage"><i class="fa fa-codepen"></i> 作者博客</a></li>
|
<% } %>
|
||||||
<% } %>
|
<li><a href="javascript:" id="fullScreen" title="全屏"><i class="fa fa-arrows-alt"></i></a></li>
|
||||||
<li><a href="javascript:" id="fullScreen" title="全屏"><i class="fa fa-arrows-alt"></i></a></li>
|
<li><a href="javascript:" id="switchSkin" title="切换主题" style="margin-top:-1px;"><i class="fa fa-dashboard"></i></a></li>
|
||||||
<li><a href="javascript:" id="switchSkin" title="切换主题" style="margin-top:-1px;"><i class="fa fa-dashboard"></i></a></li>
|
<% include('/modules/sys/sysIndex/topMenuLang.html'){} %>
|
||||||
<% include('/modules/sys/sysIndex/topMenuLang.html'){} %>
|
<% include('/modules/sys/sysIndex/topMenuOnline.html'){} %>
|
||||||
<% include('/modules/sys/sysIndex/topMenuOnline.html'){} %>
|
<% include('/modules/sys/sysIndex/topMenuMsg.html'){} %>
|
||||||
<% include('/modules/sys/sysIndex/topMenuMsg.html'){} %>
|
<% include('/modules/sys/sysIndex/topMenuNotify.html'){} %>
|
||||||
<% include('/modules/sys/sysIndex/topMenuNotify.html'){} %>
|
<% include('/modules/sys/sysIndex/topMenuTask.html'){} %>
|
||||||
<% include('/modules/sys/sysIndex/topMenuTask.html'){} %>
|
<% include('/modules/sys/sysIndex/topMenuUser.html'){} %>
|
||||||
<% include('/modules/sys/sysIndex/topMenuUser.html'){} %>
|
</ul>
|
||||||
</ul>
|
</div>
|
||||||
</div>
|
|
||||||
</nav>
|
</nav>
|
||||||
@@ -24,14 +24,14 @@ public class InitCoreData extends com.jeesite.modules.db.InitCoreData {
|
|||||||
initConfig();
|
initConfig();
|
||||||
initModule();
|
initModule();
|
||||||
initDict();
|
initDict();
|
||||||
// initRole();
|
initRole();
|
||||||
// initMenu();
|
initMenu();
|
||||||
// initUser();
|
initUser();
|
||||||
// initArea();
|
// initArea();
|
||||||
// initOffice();
|
initOffice();
|
||||||
// initCompany();
|
initCompany();
|
||||||
// initPost();
|
initPost();
|
||||||
// initEmpUser();
|
initEmpUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user