Introduction:
Here is the sample java class to create an excel sheet.
Explanation:
1. Create the workbook object.
2. Create the file output stream object for wringing the data into the file.
3. Create the sheet using the sheet object.
4. By sheet object, we can create the number of sheets by using the create sheet method.
5. Finally, write whole information using the write method to create the sheets.
- In this article, I am gonna explain how to create and write to an excel file in Java using Apache POI.
- Apache poi is a open source java library.
- Using apache poi we can create and manipulate the various types of files formats based on Microsoft office.
- Java doesn’t provide built-in support for working with excel files so we are using apache poi.
- Apache POI is able to handle both XLS and XLSX formats of spreadsheets.
Here is the sample java class to create an excel sheet.
package com.Excel; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * * @author Janardhan Randhi */ public class CreateExcel { public static void main(String[] args) throws FileNotFoundException, IOException { // Creating Workbook instances Workbook wb = new HSSFWorkbook(); // An output stream accepts output bytes and sends them to sink. FileOutputStream fileOut = new FileOutputStream("Javasnippets.xlsx"); // Creating Sheets using sheet object Sheet sheet1 = wb.createSheet("Home"); Sheet sheet2 = wb.createSheet("About"); Sheet sheet3 = wb.createSheet("Contact us"); Sheet sheet4 = wb.createSheet("Desclimer"); Sheet sheet5 = wb.createSheet("Privacy policy"); Sheet sheet6 = wb.createSheet("Email"); System.out.println("Sheets Has been Created successfully"); wb.write(fileOut); } }
1. Create the workbook object.
2. Create the file output stream object for wringing the data into the file.
3. Create the sheet using the sheet object.
4. By sheet object, we can create the number of sheets by using the create sheet method.
5. Finally, write whole information using the write method to create the sheets.