一、概述
相信大家在工作过程中,导入导出中会出现各种各样的问题,比如:数据源为空、有重复行等。那么,有没有通用的导出方式呢,这里,就带着大家一起来用Java实现一个通用的导出Excel的工具。
二、项目实现
1、构建pom.xml
我们的工程是利用Maven来构建的,项目具体搭建过程大家可以参见网上其他资料,这里我们仅给出最核心的Maven配置
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-scratchpadartifactId>
<version>3.11-beta2version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>3.11-beta2version>
dependency>
<dependency> <groupId>org.apache.poigroupId> <artifactId>poi-ooxml-schemasartifactId> <version>3.11-beta2version>dependency><dependency> <groupId>org.apache.poigroupId> <artifactId>poi-excelantartifactId> <version>3.11-beta2version>dependency>
package com.fhiguin.common.handler;import com.fhiguin.common.base.Result;import com.fhiguin.common.exception.*;import lombok.extern.slf4j.Slf4j;import org.springframework.http.HttpStatus;import org.springframework.validation.BindException;import org.springframework.validation.FieldError;import org.springframework.web.HttpRequestMethodNotSupportedException;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.RestControllerAdvice;import java.util.List;@Slf4j@RestControllerAdvicepublic class GlobalExceptionHandler { /** * @Description TODO 系统内部异常 **/ @ExceptionHandler(value = Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public Result handleException(Exception e) { log.error("系统内部异常,异常信息:", e); return new Result(false, "操作失败,请稍后重试或联系管理员!"); } // 调用方式出错 @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) public Result httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { // log.warn("调用方法错误:", e); log.warn("方法调用方式错误!当前请求方式为:" + e.getMethod()); return new Result(false, "调用方式错误!"); } @ExceptionHandler(value = UnLoginException.class) @ResponseStatus(HttpStatus.FORBIDDEN) public Result unLoginException(UnLoginException e) { //log.error("登录超时:", e); log.warn("登录超时"); return new Result(false, "登录超时,请重新登陆!"); } @ExceptionHandler(value = PageErrorException.class) @ResponseStatus(HttpStatus.OK) public Result pageErrorException(PageErrorException e) { log.error("分页参数不合法"); return new Result(false, "分页参数不合法!"); } @ExceptionHandler(value = LimitAccessException.class) @ResponseStatus(HttpStatus.TOO_MANY_REQUESTS) public Result handleLimitAccessException(LimitAccessException e) { log.warn(e.getMessage()); return new Result(false, "请求过于频繁!"); } @ExceptionHandler(value = UnauthorizedException.class) @ResponseStatus(HttpStatus.FORBIDDEN) public Result handleUnauthorizedException(Exception e) { log.error("权限不足,", e.getMessage()); return new Result(false, "权限不足!"); } @ExceptionHandler(value = OperationException.class) @ResponseStatus(HttpStatus.OK) public Result operationException(Exception e) { log.error("操作失败,", e.getMessage()); return new Result(false, "操作失败,请稍后重试!"); } /** * @Description TODO 统一处理请求参数校验(实体对象传参) **/ @ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public Result validExceptionHandler(BindException e) { StringBuilder message = new StringBuilder(); List fieldErrors = e.getBindingResult().getFieldErrors(); for (FieldError error : fieldErrors) { //message.append(error.getField()).append(error.getDefaultMessage()).append(StringPool.COMMA); } message = new StringBuilder(message.substring(0, message.length() - 1)); return new Result(false, "参数有误!", message); }}