Site icon TestingDocs.com

Apache POI Workbook Interface

Overview

In this tutorial, we will learn about Apache POI Workbook Interface. To know more about Java Interface:

https://www.testingdocs.com/java-interface-with-examples/

Workbook Interface

Apache POI Workbook Interface is an high-level representation of an Excel Workbook. It is also the top-level object for creating new Excel sheets in a Java program. We will construct this object to read or write an Excel workbook.

 

 

public interface Workbook extends Closeable, Iterable<Sheet> {

}

The interface is defined in the package org.apache.poi.ss.usermodel. We can import the interface in the Java program using the below statement.

import org.apache.poi.ss.usermodel.Workbook;

Closeable Interface

A Closeable denotes a source or destination of data that can be closed. The close method is invoked to release resources that the object is holding the open files.

Thew close() method closes the stream and releases any system resources associated with it. If the stream is already closed then invoking the close() has no effect.

Iterable Interface

Implementing Iterable interface allows an object to be used in the enhanced for statement/ for-each loop statement.

The classes such as HSSFWorkbook and XSSFWorkbook implement Workbook interface. HSSFWorkbook class is used to read/write Excel file with .xls extension. XSSFWorkbook class is used to read/write Excel file with .xlsx extension.

 

 

Example Usage

Sample use of the interface in Java program:

//Create a Workbook object
Workbook wb = new XSSFWorkbook();
// Create a Sheet
Sheet sheet = wb.createSheet("Sample Excel Sheet");

//Create file content - This depends on the Logic

// Write output to an Excel file
String file = "sampleExcelFile.xlsx";
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();

Apache POI Tutorials:

https://www.testingdocs.com/apache-poi-tutorials/

More Information on Apache POI API:

https://poi.apache.org/

Exit mobile version