|
@@ -45,44 +45,44 @@ import com.jeeplus.modules.sys.utils.UserUtils;
|
|
|
* @version 2016-03-10
|
|
|
*/
|
|
|
public class ImportExcel {
|
|
|
-
|
|
|
+
|
|
|
private static Logger log = LoggerFactory.getLogger(ImportExcel.class);
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 工作薄对象
|
|
|
*/
|
|
|
private Workbook wb;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 工作表对象
|
|
|
*/
|
|
|
private Sheet sheet;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 标题行号
|
|
|
*/
|
|
|
private int headerNum;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 构造函数
|
|
|
* @param path 导入文件,读取第一个工作表
|
|
|
* @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
- * @throws InvalidFormatException
|
|
|
- * @throws IOException
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public ImportExcel(String fileName, int headerNum)
|
|
|
+ public ImportExcel(String fileName, int headerNum)
|
|
|
throws InvalidFormatException, IOException {
|
|
|
this(new File(fileName), headerNum);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 构造函数
|
|
|
* @param path 导入文件对象,读取第一个工作表
|
|
|
* @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
- * @throws InvalidFormatException
|
|
|
- * @throws IOException
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public ImportExcel(File file, int headerNum)
|
|
|
+ public ImportExcel(File file, int headerNum)
|
|
|
throws InvalidFormatException, IOException {
|
|
|
this(file, headerNum, 0);
|
|
|
}
|
|
@@ -92,36 +92,36 @@ public class ImportExcel {
|
|
|
* @param path 导入文件
|
|
|
* @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
* @param sheetIndex 工作表编号
|
|
|
- * @throws InvalidFormatException
|
|
|
- * @throws IOException
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public ImportExcel(String fileName, int headerNum, int sheetIndex)
|
|
|
+ public ImportExcel(String fileName, int headerNum, int sheetIndex)
|
|
|
throws InvalidFormatException, IOException {
|
|
|
this(new File(fileName), headerNum, sheetIndex);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 构造函数
|
|
|
* @param path 导入文件对象
|
|
|
* @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
* @param sheetIndex 工作表编号
|
|
|
- * @throws InvalidFormatException
|
|
|
- * @throws IOException
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public ImportExcel(File file, int headerNum, int sheetIndex)
|
|
|
+ public ImportExcel(File file, int headerNum, int sheetIndex)
|
|
|
throws InvalidFormatException, IOException {
|
|
|
this(file.getName(), new FileInputStream(file), headerNum, sheetIndex);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 构造函数
|
|
|
* @param file 导入文件对象
|
|
|
* @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
* @param sheetIndex 工作表编号
|
|
|
- * @throws InvalidFormatException
|
|
|
- * @throws IOException
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public ImportExcel(MultipartFile multipartFile, int headerNum, int sheetIndex)
|
|
|
+ public ImportExcel(MultipartFile multipartFile, int headerNum, int sheetIndex)
|
|
|
throws InvalidFormatException, IOException {
|
|
|
this(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndex);
|
|
|
}
|
|
@@ -131,20 +131,20 @@ public class ImportExcel {
|
|
|
* @param path 导入文件对象
|
|
|
* @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
* @param sheetIndex 工作表编号
|
|
|
- * @throws InvalidFormatException
|
|
|
- * @throws IOException
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- public ImportExcel(String fileName, InputStream is, int headerNum, int sheetIndex)
|
|
|
+ public ImportExcel(String fileName, InputStream is, int headerNum, int sheetIndex)
|
|
|
throws InvalidFormatException, IOException {
|
|
|
if (StringUtils.isBlank(fileName)){
|
|
|
throw new RuntimeException("导入文档为空!");
|
|
|
- }else if(fileName.toLowerCase().endsWith("xls")){
|
|
|
- this.wb = new HSSFWorkbook(is);
|
|
|
- }else if(fileName.toLowerCase().endsWith("xlsx")){
|
|
|
+ }else if(fileName.toLowerCase().endsWith("xls")){
|
|
|
+ this.wb = new HSSFWorkbook(is);
|
|
|
+ }else if(fileName.toLowerCase().endsWith("xlsx")){
|
|
|
this.wb = new XSSFWorkbook(is);
|
|
|
- }else{
|
|
|
+ }else{
|
|
|
throw new RuntimeException("文档格式不正确!");
|
|
|
- }
|
|
|
+ }
|
|
|
if (this.wb.getNumberOfSheets()<sheetIndex){
|
|
|
throw new RuntimeException("文档中没有工作表!");
|
|
|
}
|
|
@@ -152,7 +152,7 @@ public class ImportExcel {
|
|
|
this.headerNum = headerNum;
|
|
|
log.debug("Initialize success.");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取行对象
|
|
|
* @param rownum
|
|
@@ -169,7 +169,7 @@ public class ImportExcel {
|
|
|
public int getDataRowNum(){
|
|
|
return headerNum+1;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取最后一个数据行号
|
|
|
* @return
|
|
@@ -177,7 +177,7 @@ public class ImportExcel {
|
|
|
public int getLastDataRowNum(){
|
|
|
return this.sheet.getLastRowNum()+headerNum;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取最后一个列号
|
|
|
* @return
|
|
@@ -185,7 +185,7 @@ public class ImportExcel {
|
|
|
public int getLastCellNum(){
|
|
|
return this.getRow(headerNum).getLastCellNum();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取单元格值
|
|
|
* @param row 获取的行
|
|
@@ -226,7 +226,7 @@ public class ImportExcel {
|
|
|
}
|
|
|
return val;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取导入数据列表
|
|
|
* @param cls 导入对象类型
|
|
@@ -234,7 +234,7 @@ public class ImportExcel {
|
|
|
*/
|
|
|
public <E> List<E> getDataList(Class<E> cls, int... groups) throws InstantiationException, IllegalAccessException{
|
|
|
List<Object[]> annotationList = Lists.newArrayList();
|
|
|
- // Get annotation field
|
|
|
+ // Get annotation field
|
|
|
Field[] fs = cls.getDeclaredFields();
|
|
|
for (Field f : fs){
|
|
|
ExcelField ef = f.getAnnotation(ExcelField.class);
|
|
@@ -292,7 +292,7 @@ public class ImportExcel {
|
|
|
//log.debug("Import column count:"+annotationList.size());
|
|
|
// Get excel data
|
|
|
List<E> dataList = Lists.newArrayList();
|
|
|
- for (int i = this.getDataRowNum(); i < this.getLastDataRowNum(); i++) {
|
|
|
+ for (int i = this.getDataRowNum(); i <= this.getLastDataRowNum(); i++) {
|
|
|
E e = (E)cls.newInstance();
|
|
|
int column = 0;
|
|
|
Row row = this.getRow(i);
|
|
@@ -338,7 +338,7 @@ public class ImportExcel {
|
|
|
val = Float.valueOf(val.toString());
|
|
|
}else if (valType == Date.class){
|
|
|
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- val=sdf.parse(val.toString());
|
|
|
+ val=sdf.parse(val.toString());
|
|
|
}else if (valType == User.class){
|
|
|
val = UserUtils.getByUserName(val.toString());
|
|
|
}else if (valType == Office.class){
|
|
@@ -349,7 +349,7 @@ public class ImportExcel {
|
|
|
if (ef.fieldType() != Class.class){
|
|
|
val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
|
|
|
}else{
|
|
|
- val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
|
|
|
+ val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
|
|
|
"fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
|
|
|
}
|
|
|
}
|
|
@@ -380,9 +380,9 @@ public class ImportExcel {
|
|
|
// * 导入测试
|
|
|
// */
|
|
|
// 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++) {
|
|
@@ -391,7 +391,7 @@ public class ImportExcel {
|
|
|
// }
|
|
|
// System.out.print("\n");
|
|
|
// }
|
|
|
-//
|
|
|
+//
|
|
|
// }
|
|
|
|
|
|
}
|