Apache POI 一键上传(导入excel文件到数据库)

本文介绍了一种使用Java POI库解析Excel文件的方法,并通过PinYin4j库实现地名拼音转换及拼音首字母提取的功能。具体展示了如何读取Excel表格中的区域信息并进行必要的数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import cn.XXXX.bos.utils.PinYin4jUtils;

import org.apache.commons.lang3.StringUtils;

 

// HSSF:操作07版本之前 后缀名xxx.xls

// XSSF:操作07版本之后 后缀名xx.xlsx




@Action("areaAction_uploadFile") public String areaAction_uploadFile() throws Exception { List<Area> list = new ArrayList<>(); //使用POI技术解析excel文件 HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(xlsFile)); HSSFSheet sheet = workbook.getSheet("Sheet1"); for (Row row : sheet) { if(row.getRowNum()==0){ continue; } String id = row.getCell(0).getStringCellValue(); String province = row.getCell(1).getStringCellValue(); String city = row.getCell(2).getStringCellValue(); String district = row.getCell(3).getStringCellValue(); String postcode = row.getCell(4).getStringCellValue(); Area area = new Area(id, province, city, district, postcode, null, null); province = province.substring(0, province.length()-1); city = city.substring(0, city.length()-1); district = district.substring(0, district.length()-1); String info = province+city+district; System.out.println(info); // 简码: HBSJZQX String[] headByString = PinYin4jUtils.getHeadByString(info); String shortcode = StringUtils.join(headByString, ""); area.setShortcode(shortcode); // 城市编码:shijiazhuang String citycode = PinYin4jUtils.hanziToPinyin(city, ""); area.setCitycode(citycode); list.add(area); } areaService.save(list); return NONE; }

 

转载于:https://www.cnblogs.com/javaxiaoxin/p/7361274.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值