excel java导入导出实例_还在重复造轮子?Java之Excel导入导出通用工具类

一、概述

相信大家在工作过程中,导入导出中会出现各种各样的问题,比如:数据源为空、有重复行等。那么,有没有通用的导出方式呢,这里,就带着大家一起来用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);    }}

4accb0d4cfa1edea4f7bc02386ec3a7b.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值