preface
And take over the old project renovation.. The excel import written by POI before would cause oom memory overflow, so I used EasyExcel to rewrite the writing based on the original project structure, which was really uncomfortable
To sum up the article in one sentence:
reference
start
Since I need to perform data verification based on the logic of the original project after import, I need to perform data verification based on the logic of the Service layer (HERE I will write based on the structure of the original project, and easyExcel has data verification annotations for details, please refer to the official document).
The service layer will throw an exception if the check fails. Normally, the controller layer will catch the exception and return it to the page, but when using EasyExcel the overridden method does not throw an exception The method overridden by the Java rewriting rules can only throw a smaller exception than the original method, which means that it will be gone by the time THE service layer throws an exception past the listener
If you want to return a foreground column that failed, you need to pass the value,
Steps:
Initialize an Ajax return object at the Controller layer and pass the return object through the listener constructor to catch the exception of the service (or you can return the error message, this old project is written to bring the error message out with an exception). Just store the error message in the Ajax return object, and then the Controller returns the Ajax object directly
The code:
controller
@ResponseBody
@RequestMapping(value = "orderImport", method = RequestMethod.POST, produces = Constant.CONTENT_TYPE_UTF8)
public RestResponse orderImport(HttpServletRequest request, @RequestParam(value = "file") MultipartFile file) throws Exception {
UserVO userVo = this.getCurrentUser(request);
// Initializing Ajax returns an object that defaults to success
RestResponse build = RestResponse.build();
EasyExcel.read(file.getInputStream(), new NoModleDataListener(orderService, userVo, build)).sheet().doRead();
If there is no error, the value of the object has been changed. If there is no error, the value has not been changed
return build;
// return orderService.importOrder(userVo, file);
}
Copy the code
Listener:
/ * * *@author: [tseng] '[email protected]' *@Date: 2020-01-07 11:09
* @Description: < map receives parsed data - listener > */
public class NoModleDataListener extends AnalysisEventListener<Map<Integer.String>> {
private static final Logger LOGGER = LoggerFactory.getLogger(NoModleDataListener.class);
/** Business logic layer */
private OrderService orderService;
Vo * / / * * the user
private UserVO userVO;
/** Ajax returns an object */
private RestResponse build;
/ * * * / excel head
private Map<Integer, String> headMaps;
/** * listener constructor **@paramOrderService business logic layer *@paramUserVO userVO (Service Service) *@paramBuild Ajax returns the object */
public NoModleDataListener(OrderService orderService, UserVO userVO, RestResponse build) {
this.orderService = orderService;
this.userVO = userVO;
this.build = build;
}
/** * Stores the database every 3000 entries */
private static final int BATCH_COUNT = 3000;
List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
@Override
public void invoke(Map<Integer, String> data, AnalysisContext context) {
list.add(data);
if (list.size() >= BATCH_COUNT) {
try {
saveData();
} catch (Exception e) {
build.setCode(1); build.setMsg(e.getMessage()); } list.clear(); }}@Override
public void doAfterAllAnalysed(AnalysisContext context) {
try {
saveData();
} catch (Exception e) {
// Change the return object code to the error status code
build.setCode(1);
// Save the error messagebuild.setMsg(e.getMessage()); }}@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
headMaps = headMap;
}
/** * Store database */
private void saveData(a) throws Exception {
orderService.importOrders(userVO, list, headMaps);
LOGGER.info("Database saved successfully!");
}
/** * Error parsing will enter the method to see the source code or documentation */
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
System.out.println("hello");
throwexception; }}Copy the code