springboot-poi -- Encapsulating Annotated Import and Export

Keywords: Java Excel Spring Lambda Lombok

This demo is based on poi encapsulated object annotation import and export, the project framework is spring boot project!

Simply explain the knowledge points involved in this demo, hoping to bring convenience to beginners!

  1. poi-excel basic operations (tools)
  2. Use of custom annotations
  3. Definition of global exception capture
  4. New features of JKD 1.8: Lambda expressions, functional interfaces, etc.
  5. Unified Packaging Class for Ginseng
  6. lombok ...

More suitable for beginners~

This excel is not able to define complex excel template. It can only be applied to the simplest import and export of excel. If it does not meet the requirements, please change it by yourself.~
Roughly speaking, excel tools are provided.

Export:

    /**
     * Export template
     *
     * @param excelName excel Name
     * @param clazz     data set
     * @param response  Export to browser using response
     * @param <T>
     * @return
     */
    public static <T> Boolean exportTemplate(String excelName, Class<T> clazz, HttpServletResponse response)

    /**
     * Export template
     *
     * @param excelName excel Name
     * @param clazz     data set
     * @param type      excel type
     * @param response  Export to browser using response
     * @param <T>
     * @return
     */
    public static <T> Boolean exportTemplate(String excelName, Class<T> clazz, Type type, HttpServletResponse response)

    /**
     * excel Export (object)
     *
     * @param excelName excel Name
     * @param list      data set
     * @param clazz     Reflection clazz
     * @param response  Export to browser using response
     * @param <T>
     * @return
     */
    public static <T> Boolean exportExcel(String excelName, List<T> list, Class<T> clazz, HttpServletResponse response)

    /**
     * excel Export (object)
     *
     * @param excelName excel Name
     * @param list      data set
     * @param clazz     Reflection clazz
     * @param type      excel type
     * @param response  Export to browser using response
     * @param <T>
     * @return
     */
    public static <T> Boolean exportExcel(String excelName, List<T> list, Class<T> clazz, Type type, HttpServletResponse response)

    /**
     * excel Export (Map)
     *
     * @param excelName excel Name
     * @param clazz     Reflection clazz
     * @param list      data set
     * @param response  Export to browser using response
     * @param <T>
     * @return
     */
    public static <T> Boolean exportExcel(String excelName, Class<T> clazz, List<Map<String, Object>> list, HttpServletResponse response)

    /**
     * excel Export (Map)
     *
     * @param excelName excel Name
     * @param clazz
     * @param list      data set
     * @param type      excel type
     * @param response  Export to browser using response
     * @param <T>
     * @return
     */
    public static <T> Boolean exportExcel(String excelName, Class<T> clazz, List<Map<String, Object>> list, Type type, HttpServletResponse response)

Import:

    /**
     * Input text object outputs list collection (import)
     *
     * @param file  Stream file
     * @param clazz Class objects to be escaped
     * @return
     */
    public static <T> List<T> importExcel(MultipartFile file, Class<T> clazz)

 

excel annotation class provides several simple judgment processing:

/**
 * <p>
 * excel annotation
 * </p>
 *
 * @author <a href="mailto:yangyanrui@yidianlife.com">xiaoyang</a>
 * @version V0.0.1
 * @date 2019 10 September 2000
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Excel {

    /**
     * Table header Chinese
     *
     * @return
     */
    String titleName();

    /**
     * Column width
     *
     * @return
     */
    int titleSize() default 30;

    /**
     * Positive field order
     *
     * @return
     */
    int orderNum();

    /**
     * Whether null values are allowed by default
     * <p>
     * false: No true: Yes
     *
     * @return
     */
    boolean empty() default false;

    /**
     * Inner class
     *
     * @return
     */
    CellType type() default @CellType;

    /**
     * Set format
     * Acquiescence:
     * Time: yyyy-MM-dd HH:mm:ss
     * Decimal point: two, rounded
     *
     * @return
     */
    @interface CellType {

        TimeType timeType() default TimeType.TIMEF_FORMAT;

        DecimalType decimalType() default DecimalType.two;
    }
}
TimeType Tool Class:
/**
 * <p>
 * Date format
 * </p>
 *
 * @author <a href="mailto:yangyanrui@yidianlife.com">xiaoyang</a>
 * @version V0.0.1
 * @date 2019 20 September 2000
 */
public enum TimeType {

    /**
     * yyyy-MM-dd
     */
    DATE_FORMAT("yyyy-MM-dd"),
    /**
     * yyyy-MM
     */
    YEAR_S_MONTH("yyyy-MM"),
    /**
     * yyyyMM
     */
    YEAR_MONTH("yyyyMM"),
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    TIMEF_FORMAT("yyyy-MM-dd HH:mm:ss"),
    /**
     * yyyy-MM-dd HH:mm:ss.SSS
     */
    MSEL_FORMAT("yyyy-MM-dd HH:mm:ss.SSS"),
    /**
     * yyyy Year MM Month dd Day
     */
    ZHCN_DATE_FORMAT("yyyy year MM month dd day"),
    /**
     * yyyy Mm min ss seconds at HH on dd day, MM Month, 2000
     */
    ZHCN_TIME_FORMAT("yyyy year MM month dd day HH Time mm branch ss second"),
    /**
     * yyyy MM Month dd HH MM min ss s SSS MS
     */
    ZHCN_MSEL_FORMAT("yyyy year MM month dd day HH Time mm branch ss second SSS Millisecond"),
    /**
     * yyyyMMdd
     */
    DATE_STR_FORMAT("yyyyMMdd"),
    /**
     * yyyyMMddHHmmss
     */
    TIME_STR_FORMAT("yyyyMMddHHmmss"),
    /**
     * yyyyMMddHHmmssSSS
     */
    MSEL_STR_FORMAT("yyyyMMddHHmmssSSS"),
    /**
     * yyyy-MM-dd HH:mm
     */
    MSEL_MIU_FORMAT("yyyy-MM-dd HH:mm"),
    /**
     * yyyyMMddHH
     */
    MS_MIU_FORMAT("yyyyMMddHH");

    /**
     * Date format
     */
    private String timeType;

    /**
     * Date format
     *
     * @param timeType
     */
    TimeType(String timeType) {
        this.timeType = timeType;
    }

    /**
     * Get date format
     *
     * @return
     */
    public String getTimeType() {
        return timeType;
    }


}

 

DecimalType Tool Class:
/**
 * <p>
 * Decimal point format
 * </p>
 *
 * @author <a href="mailto:yangyanrui@yidianlife.com">xiaoyang</a>
 * @version V0.0.1
 * @date 2019 20 September 2000
 */
public enum DecimalType {

    /**
     * One person
     */
    one(1, "0.0"),
    /**
     * Two place
     */
    two(2, "0.00"),
    /**
     * Three place
     */
    three(3, "0.000"),
    /**
     * Four place
     */
    four(4, "0.0000"),
    /**
     * Five place
     */
    five(5, "0.00000");

    /**
     * Date format
     */
    private String decimal;

    private int scale;

    /**
     * Date format
     *
     * @param scale
     * @param decimal
     */
    DecimalType(int scale, String decimal) {
        this.scale = scale;
        this.decimal = decimal;
    }

    /**
     * Get the date format
     *
     * @return
     */
    public String getDecimal() {
        return decimal;
    }

    /**
     * Get the date format
     *
     * @return
     */
    public int getScale() {
        return scale;
    }
}

Posted by biopv on Wed, 02 Oct 2019 04:14:29 -0700