使用hutool导入excel

导入依赖

<!--hutool-->
<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>5.7.20</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>4.1.2</version>
</dependency>

代码

/**
 * 导入购房者名单
 *
 * @param file            购房者名单excel文件
 * @param directSeedingId 直播信息主键
 * @param userId          用户id
 * @return
 */
public boolean importBuyerExcel(MultipartFile file, String directSeedingId, String userId) {
	if (file.isEmpty()) {
		throw new RuntimeException("上传文件为空,请重新上传!");
	}
	boolean result = false;
	// 1.获取上传文件输入流
	try (InputStream inputStream = file.getInputStream()) {
		// 调用用 hutool 方法读取数据 默认调用第一个sheet
		ExcelReader excelReader = ExcelUtil.getReader(inputStream);

		//忽略第一行头(第一行是中文的情况),直接读取表的内容
		List<List<Object>> list = excelReader.read(1);
		List<BuyerInfo> listUser = CollUtil.newArrayList();
		for (List<Object> row : list) {
			BuyerInfo buyerInfo = new BuyerInfo();
			//获取excel第二行指定列的数据进行填充
			buyerInfo.setBuyerNumber(row.get(0).toString());
			buyerInfo.setBuyerName(row.get(1).toString());
			buyerInfo.setBuyerIdCard(row.get(2).toString());
			buyerInfo.setBuyHouseCertNo(row.get(3).toString());
			buyerInfo.setDirectSeedingId(directSeedingId);
			buyerInfo.setCreateDate(LocalDateTime.now());
			buyerInfo.setUpdateDate(LocalDateTime.now());
			buyerInfo.setCreateUserId(userId);
			buyerInfo.setUpdateUserId(userId);
			listUser.add(buyerInfo);
		}

		//存数据库
		result = buyerInfoService.saveBatch(listUser);

	} catch (Exception e) {
		throw new RuntimeException("导入购房者名单出错:" + e.getMessage());
	}

	return result;
}

posted on   何苦->  阅读(696)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示