Read Multiple Sheets Example
This program demonstrates reading multiple sheets in an Excel file.
Problem Statement
Write a Java program that reads data from multiple sheets in an Excel file.
Source Code
| 1 | import java.io.*; |
| 2 | import org.apache.poi.ss.usermodel.*; |
| 3 | public class ReadMultipleSheetsExample { |
| 4 | public static void main(String[] args) { |
| 5 | System.out.println("Eduinq Read Multiple Sheets Example"); |
| 6 | try(FileInputStream fis=new FileInputStream("multi.xlsx")) { |
| 7 | Workbook wb=WorkbookFactory.create(fis); |
| 8 | for(int i=0;i<wb.getNumberOfSheets();i++){ |
| 9 | Sheet sheet=wb.getSheetAt(i); |
| 10 | System.out.println("Eduinq Sheet: "+sheet.getSheetName()); |
| 11 | for(Row row:sheet){ |
| 12 | for(Cell cell:row){ |
| 13 | System.out.print(cell+" | "); |
| 14 | } |
| 15 | System.out.println(); |
| 16 | } |
| 17 | } |
| 18 | } catch(Exception e) { |
| 19 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 20 | } |
| 21 | } |
| 22 | } |
Program Output
Eduinq Read Multiple Sheets Example Eduinq Sheet: Students 1 | Arun | India | 2 | Priya | USA | Eduinq Sheet: Courses 101 | Java | 102 | Python |
Explanation
Iterates through all sheets using getNumberOfSheets(). - Prints sheet name and data. - Exception handling ensures safe operations. - In enterprise systems, multi-sheet reading is used for complex reports. Conclusion: Shows reading multiple sheets in Excel.