EXCEL解析:使用poi解析xlsx和xls后缀的excel文件

1.pom依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>4.1.1</version>
</dependency>
<!--处理2007 excel-->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>4.1.1</version>
</dependency>

2.创建一个excel文件:test.xlsx文件

在这里插入图片描述
然后将其放入src下面

3.开始查找可以解析的类

查看apache的简介
在这里插入图片描述
发现了这个Workbook这个类,查看它的子类

在这里插入图片描述

发现可以使用的又两个一个是HSSFWorkbook(操作Excel2003以前(包括2003)的版本,扩展名是.xls)XSSFWorkbook(操作Excel2007的版本,扩展名是.xlsx)

4.开始使用

由于当前的操作的excel的版本和工具有两个所以需要通过扩展名查看,获取对应解析的类型的实例

// 通过当前的文件类型加载所诉要的模板
	// xlsx需要使用XSSFWorkbook
	// xls需要使用HSSFWorkbook
	public static Workbook getWorkBookByEngine(String filePath) throws Exception {
		int indexOf = filePath.indexOf(".");
		String type = filePath.substring(indexOf + 1, filePath.length());
		Workbook workbook = null;
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
		try {
			switch (type) {
			case "xlsx":
				workbook = new XSSFWorkbook(is);
				System.out.println("操作Excel2007的版本,扩展名是.xlsx");
				break;
			case "xls":
				workbook = new HSSFWorkbook(is);
				System.out.println("操作Excel2003以前(包括2003)的版本,扩展名是.xls");
				break;
			default:
				throw new IllegalArgumentException("不能解析的文件类型!");
			}

		} catch (Exception e) {
			closeWorkBook(workbook);
			closeInputStream(is);
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			closeInputStream(is);
		}

		return workbook;
	}

	public static void closeInputStream(InputStream is) {
		if (is != null)
			try {
				is.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
	}

	public static void closeWorkBook(Workbook workBook) {
		if (workBook != null)
			try {
				workBook.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
	}

创建读取数据的方法

public static void readExcelFile() {
		Workbook workbook = null;
		try {
			workbook=MyExcelUtils.getWorkBookByEngine("test.xlsx");
			Iterator<Sheet> iterator = workbook.iterator();
			while (iterator.hasNext()) {

				Sheet sheet = iterator.next();
				//先获取当前的最大行号(就是当前的数据行数减+1?)
				int lasRowNum=sheet.getLastRowNum()+1;
				System.out.println(lasRowNum);
			
				//循环的方式获取当前的每一行
				for (int i = 0; i < lasRowNum; i++) {
					Row row = sheet.getRow(i);
					
					//获取当前的最后一列的数量
					short lastCellNum = row.getLastCellNum();
					for (int j = 0; j <lastCellNum; j++) {
						//获取每一个单元
						Cell cell = row.getCell(j);
						//获取当前这个单元格的数据类型
						CellType cellType = cell.getCellType();
						
						if(cellType==CellType.NUMERIC) {
							System.out.print(cell.getNumericCellValue());
						}else if(cellType==CellType.BOOLEAN) {
							System.out.print(cell.getBooleanCellValue());
						}else {
							System.out.println(cell.getStringCellValue());
						}
						System.out.print(" ");	
					}
					System.out.println();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (workbook != null)
					workbook.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

1.通过解析发现了一些问题在获取当前行的行数的时候需要使用getLastRowNum获取最大的行数(这个数还需要+1才是正确的)

2.而当前的获取的单元格的行数为getLastCellNum方法(这个不需要+1)

3.在获取单元格的数据的时候需要注意获取的类型,如果类型不匹配的话就会报错

5.结果

在这里插入图片描述

读取成功!

6.总结

1.从当前获取的数据可以看到poi将excel分为:Sheet、Row、Cell,其中一个Sheet种可以有多个Row,一个Row种可以有多个Cell

2.需要注意当前的Cell获取的数据类型,不匹配会报错

3.需要注意使用的excel的版本,错误的版本也会操作失败

以上纯属个人见解,如有问题请联系本人!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值