Creating a blank worksheet

In this post under Apache Excel, I will show with example how to create a blank excel document.

Below is the code for your reference.

Main class


1  package package1;
2  
3  import java.io.File;
4  import java.io.FileOutputStream;
5  import java.io.IOException;
6  
7  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
8  
9  public class Example1 {
10     public static void main(String[] args) throws IOException {
11         File file = new File("Example1.xlsx");
12         try(XSSFWorkbook workbook = new XSSFWorkbook();
13             FileOutputStream fileOutputStream = new FileOutputStream(file)) {
14             workbook.createSheet("Page1");
15             workbook.write(fileOutputStream);
16         }
17     }
18 }

In this above code, at line 11, we create a File object with file name “Example1.xlsx”.

Next in try with resources block, I create two resources, one an instance of “XSSFWorkbook” and another an instance of “FileOutputStream”.

The instance of XSSFWorkbook represents an Excel (.xlsx) document.

At line 14, we create a sheet inside the Excel document and name it as “Page1”.

At line 15, we write the Excel workbook to a file using an instance of “FileOutputStream”.

In this way, we can create an Excel document with blank sheet.

Leave a Reply