Write Multiple Sheets Example
This program demonstrates writing multiple sheets in an Excel file.
Problem Statement
Write a Java program that creates multiple sheets in an Excel file.
Source Code
| 1 | import java.io.*; |
| 2 | import org.apache.poi.ss.usermodel.*; |
| 3 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 4 | public class WriteMultipleSheetsExample { |
| 5 | public static void main(String[] args) { |
| 6 | System.out.println("Eduinq Write Multiple Sheets Example"); |
| 7 | Workbook wb=new XSSFWorkbook(); |
| 8 | Sheet s1=wb.createSheet("Students"); |
| 9 | Row r1=s1.createRow(0); |
| 10 | r1.createCell(0).setCellValue("ID"); |
| 11 | r1.createCell(1).setCellValue("Name"); |
| 12 | Sheet s2=wb.createSheet("Courses"); |
| 13 | Row r2=s2.createRow(0); |
| 14 | r2.createCell(0).setCellValue("CourseID"); |
| 15 | r2.createCell(1).setCellValue("CourseName"); |
| 16 | try(FileOutputStream fos=new FileOutputStream("multi.xlsx")) { |
| 17 | wb.write(fos); |
| 18 | System.out.println("Eduinq Multiple Sheets Written Successfully"); |
| 19 | } catch(IOException e) { |
| 20 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 21 | } |
| 22 | } |
| 23 | } |
Program Output
Eduinq Write Multiple Sheets Example Eduinq Multiple Sheets Written Successfully
Explanation
Creates multiple sheets using createSheet(). - Adds rows and cells in each sheet. - Exception handling ensures safe operations. - In enterprise systems, multiple sheets store different categories of data. Conclusion: Shows writing multiple sheets in Excel.