springboot-poi --- Package annotations import and export of formula

This demo package is based on the object type annotation poi importing and exporting the project framework springboot items!

Simply explain knowledge related to this demo, hoping to bring convenience to give beginners!

  1. poi-excel basic operations (Tool)
  2. Custom annotation of use
  3. Global exception caught definition
  4. jkd1.8 new features: Lambda expressions, functions, interfaces, etc.
  5. A unified reference packaging
  6. chilli ...
  7. More suitable for beginners ~

This is not the definition of complex excel excel template, can only apply to the most simple import and export excel, do not meet the requirements, change itself -
roughly speaking about the excel tool provided it!

Export:

    /**
     * 导出模版
     *
     * @param excelName excel 名称
     * @param clazz     数据集
     * @param response  使用response可以导出到浏览器
     * @param <T>
     * @return
     */
    public static <T> Boolean exportTemplate(String excelName, Class<T> clazz, HttpServletResponse response)

    /**
     * 导出模版
     *
     * @param excelName excel 名称
     * @param clazz     数据集
     * @param type      excel 类型
     * @param response  使用response可以导出到浏览器
     * @param <T>
     * @return
     */
    public static <T> Boolean exportTemplate(String excelName, Class<T> clazz, Type type, HttpServletResponse response)

    /**
     * excel 导出 (对象)
     *
     * @param excelName excel 名称
     * @param list      数据集
     * @param clazz     反射clazz
     * @param response  使用response可以导出到浏览器
     * @param <T>
     * @return
     */
    public static <T> Boolean exportExcel(String excelName, List<T> list, Class<T> clazz, HttpServletResponse response)

    /**
     * excel 导出 (对象)
     *
     * @param excelName excel 名称
     * @param list      数据集
     * @param clazz     反射clazz
     * @param type      excel 类型
     * @param response  使用response可以导出到浏览器
     * @param <T>
     * @return
     */
    public static <T> Boolean exportExcel(String excelName, List<T> list, Class<T> clazz, Type type, HttpServletResponse response)

    /**
     * excel 导出 (Map)
     *
     * @param excelName excel 名称
     * @param clazz     反射clazz
     * @param list      数据集
     * @param response  使用response可以导出到浏览器
     * @param <T>
     * @return
     */
    public static <T> Boolean exportExcel(String excelName, Class<T> clazz, List<Map<String, Object>> list, HttpServletResponse response)

    /**
     * excel 导出 (Map)
     *
     * @param excelName excel 名称
     * @param clazz
     * @param list      数据集
     * @param type      excel 类型
     * @param response  使用response可以导出到浏览器
     * @param <T>
     * @return
     */
    public static <T> Boolean exportExcel(String excelName, Class<T> clazz, List<Map<String, Object>> list, Type type, HttpServletResponse response)

Import:

    /**
     * 传入文本对象输出list集合(导入)
     *
     * @param file  流文件
     * @param clazz 要转义成的类对象
     * @return
     */
    public static <T> List<T> importExcel(MultipartFile file, Class<T> clazz)

excel annotation class, provides a few simple determination process:

/**
 * <p>
 * excel 注解
 * </p>
 *
 * @author <a href="mailto:[email protected]">xiaoyang</a>
 * @version V0.0.1
 * @date 2019年09月10日
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Excel {

    /**
     * 表头中文
     *
     * @return
     */
    String titleName();

    /**
     * 列宽
     *
     * @return
     */
    int titleSize() default 30;

    /**
     * 字段顺序  正序
     *
     * @return
     */
    int orderNum();

    /**
     * 是否允许空值 ,默认不允许
     * <p>
     * false:不允许   true :允许
     *
     * @return
     */
    boolean empty() default false;

    /**
     * 内部类
     *
     * @return
     */
    CellType type() default @CellType;

    /**
     * 设置格式
     * 默认:
     * 时间:yyyy-MM-dd HH:mm:ss
     * 小数点:两位,四舍五入
     *
     * @return
     */
    @interface CellType {

        TimeType timeType() default TimeType.TIMEF_FORMAT;

        DecimalType decimalType() default DecimalType.two;
    }
}

TimeType tools:

/**
 * <p>
 * 日期格式
 * </p>
 *
 * @author <a href="mailto:[email protected]">xiaoyang</a>
 * @version V0.0.1
 * @date 2019年09月20日
 */
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年MM月dd日
     */
    ZHCN_DATE_FORMAT("yyyy年MM月dd日"),
    /**
     * yyyy年MM月dd日HH时mm分ss秒
     */
    ZHCN_TIME_FORMAT("yyyy年MM月dd日HH时mm分ss秒"),
    /**
     * yyyy年MM月dd日HH时mm分ss秒SSS毫秒
     */
    ZHCN_MSEL_FORMAT("yyyy年MM月dd日HH时mm分ss秒SSS毫秒"),
    /**
     * 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");

    /**
     * 日期格式
     */
    private String timeType;

    /**
     * 日期格式
     *
     * @param timeType
     */
    TimeType(String timeType) {
        this.timeType = timeType;
    }

    /**
     * 获取日期格式
     *
     * @return
     */
    public String getTimeType() {
        return timeType;
    }
}

DecimalType tools:

/**
 * <p>
 * 小数点格式
 * </p>
 *
 * @author <a href="mailto:[email protected]">xiaoyang</a>
 * @version V0.0.1
 * @date 2019年09月20日
 */
public enum DecimalType {

    /**
     * 一位
     */
    one(1, "0.0"),
    /**
     * 两位
     */
    two(2, "0.00"),
    /**
     * 三位
     */
    three(3, "0.000"),
    /**
     * 四位
     */
    four(4, "0.0000"),
    /**
     * 五位
     */
    five(5, "0.00000");

    /**
     * 日期格式
     */
    private String decimal;

    private int scale;

    /**
     * 日期格式
     *
     * @param scale
     * @param decimal
     */
    DecimalType(int scale, String decimal) {
        this.scale = scale;
        this.decimal = decimal;
    }

    /**
     * 获取日期格式
     *
     * @return
     */
    public String getDecimal() {
        return decimal;
    }

    /**
     * 获取日期格式
     *
     * @return
     */
    public int getScale() {
        return scale;
    }
}

  Attach GitHub address: https: //github.com/yangqiyue/excel

Guess you like

Origin www.cnblogs.com/yangyanrui/p/11591925.html