1. Products
  2.   Spreadsheet
  3.   Java
  4.   FastExcel
 
  

Open Source Java API for Microsoft Excel Spreadsheets

Create, Read, Edit and Convert Microsoft Excel Spreadsheet files in Java applications via Open Source API.

What is FastExcel?

Working with large Excel files is always a big challenge for software programmers and requires high level skills and resources to manage it. FastExcel is a very powerful open source Java Excel library that allows software developers to create and read Microsoft Excel XLSX workbooks inside their own Java applications. It helps developers to work with large Excel files without any external dependencies.

FastExcel is very simple to use and reduces memory footprint and high performance by accumulating only necessary elements. It has included several important features for working with spreadsheets such as creating simple workbooks, apply styles and formatting to cells, columns and rows, Set style on a range of cells, merge cells and rows, shade alternate rows, set paper size and page orientation, set page margins, create a freeze pane and so on.

FastExcel provides complete support for multithreading which means each worksheet in the workbook can be generated by a different thread, while fully supporting shared strings and styles. The FastExcel reader is very efficient and read only cells content and discards styles, graphs, and other stuff.

Previous Next

Getting Started with FastExcel

The FastExcel library requires Java 8+. Build with Maven. Please include the following dependency in your POM

FastExcel Maven Dependency

<dependency>
<groupId> org.dhatim</groupId>
<artifactId>fastexcel</artifactId>
<version>0.12.13</version>
</dependency>

You can also download the compiled shared library from the GitHub repository and install it.

Generate Excel XLSX File via Java API

The open source FastExcel library allows software developers to generate Excel XLSX File with just a couple of lines of Java code. It is very easy to add a new workbook and add different worksheets, insert cells and rows inside the worksheet. The library supports several features related to text formatting and styling such as change cell style, set style on a range of cells, shade alternate rows, Set paper size and page orientation and so on.

Create New Excel Fie via Java Library

package com.zetcode;

import org.dhatim.fastexcel.Workbook;
import org.dhatim.fastexcel.Worksheet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class FastExcelSimpleWrite {

    public static void main(String[] args) throws IOException {

        var words = List.of("sky", "blue", "work", "falcon");

        int row = 0;
        int col = 0;

        var f = new File("/home/janbodnar/tmp/words.xlsx");

        try (var fos = new FileOutputStream(f)) {

            var wb = new Workbook(fos, "Application", "1.0");
            Worksheet ws = wb.newWorksheet("Sheet 1");

            for (var word : words) {

                ws.value(row, col, word);
                row++;
            }

            wb.finish();
        }
    }
}

Read Excel XLSX File via Java API

The FastExcel library has provided a very powerful reader that allows users to open and read an Excel workbook with ease. It is a streaming alternative of Apache POI but very simple to handle as compare to Apache POI and 10 times faster than it. It can only read cell content and discards styles, graphs, and many other stuff. The following example shows how to open a workbook and read all rows in a streaming way using Java.

Open & Read Workbook Rows in Streaming Way via Java

try (InputStream is = ...; ReadableWorkbook wb = new ReadableWorkbook(is)) {
    Sheet sheet = wb.getFirstSheet();
    try (Stream rows = sheet.openStream()) {
        rows.forEach(r -> {
            BigDecimal num = r.getCellAsNumber(0).orElse(null);
            String str = r.getCellAsString(1).orElse(null);
            LocalDateTime date = r.getCellAsDate(2).orElse(null);
        });
    }
} 

Multithreaded Spreadsheet Generation via FastExcel`

Multithreading is the ability of a central processing unit (CPU) to provide multiple threads of execution concurrently, supported by the operating system. If the computer has multiple processors or processor cores, the operating system takes responsibility for allocating the threads to the processors in the most efficient way. The open source FastExcel library fully support multithreaded generation and create each worksheet a different thread using Java code.

Generate Spreadsheets in Multithreading Environment via Java API

try (OutputStream os = ...) {
    Workbook wb = new Workbook(os, "MyApplication", "1.0");
    Worksheet ws1 = wb.newWorksheet("Sheet 1");
    Worksheet ws2 = wb.newWorksheet("Sheet 2");
    CompletableFuture cf1 = CompletableFuture.runAsync(() -> {
        // Fill worksheet 1
        ...
    });
    CompletableFuture cf2 = CompletableFuture.runAsync(() -> {
        // Fill worksheet 2
        ...
    });
    CompletableFuture.allOf(cf1, cf2).get();
    wb.finish();
}
 English