栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring自定义注解Excel文档应用场景

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring自定义注解Excel文档应用场景

我们经常需要导入一些列表数据,那怎样用注解去实现呢?

 

一、新建@Excel注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Excel
{
    
    public String name() default "未命名";

}
二、@Excel的使用

在需要导入或者导入的实体类的字段上标注上@Excel注解,如:

package com.example.springboot4.entity;

import com.example.springboot4.annotation.Excel;


public class ConsumptionEntity {
    @Excel(name="物料")
    public String material;
    @Excel(name="组织")
    public String org;
    @Excel(name="数量")
    public String qty;
    @Excel(name="金额")
    public String amount;

    public ConsumptionEntity() {
    }
    public ConsumptionEntity(String material, String org, String qty, String amount) {
        this.material = material;
        this.org = org;
        this.qty = qty;
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "ConsumptionEntity{" +
                "material='" + material + ''' +
                ", org='" + org + ''' +
                ", qty='" + qty + ''' +
                ", amount='" + amount + ''' +
                '}';
    }

    public String getMaterial() {
        return material;
    }

    public void setMaterial(String material) {
        this.material = material;
    }

    public String getOrg() {
        return org;
    }

    public void setOrg(String org) {
        this.org = org;
    }

    public String getQty() {
        return qty;
    }

    public void setQty(String qty) {
        this.qty = qty;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }
}
三、@Excel的解析

咱一步一步来,先写个测试类。

 @Test
    public void testExcel() {

        ConsumptionEntity consumptionEntity=new ConsumptionEntity("钳子","Org-011","100","200.00");
        ConsumptionEntity consumptionEntity1=new ConsumptionEntity("剪刀","Org-012","20","200.00");
        //模拟查询到的对象集合
        List list = new ArrayList<>();
        list.add(consumptionEntity);
        list.add(consumptionEntity1);

        //得到ConsumptionEntity对象所有声明的字段
        Field[] heads = list.get(0).getClass().getDeclaredFields();
        for (Field field : heads){
            //查找被Execl注解到的字段
            if (field.isAnnotationPresent(Excel.class)){
                //得到该字段的注解
                Excel attr = field.getAnnotation(Excel.class);
                //得到注解的name属性
                System.out.print(attr.name()+",");
            }
        }
        System.out.println("");
        for (ConsumptionEntity entity : list) {
            System.out.print(entity.getMaterial()+",");
            System.out.print(entity.getOrg()+",");
            System.out.print(entity.getQty()+",");
            System.out.print(entity.getAmount());
            System.out.println("");
        }
    }

运行出来的效果

到这里已经成功了一半了。 

四、整合poi包,去生成Excel文件。

请转载到springboot文件上传+下载+解析“xls“文件_金秋0707的博客-CSDN博客4.1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785570.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号