1. 程式人生 > >easypoi解析前端multipart檔案到Java物件註解類

easypoi解析前端multipart檔案到Java物件註解類

導包:

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

定義註解:

/**
 * 自動解析前端上傳的檔案到Java物件註解類.
 */
@Documented
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelRequestBody {

    // Excel需要轉換成的類.
    Class<?> requireClass();

    // 前段上傳檔案的引數名稱.
    String name() default "file";

    int titleRows();

    int headRows();

    boolean hasSeq() default true;

    // Excel型別.預設03、07格式.
    ExcelType type() default ExcelType.HSSF;

}

定義註解解析器:

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.ahies.system.sso.annotation.ExcelRequestBody;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Excel方法引數的解析器.
 *
 * 使用@ExcelRequestBody註釋的引數,excel檔案將在spring mvc繫結引數時被轉換成excel vo.
 *
 * @author chengyuebin
 */
@Component
public class ExcelArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(ExcelRequestBody.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);

        ExcelRequestBody annotation = parameter.getParameterAnnotation(ExcelRequestBody.class);

        if (multipartRequest != null) {
            List<Object> result = new ArrayList<Object>();
            List<MultipartFile> files = multipartRequest.getFiles(annotation.name());

            ImportParams params = new ImportParams();
            params.setTitleRows(annotation.titleRows());
            params.setHeadRows(annotation.headRows());
            //params.setSheetNum(9);
            params.setNeedSave(true);

            for (MultipartFile file : files) {
                file.getContentType();
                List<?> part = ExcelImportUtil.importExcel(file.getInputStream(), annotation.requireClass(), params);
                result.addAll(part);
            }
            return result;
        }
        return null;
    }
}

配置解析器到springMvc

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 新增Excel的引數解析器到Spring MVC.
 *
 * @author Hohn
 */
@Configuration
public class ExcelConfigurationSupport extends WebMvcConfigurationSupport {
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        ExcelArgumentResolver defaultExcelHandler = new ExcelArgumentResolver();
        argumentResolvers.add(defaultExcelHandler);
    }
}