How to retrieve the sheet name in Java?

To use the Apache POI library to manipulate Excel files in Java, you can use the following code to get the name of a sheet:


FileInputStream fis = new FileInputStream("example.xlsx");
Workbook workbook = WorkbookFactory.create(fis);

// 获取第一个 sheet 的名称
String sheetName = workbook.getSheetName(0);
System.out.println("Sheet Name: " + sheetName);

// 或者通过 sheet 对象来获取名称
Sheet sheet = workbook.getSheetAt(0);
System.out.println("Sheet Name: " + sheet.getSheetName());

This way you can obtain the name of the specified sheet.

Leave a Reply 0

Your email address will not be published. Required fields are marked *