Apache POI 是一个用于操作 Microsoft Office 文件的 Java 库。它支持读取、写入和修改这些文件,广泛应用于生成报表、数据处理等场景。主要以 Excel 文件为主
-
引入依赖
<dependencies> <!-- Excel 操作核心库 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> <!-- 版本号可以根据需要调整 --> </dependency> <!-- 如果需要处理旧版 Excel (.xls) 文件 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.3</version> </dependency> </dependencies>
-
Excel 文件操作
//创建 Excel 文件 public class CreateExcelExample { public static void main(String[] args) { // 在内存中创建一个 Excel 文件对象 XSSFWorkbook excel = new XSSFWorkbook(); // 创建 Sheet 页,名称为 "N" XSSFSheet sheet = excel.createSheet("N"); // 创建第1行(索引从0开始),并设置单元格值 XSSFRow row1 = sheet.createRow(0); // 第1行 row1.createCell(1).setCellValue("姓名"); // 第2个单元格(索引1) row1.createCell(2).setCellValue("城市"); // 第3个单元格(索引2) // 创建第2行,并设置单元格值 XSSFRow row2 = sheet.createRow(1); // 第2行 row2.createCell(1).setCellValue("张三"); // 第2个单元格 row2.createCell(2).setCellValue("北京"); // 第3个单元格 // 创建第3行,并设置单元格值 XSSFRow row3 = sheet.createRow(2); // 第3行 row3.createCell(1).setCellValue("李四"); // 第2个单元格 row3.createCell(2).setCellValue("上海"); // 第3个单元格 // 将 Excel 文件写入磁盘 try (FileOutputStream out = new FileOutputStream(new File("D:\\N.xlsx"))) { // 将内存中的 Excel 工作簿写入输出流 excel.write(out); System.out.println("Excel 文件已成功创建!路径:D:\\N.xlsx"); } catch (IOException e) { // 捕获并打印 IO 异常信息 System.err.println("写入 Excel 文件时发生错误!"); e.printStackTrace(); } finally { // 确保关闭工作簿以释放资源 try { if (excel != null) { excel.close(); } } catch (IOException e) { System.err.println("关闭 Excel 工作簿时发生错误!"); e.printStackTrace(); } } } }
执行结果:
//读取 Excel 文件 public class ReadExcelExample { public static void main(String[] args) { // 定义输入流对象 FileInputStream in = null; XSSFWorkbook excel = null; try { // 通过输入流读取指定的 Excel 文件 in = new FileInputStream(new File("D:\\N.xlsx")); excel = new XSSFWorkbook(in); // 获取 Excel 文件的第一个 Sheet 页 XSSFSheet sheet = excel.getSheetAt(0); // 获取 Sheet 页中最后一行的行号(索引从 0 开始) int lastRowNum = sheet.getLastRowNum(); // 遍历每一行 for (int i = 0; i <= lastRowNum; i++) { // 获取当前行 XSSFRow row = sheet.getRow(i); if (row == null) { System.out.println("第 " + (i + 1) + " 行为空"); continue; } // 获取第 2 个单元格(索引为 1) XSSFCell cell1 = row.getCell(1); String cellValue1 = getCellValueAsString(cell1); // 获取第 3 个单元格(索引为 2) XSSFCell cell2 = row.getCell(2); String cellValue2 = getCellValueAsString(cell2); // 输出单元格内容 System.out.println(cellValue1 + " " + cellValue2); } } catch (IOException e) { // 捕获并处理 IO 异常 System.err.println("读取 Excel 文件时发生错误!"); e.printStackTrace(); } finally { // 确保关闭资源 try { if (in != null) { in.close(); } if (excel != null) { excel.close(); } } catch (IOException e) { System.err.println("关闭资源时发生错误!"); e.printStackTrace(); } } } /** * 获取单元格的值(统一转换为字符串类型) */ private static String getCellValueAsString(XSSFCell cell) { if (cell == null) { return ""; } switch (cell.getCellType()) { case STRING: return cell.getStringCellValue(); // 字符串类型 case NUMERIC: return String.valueOf(cell.getNumericCellValue()); // 数字类型 case BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); // 布尔类型 case FORMULA: return cell.getCellFormula(); // 公式类型 default: return ""; // 未知类型返回空字符串 } } }
执行结果: