Java通过poi将Excel转成实体类(Entity)【可使用avue的tableOption】

该文章介绍了一个Java工具类ExcelUtils,用于将Excel文件中的数据按指定格式解析并转换为实体类列表,通过字段选项映射实现数据绑定。
摘要由CSDN通过智能技术生成

 工具类


@Component
public class ExcelUtils<E> {
	private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	/**
	 *
	 * @param tClass 文件要转化的实体类的class
	 * @param file	文件数据
	 * @param column columnOption(实体类属性按照excel顺序排列的json字符串)
     * 例如:[{"type":"input","label":"名字","prop":"name"},{"type":"input","label":"性别","prop":"age"},{"type":"input","label":"住址","prop":"address"},]
	 * @return 实体类的list
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IOException
	 * @throws NoSuchFieldException
	 * @throws ParseException
	 */
	public List<E> excelToEntityList(Class<E> tClass, MultipartFile file, String column) throws InstantiationException, IllegalAccessException, IOException, NoSuchFieldException, ParseException {
		List<RequestVo> fields = stringToRequestVo(column);
		ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
		List<E> objects = new ArrayList<>();
		int lastRowNum = reader.getSheet().getLastRowNum()+1;
		for (int i = 1; i < lastRowNum; i++) {
			List<Object> values = reader.readRow(i);
			if (values.size()==0){
				continue;
			}
			E instance = tClass.newInstance();
			while (values.size() < fields.size()) {
				values.add("");
			}
			for (int j = 0; j < fields.size(); j++) {
				RequestVo requestVo = fields.get(j);
				String value = values.get(j) == null ? "" : values.get(j).toString();
				Field declaredField = tClass.getDeclaredField(requestVo.getProp());
				declaredField.setAccessible(true);
				if (StrUtil.isNotEmpty(value)) {
					if (declaredField.getType().getName().equals("java.util.Date")) {
						declaredField.set(instance, simpleDateFormat.parse(value));
					} else if (declaredField.getType().getName().equals("java.lang.Integer")) {
						declaredField.set(instance, Integer.valueOf(value));
					} else if (declaredField.getType().getName().equals("java.lang.Double")) {
						declaredField.set(instance, Double.valueOf(value));
					} else if (declaredField.getType().getName().equals("java.lang.String")) {
						declaredField.set(instance, value);
					}
				}
			}
			objects.add(instance);
		}
		return objects;
	}

	private List<RequestVo> stringToRequestVo(String column) {
		List<JSONObject> columns = JSON.parseObject(column, ArrayList.class);
		return columns.stream().map(v -> {
			RequestVo requestVo = new RequestVo();
			requestVo.setProp((String) v.get("prop"));
			String label = (String) v.get("label");
			try {
				requestVo.setLabel(new String(label.getBytes("iso-8859-1"), "UTF-8"));
			} catch (Exception e) {
				e.printStackTrace();
			}
			requestVo.setType((String) v.get("type"));
			return requestVo;
		}).collect(Collectors.toList());
	}
}

根据例子对应的excel,字段的前后要一一对应

RequestVo.java

@Data
public class RequestVo {
	private String type;
	private String label;
	private String prop;
}

注意:columnOption中的prop对应的是实体类中的属性名称,columnOption可以直接从前端的tableOption中搞到,avue的tableOption

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值