Creating Sheets in Excel File in Java using Apache POI

For a given Excel file (here Geeks.xlsx), it is needed to create sheets in it.

Steps:

  1. Create a JAVA Maven project
  2. Add dependency in pom.xml file
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
          <version>3.12</version>
        </dependency>
        <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>3.12</version>
        </dependency>

    chevron_right

    
    

  3. Create a class in javaResource folder
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.io.*;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
      
    public class CreateSheet {
        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.
            OutputStream fileOut = new FileOutputStream("Geeks.xlsx");
              
            // Creating Sheets using sheet object
            Sheet sheet1 = wb.createSheet("Array");
            Sheet sheet2 = wb.createSheet("String");
            Sheet sheet3 = wb.createSheet("LinkedList");
            Sheet sheet4 = wb.createSheet("Tree");
            Sheet sheet5 = wb.createSheet("Dynamic Programing");
            Sheet sheet6 = wb.createSheet("Puzzles");
              
      
    System.out.println("Sheets Has been Created successfully");
      
            wb.write(fileOut);
        }
    }

    chevron_right

    
    

Output:

Sheets Has been Created successfully

Here 6 sheets has been created in geeks.xlsx file:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.