|
@@ -4,7 +4,8 @@ import com.railway.common.annotation.Excel;
|
|
|
import com.railway.common.annotation.Excel.ColumnType;
|
|
import com.railway.common.annotation.Excel.ColumnType;
|
|
|
import com.railway.common.annotation.Excel.Type;
|
|
import com.railway.common.annotation.Excel.Type;
|
|
|
import com.railway.common.annotation.Excels;
|
|
import com.railway.common.annotation.Excels;
|
|
|
-import com.railway.common.core.domain.AjaxResult;
|
|
|
|
|
|
|
+import com.railway.common.config.RailwayConfig;
|
|
|
|
|
+import com.railway.common.constant.Constants;
|
|
|
import com.railway.common.core.text.Convert;
|
|
import com.railway.common.core.text.Convert;
|
|
|
import com.railway.common.exception.UtilException;
|
|
import com.railway.common.exception.UtilException;
|
|
|
import com.railway.common.utils.DateUtils;
|
|
import com.railway.common.utils.DateUtils;
|
|
@@ -72,7 +73,6 @@ import org.apache.poi.xssf.usermodel.XSSFPicture;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFShape;
|
|
import org.apache.poi.xssf.usermodel.XSSFShape;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
|
|
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Excel相关处理
|
|
* Excel相关处理
|
|
@@ -82,12 +82,6 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class ExcelUtil<T> {
|
|
public class ExcelUtil<T> {
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 上传文件存储在本地的根路径
|
|
|
|
|
- */
|
|
|
|
|
- @Value("${file.path}")
|
|
|
|
|
- private String localFilePath;
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* Excel sheet最大行数,默认65536
|
|
* Excel sheet最大行数,默认65536
|
|
|
*/
|
|
*/
|
|
@@ -235,7 +229,7 @@ public class ExcelUtil<T> {
|
|
|
|
|
|
|
|
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<>();
|
|
|
// 获取表头
|
|
// 获取表头
|
|
|
Row heard = sheet.getRow(titleNum);
|
|
Row heard = sheet.getRow(titleNum);
|
|
|
for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) {
|
|
for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) {
|
|
@@ -250,7 +244,7 @@ public class ExcelUtil<T> {
|
|
|
// 有数据时才处理 得到类的所有field.
|
|
// 有数据时才处理 得到类的所有field.
|
|
|
Field[] allFields = clazz.getDeclaredFields();
|
|
Field[] allFields = clazz.getDeclaredFields();
|
|
|
// 定义一个map用于存放列的序号和field.
|
|
// 定义一个map用于存放列的序号和field.
|
|
|
- Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();
|
|
|
|
|
|
|
+ Map<Integer, Field> fieldsMap = new HashMap<>();
|
|
|
for (Field field : allFields) {
|
|
for (Field field : allFields) {
|
|
|
Excel attr = field.getAnnotation(Excel.class);
|
|
Excel attr = field.getAnnotation(Excel.class);
|
|
|
if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) {
|
|
if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) {
|
|
@@ -311,18 +305,16 @@ public class ExcelUtil<T> {
|
|
|
} else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) {
|
|
} else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) {
|
|
|
val = Convert.toBool(val, false);
|
|
val = Convert.toBool(val, false);
|
|
|
}
|
|
}
|
|
|
- if (StringUtils.isNotNull(fieldType)) {
|
|
|
|
|
- Excel attr = field.getAnnotation(Excel.class);
|
|
|
|
|
- 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);
|
|
|
|
|
- }
|
|
|
|
|
- ReflectUtils.invokeSetter(entity, propertyName, val);
|
|
|
|
|
|
|
+ Excel attr = field.getAnnotation(Excel.class);
|
|
|
|
|
+ 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);
|
|
|
}
|
|
}
|
|
|
|
|
+ ReflectUtils.invokeSetter(entity, propertyName, val);
|
|
|
}
|
|
}
|
|
|
list.add(entity);
|
|
list.add(entity);
|
|
|
}
|
|
}
|
|
@@ -337,7 +329,7 @@ public class ExcelUtil<T> {
|
|
|
* @param sheetName 工作表的名称
|
|
* @param sheetName 工作表的名称
|
|
|
* @return 结果
|
|
* @return 结果
|
|
|
*/
|
|
*/
|
|
|
- public AjaxResult exportExcel(List<T> list, String sheetName) {
|
|
|
|
|
|
|
+ public String exportExcel(List<T> list, String sheetName) {
|
|
|
return exportExcel(list, sheetName, StringUtils.EMPTY);
|
|
return exportExcel(list, sheetName, StringUtils.EMPTY);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -349,7 +341,7 @@ public class ExcelUtil<T> {
|
|
|
* @param title 标题
|
|
* @param title 标题
|
|
|
* @return 结果
|
|
* @return 结果
|
|
|
*/
|
|
*/
|
|
|
- public AjaxResult exportExcel(List<T> list, String sheetName, String title) {
|
|
|
|
|
|
|
+ public String exportExcel(List<T> list, String sheetName, String title) {
|
|
|
this.init(list, sheetName, title, Type.EXPORT);
|
|
this.init(list, sheetName, title, Type.EXPORT);
|
|
|
return exportExcel();
|
|
return exportExcel();
|
|
|
}
|
|
}
|
|
@@ -388,7 +380,7 @@ public class ExcelUtil<T> {
|
|
|
* @param sheetName 工作表的名称
|
|
* @param sheetName 工作表的名称
|
|
|
* @return 结果
|
|
* @return 结果
|
|
|
*/
|
|
*/
|
|
|
- public AjaxResult importTemplateExcel(String sheetName) {
|
|
|
|
|
|
|
+ public String importTemplateExcel(String sheetName) {
|
|
|
return importTemplateExcel(sheetName, StringUtils.EMPTY);
|
|
return importTemplateExcel(sheetName, StringUtils.EMPTY);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -399,7 +391,7 @@ public class ExcelUtil<T> {
|
|
|
* @param title 标题
|
|
* @param title 标题
|
|
|
* @return 结果
|
|
* @return 结果
|
|
|
*/
|
|
*/
|
|
|
- public AjaxResult importTemplateExcel(String sheetName, String title) {
|
|
|
|
|
|
|
+ public String importTemplateExcel(String sheetName, String title) {
|
|
|
this.init(null, sheetName, title, Type.IMPORT);
|
|
this.init(null, sheetName, title, Type.IMPORT);
|
|
|
return exportExcel();
|
|
return exportExcel();
|
|
|
}
|
|
}
|
|
@@ -438,14 +430,15 @@ public class ExcelUtil<T> {
|
|
|
*
|
|
*
|
|
|
* @return 结果
|
|
* @return 结果
|
|
|
*/
|
|
*/
|
|
|
- public AjaxResult exportExcel() {
|
|
|
|
|
|
|
+ public String exportExcel() {
|
|
|
OutputStream out = null;
|
|
OutputStream out = null;
|
|
|
try {
|
|
try {
|
|
|
writeSheet();
|
|
writeSheet();
|
|
|
String filename = encodingFilename(sheetName);
|
|
String filename = encodingFilename(sheetName);
|
|
|
- out = new FileOutputStream(getAbsoluteFile(filename));
|
|
|
|
|
|
|
+ String filePath = getAbsoluteFile(filename);
|
|
|
|
|
+ out = new FileOutputStream(filePath);
|
|
|
wb.write(out);
|
|
wb.write(out);
|
|
|
- return AjaxResult.success(filename);
|
|
|
|
|
|
|
+ return filePath;
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("导出Excel异常{}", e.getMessage());
|
|
log.error("导出Excel异常{}", e.getMessage());
|
|
|
throw new UtilException("导出Excel失败,请联系网站管理员!");
|
|
throw new UtilException("导出Excel失败,请联系网站管理员!");
|
|
@@ -903,7 +896,7 @@ public class ExcelUtil<T> {
|
|
|
* 编码文件名
|
|
* 编码文件名
|
|
|
*/
|
|
*/
|
|
|
public String encodingFilename(String filename) {
|
|
public String encodingFilename(String filename) {
|
|
|
- filename = UUID.randomUUID() + "_" + filename + ".xlsx";
|
|
|
|
|
|
|
+ filename = UUID.randomUUID() + Constants.UNDERLINE + filename + ".xlsx";
|
|
|
return filename;
|
|
return filename;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -913,7 +906,7 @@ public class ExcelUtil<T> {
|
|
|
* @param filename 文件名称
|
|
* @param filename 文件名称
|
|
|
*/
|
|
*/
|
|
|
public String getAbsoluteFile(String filename) {
|
|
public String getAbsoluteFile(String filename) {
|
|
|
- String downloadPath = localFilePath + File.separator + filename;
|
|
|
|
|
|
|
+ String downloadPath = RailwayConfig.getFilePath() + File.separator + filename;
|
|
|
File desc = new File(downloadPath);
|
|
File desc = new File(downloadPath);
|
|
|
if (!desc.getParentFile().exists()) {
|
|
if (!desc.getParentFile().exists()) {
|
|
|
desc.getParentFile().mkdirs();
|
|
desc.getParentFile().mkdirs();
|