Write Excel File Example
This program demonstrates writing data into an Excel file.
Problem Statement
Write a Java program that writes rows and columns into an Excel file using Apache POI.
Source Code
| 1 | import java.io.*; |
| 2 | import org.apache.poi.ss.usermodel.*; |
| 3 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 4 | public class WriteExcelExample { |
| 5 | public static void main(String[] args) { |
| 6 | System.out.println("Eduinq Write Excel Example"); |
| 7 | Workbook wb=new XSSFWorkbook(); |
| 8 | Sheet sheet=wb.createSheet("EduinqData"); |
| 9 | Row row=sheet.createRow(0); |
| 10 | row.createCell(0).setCellValue("ID"); |
| 11 | row.createCell(1).setCellValue("Name"); |
| 12 | row.createCell(2).setCellValue("Country"); |
| 13 | try(FileOutputStream fos=new FileOutputStream("output.xlsx")) { |
| 14 | wb.write(fos); |
| 15 | System.out.println("Eduinq Excel Written Successfully"); |
| 16 | } catch(IOException e) { |
| 17 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 18 | } |
| 19 | } |
| 20 | } |
Program Output
Eduinq Write Excel Example Eduinq Excel Written Successfully
Explanation
Apache POI XSSFWorkbook creates new Excel files. - Rows and cells are created programmatically. - Exception handling ensures safe operations. - In enterprise systems, Excel writing is used for exporting structured reports. Conclusion: Shows writing Excel files.