优化excel预览逻辑
This commit is contained in:
parent
11315eca05
commit
22d52ac529
|
@ -1,6 +1,5 @@
|
||||||
package com.diboot.file.controller;
|
package com.diboot.file.controller;
|
||||||
|
|
||||||
import com.diboot.file.excel.listener.DynamicHeadExcelListener;
|
|
||||||
import com.diboot.file.excel.listener.FixedHeadExcelListener;
|
import com.diboot.file.excel.listener.FixedHeadExcelListener;
|
||||||
import com.diboot.file.util.ExcelHelper;
|
import com.diboot.file.util.ExcelHelper;
|
||||||
import com.diboot.file.util.FileHelper;
|
import com.diboot.file.util.FileHelper;
|
||||||
|
@ -61,9 +60,8 @@ public abstract class BaseExcelFileController extends BaseFileController {
|
||||||
String fileUid = S.substringBefore(previewFileName, ".");
|
String fileUid = S.substringBefore(previewFileName, ".");
|
||||||
String fullPath = FileHelper.getFullPath(previewFileName);
|
String fullPath = FileHelper.getFullPath(previewFileName);
|
||||||
String ext = FileHelper.getFileExtByName(originFileName);
|
String ext = FileHelper.getFileExtByName(originFileName);
|
||||||
String description = getString(request, "description");
|
|
||||||
// 保存文件上传记录
|
// 保存文件上传记录
|
||||||
createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, description);
|
createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, request);
|
||||||
return new JsonResult(Status.OK);
|
return new JsonResult(Status.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,13 +92,10 @@ public abstract class BaseExcelFileController extends BaseFileController {
|
||||||
String newFileName = S.newUuid() + "." + ext;
|
String newFileName = S.newUuid() + "." + ext;
|
||||||
String fullPath = FileHelper.saveFile(file, newFileName);
|
String fullPath = FileHelper.saveFile(file, newFileName);
|
||||||
// 预览
|
// 预览
|
||||||
DynamicHeadExcelListener listener = new DynamicHeadExcelListener() {
|
FixedHeadExcelListener listener = getExcelDataListener();
|
||||||
@Override
|
listener.setRequestParams(super.getParamsMap(request));
|
||||||
protected void saveData(Map<Integer, String> headMap, List<Map<Integer, String>> dataList) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
try {
|
try {
|
||||||
ExcelHelper.readDynamicHeadExcel(fullPath, listener);
|
ExcelHelper.previewReadExcel(fullPath, listener);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
log.warn("解析并校验excel文件失败", e);
|
log.warn("解析并校验excel文件失败", e);
|
||||||
|
@ -125,11 +120,13 @@ public abstract class BaseExcelFileController extends BaseFileController {
|
||||||
* 保存文件之后的处理逻辑,如解析excel
|
* 保存文件之后的处理逻辑,如解析excel
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected int extractDataCount(String fileUuid, String fullPath) throws Exception{
|
protected int extractDataCount(String fileUuid, String fullPath, HttpServletRequest request) throws Exception{
|
||||||
FixedHeadExcelListener listener = getExcelDataListener();
|
FixedHeadExcelListener listener = getExcelDataListener();
|
||||||
listener.setUploadFileUuid(fileUuid);
|
listener.setUploadFileUuid(fileUuid);
|
||||||
|
listener.setPreview(false);
|
||||||
|
listener.setRequestParams(super.getParamsMap(request));
|
||||||
try{
|
try{
|
||||||
ExcelHelper.readAndSave(fullPath, listener);
|
ExcelHelper.readAndSaveExcel(fullPath, listener);
|
||||||
}
|
}
|
||||||
catch(Exception e){
|
catch(Exception e){
|
||||||
log.warn("上传数据错误: "+ e.getMessage(), e);
|
log.warn("上传数据错误: "+ e.getMessage(), e);
|
||||||
|
|
|
@ -68,9 +68,8 @@ public abstract class BaseFileController extends BaseController {
|
||||||
String fileUid = S.newUuid();
|
String fileUid = S.newUuid();
|
||||||
String newFileName = fileUid + "." + ext;
|
String newFileName = fileUid + "." + ext;
|
||||||
String fullPath = FileHelper.saveFile(file, newFileName);
|
String fullPath = FileHelper.saveFile(file, newFileName);
|
||||||
String description = getString(request, "description");
|
|
||||||
// 保存文件上传记录
|
// 保存文件上传记录
|
||||||
createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, description);
|
createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, request);
|
||||||
Map<String, String> dataMap = new HashMap<>();
|
Map<String, String> dataMap = new HashMap<>();
|
||||||
dataMap.put("uuid", fileUid);
|
dataMap.put("uuid", fileUid);
|
||||||
return new JsonResult(Status.OK).data(dataMap);
|
return new JsonResult(Status.OK).data(dataMap);
|
||||||
|
@ -83,17 +82,18 @@ public abstract class BaseFileController extends BaseController {
|
||||||
* @param originFileName
|
* @param originFileName
|
||||||
* @param storagePath
|
* @param storagePath
|
||||||
* @param fileType
|
* @param fileType
|
||||||
* @param description
|
* @param request
|
||||||
* @param <T>
|
* @param <T>
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected <T> void createUploadFile(Class<T> entityClass, String fileUid, String originFileName, String storagePath, String fileType, String description) throws Exception{
|
protected <T> void createUploadFile(Class<T> entityClass, String fileUid, String originFileName, String storagePath, String fileType, HttpServletRequest request) throws Exception{
|
||||||
// 保存文件之后的处理逻辑
|
// 保存文件之后的处理逻辑
|
||||||
int dataCount = extractDataCount(fileUid, storagePath);
|
int dataCount = extractDataCount(fileUid, storagePath, request);
|
||||||
UploadFile uploadFile = new UploadFile();
|
UploadFile uploadFile = new UploadFile();
|
||||||
uploadFile.setUuid(fileUid).setFileName(originFileName).setFileType(fileType);
|
uploadFile.setUuid(fileUid).setFileName(originFileName).setFileType(fileType);
|
||||||
uploadFile.setRelObjType(entityClass.getSimpleName()).setStoragePath(storagePath);
|
uploadFile.setRelObjType(entityClass.getSimpleName()).setStoragePath(storagePath);
|
||||||
// 初始设置为0,批量保存数据后更新
|
// 初始设置为0,批量保存数据后更新
|
||||||
|
String description = getString(request, "description");
|
||||||
uploadFile.setDataCount(dataCount).setDescription(description);
|
uploadFile.setDataCount(dataCount).setDescription(description);
|
||||||
// 保存文件上传记录
|
// 保存文件上传记录
|
||||||
uploadFileService.createEntity(uploadFile);
|
uploadFileService.createEntity(uploadFile);
|
||||||
|
@ -102,7 +102,7 @@ public abstract class BaseFileController extends BaseController {
|
||||||
/**
|
/**
|
||||||
* 保存文件之后的处理逻辑,如解析excel
|
* 保存文件之后的处理逻辑,如解析excel
|
||||||
*/
|
*/
|
||||||
protected int extractDataCount(String fileUuid, String fullPath) throws Exception{
|
protected int extractDataCount(String fileUuid, String fullPath, HttpServletRequest request) throws Exception{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import com.diboot.core.vo.Status;
|
||||||
import com.diboot.file.excel.cache.DictTempCache;
|
import com.diboot.file.excel.cache.DictTempCache;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/***
|
/***
|
||||||
|
@ -29,10 +30,21 @@ public abstract class FixedHeadExcelListener<T extends BaseExcelModel> extends A
|
||||||
private List<String> validateErrorMsgs = new ArrayList<>();
|
private List<String> validateErrorMsgs = new ArrayList<>();
|
||||||
// 导入文件的uuid
|
// 导入文件的uuid
|
||||||
protected String uploadFileUuid;
|
protected String uploadFileUuid;
|
||||||
|
// 是否为预览模式
|
||||||
|
private boolean preview = false;
|
||||||
|
// 注入request
|
||||||
|
private Map<String, Object> requestParams;
|
||||||
|
|
||||||
public FixedHeadExcelListener(){
|
public FixedHeadExcelListener(){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPreview(boolean isPrevieew){
|
||||||
|
this.preview = isPrevieew;
|
||||||
|
}
|
||||||
|
public void setRequestParams(Map<String, Object> requestParams){
|
||||||
|
this.requestParams = requestParams;
|
||||||
|
}
|
||||||
|
|
||||||
public void setUploadFileUuid(String uploadFileUuid){
|
public void setUploadFileUuid(String uploadFileUuid){
|
||||||
this.uploadFileUuid = uploadFileUuid;
|
this.uploadFileUuid = uploadFileUuid;
|
||||||
}
|
}
|
||||||
|
@ -70,9 +82,9 @@ public abstract class FixedHeadExcelListener<T extends BaseExcelModel> extends A
|
||||||
throw new BusinessException(Status.FAIL_VALIDATION, S.join(this.validateErrorMsgs, "; "));
|
throw new BusinessException(Status.FAIL_VALIDATION, S.join(this.validateErrorMsgs, "; "));
|
||||||
}
|
}
|
||||||
// 保存
|
// 保存
|
||||||
if(V.notEmpty(dataList)){
|
if(preview == false && V.notEmpty(dataList)){
|
||||||
// 保存数据
|
// 保存数据
|
||||||
saveData(dataList);
|
saveData(dataList, requestParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +149,7 @@ public abstract class FixedHeadExcelListener<T extends BaseExcelModel> extends A
|
||||||
/**
|
/**
|
||||||
* 保存数据
|
* 保存数据
|
||||||
*/
|
*/
|
||||||
protected abstract void saveData(List<T> dataList);
|
protected abstract void saveData(List<T> dataList, Map<String, Object> requestParams);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验错误信息
|
* 校验错误信息
|
||||||
|
|
|
@ -24,12 +24,23 @@ import java.util.List;
|
||||||
public class ExcelHelper {
|
public class ExcelHelper {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 简单读取excel文件数据并保存到数据库
|
* 预览读取excel文件数据
|
||||||
* @param filePath
|
* @param filePath
|
||||||
* @param listener
|
* @param listener
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static <T extends BaseExcelModel> boolean readAndSave(String filePath, FixedHeadExcelListener listener) throws Exception{
|
public static <T extends BaseExcelModel> boolean previewReadExcel(String filePath, FixedHeadExcelListener listener) throws Exception{
|
||||||
|
listener.setPreview(true);
|
||||||
|
return readAndSaveExcel(filePath, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取excel文件数据并保存到数据库
|
||||||
|
* @param filePath
|
||||||
|
* @param listener
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T extends BaseExcelModel> boolean readAndSaveExcel(String filePath, FixedHeadExcelListener listener) throws Exception{
|
||||||
File excel = getExcelFile(filePath);
|
File excel = getExcelFile(filePath);
|
||||||
Class<T> headClazz = BeanUtils.getGenericityClass(listener, 0);
|
Class<T> headClazz = BeanUtils.getGenericityClass(listener, 0);
|
||||||
EasyExcel.read(excel).registerReadListener(listener).head(headClazz).sheet().doRead();
|
EasyExcel.read(excel).registerReadListener(listener).head(headClazz).sheet().doRead();
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <Description>
|
* <Description>
|
||||||
|
@ -39,7 +40,7 @@ public class DepartmentImportListener extends FixedHeadExcelListener<DepartmentE
|
||||||
* @param dataList
|
* @param dataList
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void saveData(List<DepartmentExcelModel> dataList) {
|
protected void saveData(List<DepartmentExcelModel> dataList, Map<String,Object> paramsMap) {
|
||||||
// 转换数据
|
// 转换数据
|
||||||
List<Department> departmentList = BeanUtils.convertList(getDataList(), Department.class);
|
List<Department> departmentList = BeanUtils.convertList(getDataList(), Department.class);
|
||||||
// 保存数据
|
// 保存数据
|
||||||
|
|
|
@ -33,7 +33,7 @@ public class FixedHeadExcelReadTest extends ExcelWriteTest {
|
||||||
prepareNormalDataExcel();
|
prepareNormalDataExcel();
|
||||||
// 读且保存
|
// 读且保存
|
||||||
DepartmentImportListener listener = new DepartmentImportListener();
|
DepartmentImportListener listener = new DepartmentImportListener();
|
||||||
boolean success = ExcelHelper.readAndSave(getTempFilePath(), listener);
|
boolean success = ExcelHelper.previewReadExcel(getTempFilePath(), listener);
|
||||||
Assert.assertTrue(success);
|
Assert.assertTrue(success);
|
||||||
System.out.println(JSON.stringify(listener.getDataList()));
|
System.out.println(JSON.stringify(listener.getDataList()));
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ public class FixedHeadExcelReadTest extends ExcelWriteTest {
|
||||||
// 预览读
|
// 预览读
|
||||||
//ExcelHelper.previewRead(getTempFilePath(), new DepartmentImportListener());
|
//ExcelHelper.previewRead(getTempFilePath(), new DepartmentImportListener());
|
||||||
// 读且保存
|
// 读且保存
|
||||||
ExcelHelper.readAndSave(getTempFilePath(), new DepartmentImportListener());
|
ExcelHelper.previewReadExcel(getTempFilePath(), new DepartmentImportListener());
|
||||||
}
|
}
|
||||||
catch (Exception e){
|
catch (Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
Loading…
Reference in New Issue