myrelaxsauna.com

Generating Large Excel Files in Java with Apache POI

Written on

Chapter 1: Introduction to Excel File Generation

Generating Excel files is a common requirement in many web applications. This guide will discuss how to efficiently create large Excel files using Java and Apache POI.

The challenge arises when a client needs to export real-time data from a database that may contain millions of records. Attempting to generate an Excel file with such vast data can lead to an "out of memory" error.

If you encounter this issue during the Excel file generation process, there is a solution. Utilizing Apache POI version 3.16 or newer along with Java 1.7 or above can significantly reduce the logical memory footprint on the Java Virtual Machine (JVM).

Here’s a sample implementation to illustrate how this can be done:

public static void generateExcelExample1() throws IOException {

SXSSFWorkbook sxssWb = new SXSSFWorkbook(100);

SXSSFSheet sxssSheet = (SXSSFSheet) sxssWb.createSheet("sheet1");

sxssSheet.setRandomAccessWindowSize(100);

Iterator<UserInfo> it = sampleData().stream().iterator();

int rowNumber = 0;

while (it.hasNext()) {

UserInfo userInfo = (UserInfo) it.next();

SXSSFRow row = (SXSSFRow) sxssSheet.createRow(rowNumber++);

SXSSFCell cell = (SXSSFCell) row.createCell(0);

cell.setCellValue(userInfo.getFirstName());

cell = (SXSSFCell) row.createCell(1);

cell.setCellValue(userInfo.getLastName());

}

// Support file name UTF-8.

String originalNameEncode = URLEncoder.encode("excel.xlsx", "UTF-8");

FileOutputStream outputStream = new FileOutputStream(originalNameEncode);

sxssWb.write(outputStream);

sxssWb.close();

}

@Data

public class UserInfo {

private String firstName;

private String lastName;

public UserInfo(String firstName, String lastName){

this.firstName = firstName;

this.lastName = lastName;

}

}

public List<UserInfo> sampleData(){

List<UserInfo> userInfos = new ArrayList<>();

for(int i = 0 ; i < 1000000; i++) {

userInfos.add(new UserInfo("Jane", "Doe"));

}

return userInfos;

}

Chapter 2: The Result of Excel File Creation

Example of generated Excel file

The Excel file has been successfully created.

Displaying data in an Excel file

The method setRandomAccessWindowSize is essential as it minimizes logical memory usage on the JVM, thus helping to avoid the "out of memory" error. Developers have the flexibility to adjust this size based on their specific project requirements.

It is important to note that the SXSSFWorkbook, which is used for writing rows and cells, does not allow for editing or replacing cell values. If you need to modify cell values after the file has been created, consider switching to XSSFWorkbook.

Thank you for your attention.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

A Comical Journey Through Life: Ikpa's Daily Struggles

Follow Ikpa's humorous day-to-day adventures as he navigates life’s absurdities with a grateful heart and a playful spirit.

Strategies for Selecting the Optimal Learning Experience

Explore how to identify effective learning methods and experiences through science-backed questions and practical examples.

# Embracing Growth: A Daily Commitment to Progress and Dreams

Discover how to stay committed to your goals and dreams while maintaining balance in life, inspired by daily experiences.

Title: Transforming Ordinary Spaces with the Rossetta Star Projector

Discover how the Rossetta Star Projector transforms mundane rooms into celestial wonderlands, sparking imagination and creativity in children.

Transform Your Mornings: The ABC Method for Success

Discover how waking up early can change your life with the ABC method for enhanced productivity and happiness.

Top Interactive Widgets to Enhance Your iOS 17 Experience

Discover the best interactive widgets for iOS 17 that can boost productivity and streamline your daily tasks.

# Essential Lessons Employees Can Learn from Hustlers and Creators

Discover valuable insights employees can gain from hustlers and content creators to enhance their professional journey.

Innovative NYC Program Offers Homeowners Up to $395,000 for ADUs

NYC's new initiative provides financial support to homeowners for building accessory dwelling units to combat housing shortages.