EXCEL转java对象

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.14</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.4</version>
</dependency>

excel数据:

姓名性别年龄出身年月
张三121991/2/6
李四131991/5/6

Java对象

package com.example.bootredis.pojo;

import java.io.Serializable;

public class User implements Serializable  {
    private String name;
    private String age;
    private String sex;
    private String birth;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                ", birth='" + birth + '\'' +
                '}';
    }
}

转换exce文件转换成User对象代码

package com.example.bootredis.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.bootredis.pojo.User;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelImport {
    public static void main(String args[]) {
        ExcelImport excelImport = new ExcelImport();

        try {
            excelImport.importExcelAction();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //导入Excel数据
    public void importExcelAction() throws Exception {
        //文件路径

        String filePath = "D:\\test.xlsx";

        XSSFWorkbook wookbook = new XSSFWorkbook(new FileInputStream(filePath));

        XSSFSheet sheet = wookbook.getSheet("Sheet1");

        //获取到Excel文件中的所有行数

        int rows = sheet.getPhysicalNumberOfRows();

        //遍历行

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        for (int i = 1; i < rows; i++) {
            // 读取左上端单元格

            XSSFRow row = sheet.getRow(i);

            // 行不为空

            if (row != null) {
                Map<String, Object> map = new HashMap<String, Object>();

                //获取到Excel文件中的所有的列

                int cells = row.getPhysicalNumberOfCells();

                //姓名

                XSSFCell nameCell = row.getCell(1);

                String name = getValue(nameCell);

                //性别

                XSSFCell sexCell = row.getCell(2);

                String sex = getValue(sexCell);

                //年龄

                XSSFCell ageCell = row.getCell(3);

                String age = getValue(ageCell);

                //出生年月

                XSSFCell birthCell = row.getCell(4);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String birth = simpleDateFormat.format(birthCell.getDateCellValue());

                map.put("name", name);

                map.put("sex", sex);

                map.put("age", age);

                map.put("birth", birth);

                list.add(map);

            }

        }
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(list);
        List<User> users = jsonArray.toJavaList(User.class);

        System.out.println("list = " + users.toString());

    }

    private String getValue(XSSFCell xSSFCell) {
        if (null == xSSFCell) {
            return "";

        }

        if (xSSFCell.getCellType() == xSSFCell.CELL_TYPE_BOOLEAN) {

            // 返回布尔类型的值

            return String.valueOf(xSSFCell.getBooleanCellValue());

        } else if (xSSFCell.getCellType() == xSSFCell.CELL_TYPE_NUMERIC) {

            // 返回数值类型的值

            return String.valueOf(xSSFCell.getNumericCellValue());

        } else {

            // 返回字符串类型的值

            return String.valueOf(xSSFCell.getStringCellValue());

        }

    }

}

 输出:

list = [User{name='张三', age='12.0', sex='男', birth='1991-02-06'}, User{name='李四', age='13.0', sex='男', birth='1991-05-06'}]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值