EasyPoi implements Excel data import

        EasyPoi is a Java-based Excel import and export framework, which mainly provides basic functions such as Excel reading and writing, and supports defining the format of Excel files through annotations .

Add maven dependency

<dependencies>
        <!--基础模块,提供了Excel读取、写入等基本功能-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!--Web模块,提供了在Web应用中使用EasyPoi的相关功能-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!--注解模块,提供了一些用于Excel导入导出的注解,例如@Excel、@ExcelField等-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!--基于Hibernate的Java Bean验证框架,提供了一些常用的验证注解,例如@NotNull..-->
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.1.5.Final</version>
        </dependency>
        <!--javax.el提供了Java EL的实现,简化Java应用程序中表达式的语言-->
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.1-b11</version>
        </dependency>
    </dependencies>

Annotations define the format of the Excel file

//指定了生成Excel文件时的sheet名称
@ExcelTarget("EasyPoi")
public class UserBean {
/*指定了生成Excel文件时该属性对应的列名称,
即生成的Excel文件中会有“编号”、“用户名”、“生日”和“工资”四列。*/
    @Excel(name = "编号")
    private Integer id;
    @Excel(name = "用户名")
    private String name;
    @Excel(name = "生日")
    private String birthday;
    @Excel(name = "工资")
    private Integer money;
}

export 

@RestController
@RequestMapping("user")   
public class UserController {
   @RequestMapping("uploadExcel")
    public String upload(MultipartFile excel) throws Exception {
        //初始化设置
        ImportParams params = new ImportParams();
          //设置 读取第几个选项卡
          params.setSheetNum(1);
          //设置表头 占几行
          params.setHeadRows(1);
          params.setTitleRows(1);
        //将上传Excel数据  封装成用户集合
        //第一个参数为:上传文件的读取流
        //第二个参数为:要封装实体对象的类模板
        //第三个参数为:初始化对象
        List<UserBean> list = ExcelImportUtil.importExcel(
                excel.getInputStream(),UserBean.class,params);
        System.out.println(list);
        return "ok";
    }
}

        MultipartFile is an interface in the Spring framework for handling file uploads in HTTP multipart requests. When the client uploads a file, the server can obtain the uploaded file content, file name, file type and other information through the MultipartFile interface, and can save the file to the local disk or other storage media.

getName():获取上传文件的参数名称。

getOriginalFilename():获取上传文件的原始文件名。

getContentType():获取上传文件的类型。

getSize():获取上传文件的大小。

getBytes();获取上传文件的字节数组。

getInputStream();获取上传文件的InputStream流。

transferTo():将上传文件保存到指定目录中。
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="axios.min.js"></script>
    <script src="util.js"></script>
    <script>
        function uploadExcel() {
            //使用FormData对象来创建表单数据
            let formObj = new FormData;
            //将上传的Excel文件添加到表单数据中
            //第一个参数:将获取到的文件添加到名为excel的FormData对象中
            //第二个参数:获取了id为excelFile的文件上传控件
            //第三个参数:获取了文件上传控件中选择的文件列表中的第一个文件
            formObj.append("excel",$("excelFile").files[0]);
            let config={
                headers: {'Content-Type': 'multipart/form-data' }
            }
            axios.get("/user/uploadExcel",formObj,config).then(resp=>{
                alert(resp.data);
            })
        }
    </script>
</head>
<body>
请选择Excel:<input type="file" id="excelFile"><br>
<input type="button" value="上传" onclick="uploadExcel()">
</body>

Note that if you want to upload multiple files, you can use a loop in the append method to add them to the FormData object one by one.

Guess you like

Origin blog.csdn.net/m0_74421344/article/details/130611956