Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Browser testing is essential if you have a website or web applications that users interact with. Manual testing can be very helpful to an extent, but given the multiple browsers available, not to mention versions and operating system, testing everything manually becomes time-consuming and repetitive.

To help automate this process, Selenium is a popular choice for developers, as an open-source tool with a large and active community. What's more, we can further scale our automation testing by running on theLambdaTest cloud-based testing platform.

Read more through our step-by-step tutorial on how to set up Selenium tests with Java and run them on LambdaTest:

>> Automated Browser Testing With Selenium

Partner – Orkes – NPI EA (cat=Java)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

1. Overview

Apache POI is an open-source library that allows us to work programmatically with Microsoft Office documents, including Excel. Apache POI has three different classes that can be used to create workbooks: HSSFWorkbook, XSSFWorkbook, and SXSSFWorkbook.

In this tutorial, we’ll compare the functionality of these three classes and conduct some evaluations to help us choose the best option for our particular use case.

2. Creating an Excel File

Before comparing them, let’s do a quick review on how to generate an Excel file using Apache POI. We’ll need the Apache POI and POI OOXML schema dependencies in the pom.xml:

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

To create an instance of Workbook, we need to call the default constructor of our target Workbook class. After that, we can write our content to the Workbook and then write the Workbook into the target OutputStream.

In the following example, we create a Workbook with XSSFWorkbook and write some content to the test.xlsx file:

try (Workbook workbook = new XSSFWorkbook();
  OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("test.xlsx"))) {
    Sheet sheet = workbook.createSheet("test");
    sheet.createRow(0).createCell(0).setCellValue("test content");
    workbook.write(outputStream);
}

It’s simple to replace the Workbook class because we only need to change the instance of XSSFWorkbook to whatever Workbook class we want to use, and after that, everything is just the same: creating sheets, rows, and columns in the Excel file.

Another thing to remember is that HSSFWorkbook is used for the old Excel format, so the output file needs to have the .xls extension. The XSSFWorkbook and the SXSSFWorkbook are used for the newer Excel format, which provides an output file with the .xlsx extension.

3. General Comparisons

The three Workbook classes available in Apache POI are HSSFWorkbook, XSSFWorkbook, and SXSSFWorkbook. They share similarities yet offer distinct functionalities catering to different file formats and use cases. Here’s a quick summary of their main characteristics:

HSSFWorkbook XSSFWorkbook SXSSFWorkbook
File Format .xls (BIFF8 binary, Excel 97-2003) .xlsx (OpenXML, Excel 2007+) .xlsx (OpenXML, Excel 2007+)
Max Rows per Sheet 65,536 1,048,576 1,048,576
Max Columns per Sheet 256 16,384 16,384
Streaming Workbook No No Yes
Memory Usage High High Low

In short, HSSFWorkbook produces Excel files in the older .xls format. The XSSFWorkbook and SXSSFWorkbook create files in the XML-based .xlsx format used by Excel 2007 and later.

Both HSSFWorbook and XSSFWorkbook are non-streaming workbooks that keep all rows of data in memory, whereas SXSSFWorkbook is a streaming workbook that only retains a certain number of rows in memory. Hence, it’s much more memory-efficient if the dataset is huge.

4. Unsupported Functions in SXSSFWorkbook

A streaming workbook flushes rows when the number of rows in the window reaches a threshold called row access window size. The default value is 100, but we can change it with the constructor. For example, let’s see how to create a workbook with a window size of 50:

Workbook workbook = new SXSSFWorkbook(50);

Due to this streaming behavior, accessing all of the row information simultaneously is impossible, which can sometimes lead to the invocation failure of some Apache POI functions. In the following subsections, we’ll examine which functions are not supported in the SXSSFWorkbook:

4.1. Test Setup

Let’s set up some tests to verify the functionalities that do not work in the streaming workbook. We’ll create an instance of SXSSFWorkbook with two data rows and one column in the sheet.

For our demonstration, we’ll explicitly set the window size to 1. This would be considerably larger in reality:

@BeforeEach
void setup() {
    workbook = new SXSSFWorkbook(1);
    sheet = workbook.createSheet("Test Sheet");
    sheet.createRow(0).createCell(0).setCellValue(5);
    sheet.createRow(1).createCell(0).setCellValue(15);
}

4.2. Auto-Size Column

The auto-size column function automatically sets the width of the column to the longest cell value in that column so that the data becomes fully visible without manual resizing. This should fail during streaming, as it does not know the row widths:

@Test
void whenAutoSizeColumnOnSXSSFWorkbook_thenThrowsIllegalStateException() {
    assertThrows(IllegalStateException.class, () -> sheet.autoSizeColumn(0));
}

4.3. Clone Sheet

The clone sheet function creates a copy of an existing sheet within the Workbook. This operation isn’t supported in streaming because it doesn’t keep all data from the sheet in memory:

@Test
void whenCloneSheetOnSXSSFWorkbook_thenThrowsIllegalStateException() {
    assertThrows(IllegalStateException.class, () -> workbook.cloneSheet(0));
}

4.4. Get Row

Now, let’s attempt to get a row that has already been flushed. It’ll return null from the sheet:

@Test
void whenGetRowOnSXSSFWorkbook_thenReturnNull() {
    Row row = sheet.getRow(0);
    assertThat(row).isNull();
}

However, in the case of a window size of 2, it will return the instance of the row because it hasn’t been flushed yet.

4.5. Formula Evaluation

Formula evaluation refers to the re-computation of the formula result present in Excel cells. Similar to previous cases, evaluation may happen only when rows aren’t flushed:

@Test
void whenEvaluateFormulaCellOnSXSSFWorkbook_thenThrowsIllegalStateException() {
    Cell formulaCell = sheet.createRow(sheet.getLastRowNum()).createCell(0);
    formulaCell.setCellFormula("SUM(A1:B1)");

    FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
    assertThrows(SXSSFFormulaEvaluator.RowFlushedException.class,
      () -> evaluator.evaluateFormulaCell(formulaCell));
}

4.6. Shift Columns

Finally, let’s look into shift columns, which shift existing columns left or right in a sheet. It’s expected to fail in the streaming workbook, as shifting a column obviously requires the whole data of the sheet to be in memory:

@Test
void whenShiftColumnsOnSXSSFWorkbook_thenThrowsUnsupportedOperationException() {
    assertThrows(UnsupportedOperationException.class, () -> sheet.shiftColumns(0, 2, 1));
}

5. Evaluations

We’ve discussed the functional differences between the three Workbook classes. Now, it’s time to perform experiments to compare execution time and memory consumption. These are the basic metrics for the Workbook class choice regarding our requirements.

For our comparison, we’ll adopt JMH (Java Microbenchmark Harness) for benchmarking. Let’s add the JMH dependencies to the pom.xml. Both Core and Annotation Processors can be found in Maven Central:

<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-core</artifactId>
    <version>1.37</version>
</dependency>
<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-generator-annprocess</artifactId>
    <version>1.37</version>
</dependency>

5.1. Experiments Setup

In our experiments, let’s create a sheet via the chosen Workbook class and write a specified number of rows into an Excel file, each containing 256 columns with identical text across all columns:

Sheet sheet = workbook.createSheet();
for (int n=0;n<iterations;n++) {
    Row row = sheet.createRow(sheet.getLastRowNum()+1);
    for (int c=0;c<256;c++) {
        Cell cell = row.createCell(c);
        cell.setCellValue("abcdefghijklmnopqrstuvwxyz");
    }
}

We’ll execute the test for various quantities of rows: 2,500, 5,000, 10,000, 20,000, and 40,000 rows. Three classes of Workbook will be tested based on execution time and memory consumption. We’ll conduct each set of experiments three times and take their average as the results.

5.2. Execution Time

Let’s look at the execution time (in milliseconds) each workbook class took to write a certain number of rows into an Excel file:

Number of Rows HSSFWorkbook XSSFWorkbook SXSSFWorkbook
2,500 73 2,658 296
5,000 174 4,522 612
10,000 347 10,994 1,808
20,000 754 21,733 3,751
40,000 1,455 42,331 7,342

Among the three classes, the HSSFWorkbook is always faster compared to the XSSFWorkbook and the SXSSFWorkbook. The XSSFWorkbook shows the highest execution time, which is around 30 times slower than the HSSFWorkbook. The SXSSFWorkbook class provides a compromise between the two.

The reason for such results could be that the binary .xls format is less complex to handle. It’s evident that the XML-based .xlsx format requires more processing, and such slowdown will be more significant with larger datasets. 

5.3. Memory Consumption

Let’s review the memory consumption (in megabytes) for each workbook class when writing the same number of rows:

Number of Rows HSSFWorkbook XSSFWorkbook SXSSFWorkbook
2,500 828 1,871 258
5,000 1,070 2,926 212
10,000 1,268 4,136 209
20,000 1,766 7,443 209
40,000 1,475 10,119 210

For both the HSSFWorkbook and the XSSFWorkbook, the memory consumption grows with the number of rows. That’s because these Workbook classes store all data in memory. However, the XSSFWorkbook grows significantly more than the HSSFWorkbook.

SXSSFWorkbook is the clear winner in terms of memory efficiency. Its memory consumption remains almost constant, regardless of the number of rows, at around 210MB.

This is due to its streaming behavior, where only a small portion of rows is kept in memory at any given time. This makes SXSSFWorkbook ideal for handling large datasets without running out of memory.

6. Conclusion

The HSSFWorkbook, XSSFWorkbook, and SXSSFWorkbook have different use cases:

  • HSSFWorkbook is the fastest but is limited to the old .xls format and small datasets.
  • XSSFWorkbook supports most of the Excel features in the .xlsx format. However, this is highly memory-consuming.
  • SXSSFWorkbook excels with big data sets and does so using very little memory, via its streaming capabilities. However, some functionalities are missing compared to the XSSFWorkbook.

In general, we can choose SXSSFWorkbook for large files, XSSFWorkbook for complete features, and HSSFWorkbook for compatibility with older Excel formats.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)