poi 解析中文_Java解析Excel之POI(二):解决 xls 和 xlsx 两种格式解析时的差异统一处理...

这篇博客介绍了如何使用Apache POI的WorkbookFactory简化Excel(.xls和.xlsx)文件的解析过程,避免了根据不同文件格式编写不同解析方法的需求。通过WorkbookFactory可以从InputStream直接创建Workbook对象,自动处理不同格式的文件。解析代码示例中展示了读取Excel内容并存储到Word对象列表的过程。
摘要由CSDN通过智能技术生成

初学 POI 解析 Excel 时,总是写两种方法来分别处理 .xls 和 .xlsx 格式。方法调用前还要进行文件后缀的判断。

POI 提供了 HSSFWorkbook 和 XSSFWorkbook 两种实现,来分别处理 .xls 和 .xlsx 格式的 Excel 文件。解析时,可以根据判断 Excel 文件的后缀,来进行调用相应的解析方法。

此方法虽然可行,但代码却有些繁琐,不够简洁。POI 提供的 WorkbookFactory 类可以自动根据文件类型,来决定调用对应的 HSSFWorkbookFactory 或 XSSFWorkbookFactory 来处理文件。

大致解析流程和之前实现的解析代码基本相同,只有些许区别。具体代码如下:

1.引入 jar 包

org.apache.poi

poi-ooxml

4.0.1

引入 poi-ooxml 时,会自动引入其相关依赖(包括 poi,其用于解析 .xls)。

2.关键代码

2.1.具体解析实现

public static Listparser(InputStream inputStream) {

List list = new ArrayList();

Workbook workbook= null;try{

workbook=WorkbookFactory.create(inputStream);

Sheet sheet= workbook.getSheetAt(0);for(Row row : sheet) {if (row.getRowNum() == 0)continue;

String danci= row.getCell(0).getStringCellValue();

String duyin= row.getCell(0).getStringCellValue();

String yisi= row.getCell(0).getStringCellValue();

String liju= row.getCell(0).getStringCellValue();

Word word= newWord();

word.setDanci(danci);

word.setDuyin(duyin);

word.setYisi(yisi);

word.setLiju(liju);

list.add(word);

}

}catch(EncryptedDocumentException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{if (workbook != null) {

workbook.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}returnlist;

}

2.2.解析文件结构

2.2.1.实体Bean

public class Word {

private String danci;

private String duyin;

private String yisi;

private String liju;

// Getter and Setter ...

// toString()

}

2.2.2.Excel文件

b22c343d6ae33f52cb37cbe258cd8861.png

2.3.Test测试

public static voidmain(String[] args) {

File fileXLS= new File("C:\\Book1.xls");

File fileXLSX= new File("C:\\Book1.xlsx");try{

InputStream inputStream= newFileInputStream(fileXLS);

List words =ExcelParser.parser(inputStream);

System.out.println(words.toString());

System.out.println("\n-----------------------------------------\n");

inputStream= newFileInputStream(fileXLSX);

words=ExcelParser.parser(inputStream);

System.out.println(words.toString());

}catch(FileNotFoundException e) {

e.printStackTrace();

}

}

2.4.结果

66b413f303daae4dddadcc05580e9a23.png

【注意点】

1.当获取单元格内容的时候,需要使用对应数据类型 getXxxCellValue() 方法,否则会报数据类型错误。

【关于疑问】

1.关于输入流的创建,创建一个 FileInputStream 后,用完是不是需要手动 close() ?

你可以使用Apache POI库来解析Excel文件。下面是一个使用Java解析Excel文件的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelParser { public static void main(String[] args) { String filePath = "path/to/your/excel/file.xlsx"; // 替换为你的Excel文件路径 try { FileInputStream fis = new FileInputStream(new File(filePath)); Workbook workbook; if (filePath.endsWith(".xlsx")) { workbook = new XSSFWorkbook(fis); // 处理.xlsx文件 } else if (filePath.endsWith(".xls")) { workbook = new HSSFWorkbook(fis); // 处理.xls文件 } else { throw new IllegalArgumentException("The specified file is not Excel file"); } Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 for (Row row : sheet) { for (Cell cell : row) { CellType cellType = cell.getCellType(); if (cellType == CellType.STRING) { System.out.print(cell.getStringCellValue() + " "); } else if (cellType == CellType.NUMERIC) { System.out.print(cell.getNumericCellValue() + " "); } else if (cellType == CellType.BOOLEAN) { System.out.print(cell.getBooleanCellValue() + " "); } } System.out.println(); } workbook.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 请将代码中的`"path/to/your/excel/file.xlsx"`替换为你实际的Excel文件路径。该代码会打开Excel文件并输出每个单元格的值。你可以根据需要对解析的内容进行进一步处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值