poi导入excel转化成实体类

1、Controller

//导入excel
	@RequestMapping(value = "/import", method=RequestMethod.POST)
	public JsonDto importExcel(@RequestParam(value="file",required = false) MultipartFile file,
							   HttpServletRequest request,
							   HttpServletResponse response){
		return  functionProjectRelationService.importExcelFile(file);
	}

2、Service

public JsonDto importExcelFile(MultipartFile file) {
		JsonDto jsonDto = new JsonDto();
		try{
			int successNum = 0;
			int failureNum = 0;
			ImportExcel ei = new ImportExcel(file, 0, 0);
			List<User> list = ei.getDataList(User.class);
			logger.info("导入" + list.size() + "条数据");
			jsonDto.setSuccess(Boolean.TRUE);
			jsonDto.setErrcode("200");
			jsonDto.setErrmsg("上传成功");
			jsonDto.setData(list);
		}catch(Exception e){
			e.printStackTrace();
			jsonDto.setSuccess(Boolean.TRUE);
			jsonDto.setErrcode("-1");
			jsonDto.setErrmsg("上传异常"+e.getMessage());
			jsonDto.setData(new JSONObject());
			return jsonDto;
		}
		return  jsonDto;
	}

3、User.java

package com.elink.estos.common.utils.excel;

import com.elink.estos.common.utils.excel.annotation.ExcelField;

/**
 * @Auther: yinzuomei
 * @Date: 2019/9/3 13:51
 * @Description:
 */
public class User {
    private String name;
    private String sex;
    private String age;
    private String address;
    @ExcelField(title="姓名", align=1,sort = 1)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @ExcelField(title="性别", align=1,sort = 2)
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    @ExcelField(title="年龄", align=1,sort = 3)
    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
    @ExcelField(title="地址", align=1,sort = 4)
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

4、工具类修改
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要引入POI的相关依赖包,并且要导入相关的类: ```java import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; ``` 然后,可以编写一个方法来读取Excel文件并将其转换为实体类: ```java public List<Entity> readExcel(File file) throws IOException { List<Entity> entities = new ArrayList<>(); FileInputStream inputStream = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); Entity entity = new Entity(); Cell cell1 = row.getCell(0); entity.setProperty1(cell1.getStringCellValue()); Cell cell2 = row.getCell(1); entity.setProperty2(cell2.getNumericCellValue()); Cell cell3 = row.getCell(2); entity.setProperty3(cell3.getStringCellValue()); entities.add(entity); } workbook.close(); inputStream.close(); return entities; } ``` 在这个方法中,我们首先打开Excel文件并获取第一个工作表。接下来,我们循环遍历每一行,并在每一行中获取每个单元格的值,并将其设置到实体类属性中。最后,我们将实体类添加到列表中并返回列表。 需要注意的是,在这个方法中,我们假设Excel文件的第一行是表头,因此我们从第二行开始循环遍历。如果你的Excel文件没有表头,那么你需要从第一行开始循环,并相应地调整代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值