Apache POI导入Excel,以及PinYin4j的使用_02

核心API
 HSSFWorkbook : 提供读写Microsoft Excel XLS格式档案的功能。
 XSSFWorkbook : 提供读写Microsoft Excel XLSX格式档案的功能。

坐标

<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>${poi.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>${poi.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml-schemas</artifactId>
			<version>${poi.version}</version>
		</dependency>

java代码

public String importXLS() {
		// 用来保存数据的集合
		ArrayList<Area> list = new ArrayList<>();

		try {
			HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
			// 读取工作簿
			HSSFSheet sheet = workbook.getSheetAt(0);
			// 遍历行
			for (Row row : sheet) {
				// 第一行是标题,这一行的数据不要
				if (row.getRowNum() == 0) {
					continue;
				}
				// 读取表格的数据
				String province = row.getCell(1).getStringCellValue();
				String city = row.getCell(2).getStringCellValue();
				String district = row.getCell(3).getStringCellValue();
				String postcode = row.getCell(4).getStringCellValue();

				// 截掉省市区的最后一个字符
				province = province.substring(0, province.length() - 1);
				city = city.substring(0, city.length() - 1);
				district = district.substring(0, district.length() - 1);
				// 获取城市编码
				String citycode = PinYin4jUtils.hanziToPinyin(city, "").toUpperCase();
				String[] headByString = PinYin4jUtils.getHeadByString(province + city + district, true);
				// 获取简码
				String shortcode = PinYin4jUtils.stringArrayToString(headByString);
				// 构造一个Area
				Area area = new Area();
				area.setProvince(province);
				area.setCity(city);
				area.setDistrict(district);
				area.setPostcode(postcode);
				area.setCitycode(citycode);
				area.setShortcode(shortcode);
				// 添加到集合
				list.add(area);
			}
			// 一次性保存区域数据
			areaService.save(list);
			// 释放资源
			workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}

PinYin4J的使用

坐标

	<dependency>
			<groupId>com.belerweb</groupId>
			<artifactId>pinyin4j</artifactId>
			<version>2.5.0</version>
		</dependency>

javaDemo

public static void main(String[] args) {
		String province = "广东省";
		String city = "深圳市";
		String district = "宝安区";

		// 简码 GDSZBA 城市编码 SHENZHEN

		province = province.substring(0, province.length() - 1);
		city = city.substring(0, city.length() - 1);
		district = district.substring(0, district.length() - 1);

//		String hanziToPinyin = PinYin4jUtils.hanziToPinyin(city, "").toUpperCase();
//		System.out.println(hanziToPinyin);
		
		String[] headByString = PinYin4jUtils.getHeadByString(province+city+district, true);
		
		String stringArrayToString = PinYin4jUtils.stringArrayToString(headByString);
		System.out.println(stringArrayToString);

	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值