1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > EXCEL解析:使用poi解析xlsx和xls后缀的excel文件

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

时间:2021-09-04 00:15:12

相关推荐

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

1.pom依赖

<!-- /artifact/org.apache.poi/poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.1</version></dependency><!--处理 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(操作Excel以前(包括)的版本,扩展名是.xls)XSSFWorkbook(操作Excel的版本,扩展名是.xlsx)

4.开始使用

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

// 通过当前的文件类型加载所诉要的模板// xlsx需要使用XSSFWorkbook// xls需要使用HSSFWorkbookpublic 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("操作Excel的版本,扩展名是.xlsx");break;case "xls":workbook = new HSSFWorkbook(is);System.out.println("操作Excel以前(包括)的版本,扩展名是.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的版本,错误的版本也会操作失败

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。