轻松掌握Java读取Excel文件:POI技术实战全解析

引言
Excel文件在日常工作和数据管理中扮演着重要角色。Java作为一门强大的编程语言,可以轻松地操作Excel文件。Apache POI是Java操作Microsoft Office格式文件(包括Excel)的强大工具。本文将详细解析如何使用POI技术轻松读取Excel文件。
1. POI简介
Apache POI是Apache软件基金会的一个开源项目,它提供了Java API来操作Microsoft Office文档。POI支持多种文件格式,包括xls和xlsx。
1.1 POI的主要组件
HSSF
:用于操作Excel 97-2003(.xls)格式文件。
XSSF
:用于操作Excel 2007及以上版本(.xlsx)格式文件。
SXSSF
:用于操作大型Excel 2007及以上版本(.xlsx)格式文件,它通过流的方式写入文件,以减少内存消耗。
2. 环境搭建
要在Java项目中使用POI,首先需要在项目中添加POI依赖。以下是在Maven项目中添加POI依赖的示例:


org.apache.poi
poi-ooxml
5.2.2


3. 读取Excel文件
以下是使用POI读取Excel文件的步骤:
3.1 创建文件输入流
FileInputStream fis = new FileInputStream(“path/to/your/excel/file.xlsx”);
3.2 创建工作簿对象
Workbook workbook = WorkbookFactory.create(fis);
3.3 获取工作表
Sheet sheet = workbook.getSheetAt(0);
3.4 遍历行和单元格
Row row;
for (int i = 0; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); if (row != null) { for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); // 获取单元格数据 String cellValue = getCellValue(cell); System.out.print(cellValue + "\t"); } System.out.println(); } } 3.5 获取单元格数据 public static String getCellValue(Cell cell) { if (cell == null) { return ""; } switch (cell.getCellType()) { case STRING: return cell.getStringCellValue(); case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue()); } else { return String.valueOf(cell.getNumericCellValue()); } case BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case FORMULA: return cell.getCellFormula(); default: return ""; } } 4. 读取特定单元格 如果需要读取特定单元格的数据,可以使用以下方法: Cell cell = sheet.getRow(1).getCell(2); String cellValue = getCellValue(cell); System.out.println(cellValue); 5. 总结 使用POI技术读取Excel文件是一种简单而有效的方法。通过以上步骤,你可以轻松地读取Excel文件中的数据,并将其用于你的Java应用程序中。 6. 示例代码 以下是完整的示例代码,演示如何使用POI读取Excel文件: import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.IOException; public class ReadExcelExample { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("path/to/your/excel/file.xlsx"); Workbook workbook = new XSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); Row row; for (int i = 0; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); if (row != null) { for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); String cellValue = getCellValue(cell); System.out.print(cellValue + "\t"); } System.out.println(); } } fis.close(); } catch (IOException | InvalidFormatException e) { e.printStackTrace(); } } public static String getCellValue(Cell cell) { if (cell == null) { return ""; } switch (cell.getCellType()) { case STRING: return cell.getStringCellValue(); case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue()); } else { return String.valueOf(cell.getNumericCellValue()); } case BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case FORMULA: return cell.getCellFormula(); default: return ""; } } } 通过以上步骤和代码示例,你可以轻松地使用Java和POI技术读取Excel文件。