|
@@ -1,26 +1,12 @@
|
|
|
package com.goafanti.common.utils.excel;
|
|
package com.goafanti.common.utils.excel;
|
|
|
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
|
-import java.io.File;
|
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.io.InputStream;
|
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
|
-import java.lang.reflect.Field;
|
|
|
|
|
-import java.lang.reflect.Method;
|
|
|
|
|
-import java.math.BigDecimal;
|
|
|
|
|
-import java.net.URL;
|
|
|
|
|
-import java.net.URLConnection;
|
|
|
|
|
-import java.text.DecimalFormat;
|
|
|
|
|
-import java.time.LocalDate;
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
|
|
-import java.util.*;
|
|
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
-
|
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
-
|
|
|
|
|
|
|
+import com.goafanti.common.bo.Result;
|
|
|
|
|
+import com.goafanti.common.constant.AFTConstants;
|
|
|
|
|
+import com.goafanti.common.error.BusinessException;
|
|
|
|
|
+import com.goafanti.common.utils.DateUtils;
|
|
|
|
|
+import com.goafanti.common.utils.StringUtils;
|
|
|
|
|
+import com.goafanti.common.utils.excel.Excel.ColumnType;
|
|
|
|
|
+import com.goafanti.common.utils.excel.Excel.Type;
|
|
|
import org.apache.poi.POIXMLDocumentPart;
|
|
import org.apache.poi.POIXMLDocumentPart;
|
|
|
import org.apache.poi.hssf.usermodel.*;
|
|
import org.apache.poi.hssf.usermodel.*;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
import org.apache.poi.ss.usermodel.*;
|
|
@@ -32,13 +18,18 @@ import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
-import com.goafanti.common.bo.Result;
|
|
|
|
|
-import com.goafanti.common.constant.AFTConstants;
|
|
|
|
|
-import com.goafanti.common.error.BusinessException;
|
|
|
|
|
-import com.goafanti.common.utils.DateUtils;
|
|
|
|
|
-import com.goafanti.common.utils.StringUtils;
|
|
|
|
|
-import com.goafanti.common.utils.excel.Excel.ColumnType;
|
|
|
|
|
-import com.goafanti.common.utils.excel.Excel.Type;
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import java.io.*;
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.net.URL;
|
|
|
|
|
+import java.net.URLConnection;
|
|
|
|
|
+import java.text.DecimalFormat;
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Excel相关处理
|
|
* Excel相关处理
|
|
@@ -1169,8 +1160,7 @@ public class NewExcelUtil<T> {
|
|
|
// 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
|
|
// 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
|
|
|
int rows = sheet.getLastRowNum();
|
|
int rows = sheet.getLastRowNum();
|
|
|
|
|
|
|
|
- if (rows > 0)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (rows > 0){
|
|
|
// 定义一个map用于存放excel列的序号和field.
|
|
// 定义一个map用于存放excel列的序号和field.
|
|
|
Map<String, Integer> cellMap = new HashMap<String, Integer>();
|
|
Map<String, Integer> cellMap = new HashMap<String, Integer>();
|
|
|
// 获取表头
|
|
// 获取表头
|
|
@@ -1200,117 +1190,124 @@ public class NewExcelUtil<T> {
|
|
|
fieldsMap.put(column, objects);
|
|
fieldsMap.put(column, objects);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- for (int i = titleNum + 1; i <= rows; i++)
|
|
|
|
|
- {
|
|
|
|
|
- // 从第2行开始取数据,默认第一行是表头.
|
|
|
|
|
- Row row = sheet.getRow(i);
|
|
|
|
|
- // 判断当前行是否是空行
|
|
|
|
|
- if (isRowEmpty(row))
|
|
|
|
|
- {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- T entity = null;
|
|
|
|
|
- for (Map.Entry<Integer, Object[]> entry : fieldsMap.entrySet())
|
|
|
|
|
- {
|
|
|
|
|
- Object val = this.getCellValue(row, entry.getKey());
|
|
|
|
|
-
|
|
|
|
|
- // 如果不存在实例则新建.
|
|
|
|
|
- entity = (entity == null ? clazz.newInstance() : entity);
|
|
|
|
|
- // 从map中得到对应列的field.
|
|
|
|
|
- Field field = (Field) entry.getValue()[0];
|
|
|
|
|
- Excel attr = (Excel) entry.getValue()[1];
|
|
|
|
|
- // 取得类型,并根据对象类型设置值.
|
|
|
|
|
- Class<?> fieldType = field.getType();
|
|
|
|
|
- if (String.class == fieldType)
|
|
|
|
|
|
|
+ int errorCount=0;
|
|
|
|
|
+ try {
|
|
|
|
|
+ for (int i = titleNum + 1; i <= rows; i++){
|
|
|
|
|
+ errorCount=i+1;
|
|
|
|
|
+ // 从第2行开始取数据,默认第一行是表头.
|
|
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
|
|
+ // 判断当前行是否是空行
|
|
|
|
|
+ if (isRowEmpty(row))
|
|
|
{
|
|
{
|
|
|
- String s = Convert.toStr(val);
|
|
|
|
|
- if (StringUtils.endsWith(s, ".0"))
|
|
|
|
|
- {
|
|
|
|
|
- val = StringUtils.substringBefore(s, ".0");
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ T entity = null;
|
|
|
|
|
+ for (Map.Entry<Integer, Object[]> entry : fieldsMap.entrySet())
|
|
|
|
|
+ {
|
|
|
|
|
+ Object val = this.getCellValue(row, entry.getKey());
|
|
|
|
|
+
|
|
|
|
|
+ // 如果不存在实例则新建.
|
|
|
|
|
+ entity = (entity == null ? clazz.newInstance() : entity);
|
|
|
|
|
+ // 从map中得到对应列的field.
|
|
|
|
|
+ Field field = (Field) entry.getValue()[0];
|
|
|
|
|
+ Excel attr = (Excel) entry.getValue()[1];
|
|
|
|
|
+ // 取得类型,并根据对象类型设置值.
|
|
|
|
|
+ Class<?> fieldType = field.getType();
|
|
|
|
|
+ if (String.class == fieldType)
|
|
|
{
|
|
{
|
|
|
- String dateFormat = field.getAnnotation(Excel.class).dateFormat();
|
|
|
|
|
- if (StringUtils.isNotEmpty(dateFormat))
|
|
|
|
|
|
|
+ String s = Convert.toStr(val);
|
|
|
|
|
+ if (StringUtils.endsWith(s, ".0"))
|
|
|
{
|
|
{
|
|
|
- val = parseDateToStr(dateFormat, val);
|
|
|
|
|
|
|
+ val = StringUtils.substringBefore(s, ".0");
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- val = Convert.toStr(val);
|
|
|
|
|
|
|
+ String dateFormat = field.getAnnotation(Excel.class).dateFormat();
|
|
|
|
|
+ if (StringUtils.isNotEmpty(dateFormat))
|
|
|
|
|
+ {
|
|
|
|
|
+ val = parseDateToStr(dateFormat, val);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ val = Convert.toStr(val);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
|
|
|
|
|
- {
|
|
|
|
|
- val = Convert.toInt(val);
|
|
|
|
|
- }
|
|
|
|
|
- else if ((Long.TYPE == fieldType || Long.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
|
|
|
|
|
- {
|
|
|
|
|
- val = Convert.toLong(val);
|
|
|
|
|
- }
|
|
|
|
|
- else if (Double.TYPE == fieldType || Double.class == fieldType)
|
|
|
|
|
- {
|
|
|
|
|
- val = Convert.toDouble(val);
|
|
|
|
|
- }
|
|
|
|
|
- else if (Float.TYPE == fieldType || Float.class == fieldType)
|
|
|
|
|
- {
|
|
|
|
|
- val = Convert.toFloat(val);
|
|
|
|
|
- }
|
|
|
|
|
- else if (BigDecimal.class == fieldType)
|
|
|
|
|
- {
|
|
|
|
|
- val = Convert.toBigDecimal(val);
|
|
|
|
|
- }
|
|
|
|
|
- else if (Date.class == fieldType)
|
|
|
|
|
- {
|
|
|
|
|
- if (val instanceof String)
|
|
|
|
|
|
|
+ else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
|
|
|
{
|
|
{
|
|
|
- val = DateUtils.parseDate((String) val);
|
|
|
|
|
|
|
+ val = Convert.toInt(val);
|
|
|
}
|
|
}
|
|
|
- else if (val instanceof Double)
|
|
|
|
|
|
|
+ else if ((Long.TYPE == fieldType || Long.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
|
|
|
{
|
|
{
|
|
|
- val = DateUtil.getJavaDate((Double) val);
|
|
|
|
|
|
|
+ val = Convert.toLong(val);
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- else if (Boolean.TYPE == fieldType || Boolean.class == fieldType)
|
|
|
|
|
- {
|
|
|
|
|
- val = Convert.toBool(val, false);
|
|
|
|
|
- }
|
|
|
|
|
- if (StringUtils.isNotNull(fieldType))
|
|
|
|
|
- {
|
|
|
|
|
- String propertyName = field.getName();
|
|
|
|
|
- if (StringUtils.isNotEmpty(attr.targetAttr()))
|
|
|
|
|
|
|
+ else if (Double.TYPE == fieldType || Double.class == fieldType)
|
|
|
{
|
|
{
|
|
|
- propertyName = field.getName() + "." + attr.targetAttr();
|
|
|
|
|
|
|
+ val = Convert.toDouble(val);
|
|
|
}
|
|
}
|
|
|
- else if (StringUtils.isNotEmpty(attr.readConverterExp()))
|
|
|
|
|
|
|
+ else if (Float.TYPE == fieldType || Float.class == fieldType)
|
|
|
{
|
|
{
|
|
|
- val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
|
|
|
|
|
|
|
+ val = Convert.toFloat(val);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- else if (!attr.handler().equals(ExcelHandlerAdapter.class))
|
|
|
|
|
|
|
+ else if (BigDecimal.class == fieldType)
|
|
|
{
|
|
{
|
|
|
- val = dataFormatHandlerAdapter(val, attr);
|
|
|
|
|
|
|
+ val = Convert.toBigDecimal(val);
|
|
|
}
|
|
}
|
|
|
- else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty((CharSequence) pictures))
|
|
|
|
|
|
|
+ else if (Date.class == fieldType)
|
|
|
{
|
|
{
|
|
|
- PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey());
|
|
|
|
|
- if (image == null)
|
|
|
|
|
|
|
+ if (val instanceof String)
|
|
|
{
|
|
{
|
|
|
- val = "";
|
|
|
|
|
|
|
+ val = DateUtils.parseDate((String) val);
|
|
|
}
|
|
}
|
|
|
- else
|
|
|
|
|
|
|
+ else if (val instanceof Double)
|
|
|
|
|
+ {
|
|
|
|
|
+ val = DateUtil.getJavaDate((Double) val);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (Boolean.TYPE == fieldType || Boolean.class == fieldType)
|
|
|
|
|
+ {
|
|
|
|
|
+ val = Convert.toBool(val, false);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isNotNull(fieldType))
|
|
|
|
|
+ {
|
|
|
|
|
+ String propertyName = field.getName();
|
|
|
|
|
+ if (StringUtils.isNotEmpty(attr.targetAttr()))
|
|
|
|
|
+ {
|
|
|
|
|
+ propertyName = field.getName() + "." + attr.targetAttr();
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (StringUtils.isNotEmpty(attr.readConverterExp()))
|
|
|
|
|
+ {
|
|
|
|
|
+ val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ else if (!attr.handler().equals(ExcelHandlerAdapter.class))
|
|
|
|
|
+ {
|
|
|
|
|
+ val = dataFormatHandlerAdapter(val, attr);
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty((CharSequence) pictures))
|
|
|
{
|
|
{
|
|
|
- byte[] data = image.getData();
|
|
|
|
|
- val = FileUtils.writeImportBytes(data);
|
|
|
|
|
|
|
+ PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey());
|
|
|
|
|
+ if (image == null)
|
|
|
|
|
+ {
|
|
|
|
|
+ val = "";
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ byte[] data = image.getData();
|
|
|
|
|
+ val = FileUtils.writeImportBytes(data);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ ReflectUtils.invokeSetter(entity, propertyName, val);
|
|
|
}
|
|
}
|
|
|
- ReflectUtils.invokeSetter(entity, propertyName, val);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ list.add(entity);
|
|
|
}
|
|
}
|
|
|
- list.add(entity);
|
|
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ throw new BusinessException(String.format("表格第%s行内容错误,请检查修改后再上传。",errorCount));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return list;
|
|
return list;
|
|
|
}
|
|
}
|
|
|
|
|
|