CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-alibaba--easyexcel

High-performance Java library for reading and writing Excel files with minimal memory usage

Pending
Overview
Eval results
Files

writing-operations.mddocs/

Writing Operations

Functionality for creating and writing Excel files with template support, styling options, and memory-efficient streaming for large datasets.

Capabilities

Main Write Entry Points

Factory methods for creating Excel writers with various output destinations.

/**
 * Create a writer builder without specifying output destination
 * @return ExcelWriterBuilder for configuration
 */
public static ExcelWriterBuilder write();

/**
 * Create a writer builder for a file
 * @param file File to write to
 * @return ExcelWriterBuilder for configuration
 */
public static ExcelWriterBuilder write(File file);

/**
 * Create a writer builder for a file path
 * @param pathName Path to the file to write
 * @return ExcelWriterBuilder for configuration
 */
public static ExcelWriterBuilder write(String pathName);

/**
 * Create a writer builder for an output stream
 * @param outputStream OutputStream to write to
 * @return ExcelWriterBuilder for configuration
 */
public static ExcelWriterBuilder write(OutputStream outputStream);

/**
 * Create a writer builder with head class
 * @param file File to write to
 * @param head Class for data mapping
 * @return ExcelWriterBuilder for configuration
 */
public static ExcelWriterBuilder write(File file, Class<?> head);

/**
 * Create a writer builder with head class for output stream
 * @param outputStream OutputStream to write to
 * @param head Class for data mapping
 * @return ExcelWriterBuilder for configuration
 */
public static ExcelWriterBuilder write(OutputStream outputStream, Class<?> head);

ExcelWriterBuilder Configuration

Fluent builder for configuring Excel writer options before execution.

public class ExcelWriterBuilder {
    /**
     * Set the output destination file
     * @param file File to write to
     * @return this builder
     */
    public ExcelWriterBuilder file(File file);
    
    /**
     * Set the output destination by path
     * @param pathName Path to file
     * @return this builder
     */
    public ExcelWriterBuilder file(String pathName);
    
    /**
     * Set the output destination stream
     * @param outputStream OutputStream to write to
     * @return this builder
     */
    public ExcelWriterBuilder file(OutputStream outputStream);
    
    /**
     * Set the head class for data mapping
     * @param head Class with ExcelProperty annotations
     * @return this builder
     */
    public ExcelWriterBuilder head(Class<?> head);
    
    /**
     * Set character encoding for CSV files
     * @param charset Character encoding
     * @return this builder
     */
    public ExcelWriterBuilder charset(Charset charset);
    
    /**
     * Set password for file encryption
     * @param password File password
     * @return this builder
     */
    public ExcelWriterBuilder password(String password);
    
    /**
     * Configure memory mode vs file mode
     * @param inMemory true for memory mode, false for file mode
     * @return this builder
     */
    public ExcelWriterBuilder inMemory(Boolean inMemory);
    
    /**
     * Use a template file for writing
     * @param templateFile Template Excel file
     * @return this builder
     */
    public ExcelWriterBuilder withTemplate(File templateFile);
    
    /**
     * Use a template from InputStream
     * @param templateInputStream Template Excel stream
     * @return this builder
     */
    public ExcelWriterBuilder withTemplate(InputStream templateInputStream);
    
    /**
     * Use a template from file path
     * @param templateFileName Template file path
     * @return this builder
     */
    public ExcelWriterBuilder withTemplate(String templateFileName);
    
    /**
     * Configure whether to write Excel on exception
     * @param writeExcelOnException true to write partial data on exception
     * @return this builder
     */
    public ExcelWriterBuilder writeExcelOnException(Boolean writeExcelOnException);
    
    /**
     * Configure BOM for CSV files
     * @param withBom true to include BOM in CSV files
     * @return this builder
     */
    public ExcelWriterBuilder withBom(Boolean withBom);
    
    /**
     * Build the ExcelWriter instance
     * @return ExcelWriter for manual control
     */
    public ExcelWriter build();
    
    /**
     * Create a sheet builder for sheet configuration
     * @return ExcelWriterSheetBuilder
     */
    public ExcelWriterSheetBuilder sheet();
    
    /**
     * Create a sheet builder for specific sheet
     * @param sheetNo Sheet index (0-based)
     * @return ExcelWriterSheetBuilder
     */
    public ExcelWriterSheetBuilder sheet(Integer sheetNo);
    
    /**
     * Create a sheet builder for named sheet
     * @param sheetName Sheet name
     * @return ExcelWriterSheetBuilder
     */
    public ExcelWriterSheetBuilder sheet(String sheetName);
}

ExcelWriterSheetBuilder Configuration

Configure specific sheet writing parameters and execute writing operations.

public class ExcelWriterSheetBuilder {
    /**
     * Set sheet index
     * @param sheetNo Sheet index (0-based)
     * @return this builder
     */
    public ExcelWriterSheetBuilder sheetNo(Integer sheetNo);
    
    /**
     * Set sheet name
     * @param sheetName Sheet name
     * @return this builder
     */
    public ExcelWriterSheetBuilder sheetName(String sheetName);
    
    /**
     * Set head class for this sheet
     * @param head Class with annotations
     * @return this builder
     */
    public ExcelWriterSheetBuilder head(Class<?> head);
    
    /**
     * Write data to the sheet
     * @param data Collection of data objects to write
     */
    public void doWrite(Collection<?> data);
    
    /**
     * Fill template with data
     * @param data Object or Map containing fill data
     */
    public void doFill(Object data);
    
    /**
     * Fill template with collection data
     * @param data Collection of objects for filling
     */
    public void doFill(Collection<?> data);
    
    /**
     * Create a table builder for table-level configuration
     * @return ExcelWriterTableBuilder
     */
    public ExcelWriterTableBuilder table();
    
    /**
     * Create a table builder for specific table
     * @param tableNo Table index (0-based)
     * @return ExcelWriterTableBuilder
     */
    public ExcelWriterTableBuilder table(Integer tableNo);
}

ExcelWriterTableBuilder Configuration

Configure table-level writing parameters for advanced scenarios.

public class ExcelWriterTableBuilder {
    /**
     * Set table index
     * @param tableNo Table index (0-based)
     * @return this builder
     */
    public ExcelWriterTableBuilder tableNo(Integer tableNo);
    
    /**
     * Set head class for this table
     * @param head Class with annotations
     * @return this builder
     */
    public ExcelWriterTableBuilder head(Class<?> head);
    
    /**
     * Write data to the table
     * @param data Collection of data objects to write
     */
    public void doWrite(Collection<?> data);
}

ExcelWriter Manual Control

Direct control over the writing process for advanced scenarios.

public class ExcelWriter implements Closeable {
    /**
     * Write data to specific sheet
     * @param data Collection of data to write
     * @param writeSheet Sheet configuration
     * @return ExcelWriter for chaining
     */
    public ExcelWriter write(Collection<?> data, WriteSheet writeSheet);
    
    /**
     * Write data to specific sheet and table
     * @param data Collection of data to write  
     * @param writeSheet Sheet configuration
     * @param writeTable Table configuration
     * @return ExcelWriter for chaining
     */
    public ExcelWriter write(Collection<?> data, WriteSheet writeSheet, WriteTable writeTable);
    
    /**
     * Fill template with data
     * @param data Fill data object or Map
     * @param writeSheet Sheet configuration
     * @return ExcelWriter for chaining
     */
    public ExcelWriter fill(Object data, WriteSheet writeSheet);
    
    /**
     * Fill template with collection data
     * @param data Collection of fill data
     * @param fillConfig Fill configuration
     * @param writeSheet Sheet configuration
     * @return ExcelWriter for chaining
     */
    public ExcelWriter fill(Collection<?> data, FillConfig fillConfig, WriteSheet writeSheet);
    
    /**
     * Finish writing and flush data
     */
    public void finish();
    
    /**
     * Close the writer and cleanup resources
     */
    public void close();
}

Sheet and Table Factory Methods

Static factory methods for creating sheet and table configurations.

/**
 * Create a default sheet configuration
 * @return ExcelWriterSheetBuilder
 */
public static ExcelWriterSheetBuilder writerSheet();

/**
 * Create a sheet configuration by index
 * @param sheetNo Sheet index (0-based)
 * @return ExcelWriterSheetBuilder
 */
public static ExcelWriterSheetBuilder writerSheet(Integer sheetNo);

/**
 * Create a sheet configuration by name
 * @param sheetName Sheet name
 * @return ExcelWriterSheetBuilder
 */
public static ExcelWriterSheetBuilder writerSheet(String sheetName);

/**
 * Create a sheet configuration with index and name
 * @param sheetNo Sheet index (0-based)
 * @param sheetName Sheet name
 * @return ExcelWriterSheetBuilder
 */
public static ExcelWriterSheetBuilder writerSheet(Integer sheetNo, String sheetName);

/**
 * Create a default table configuration
 * @return ExcelWriterTableBuilder
 */
public static ExcelWriterTableBuilder writerTable();

/**
 * Create a table configuration by index
 * @param tableNo Table index (0-based)
 * @return ExcelWriterTableBuilder
 */
public static ExcelWriterTableBuilder writerTable(Integer tableNo);

Usage Examples

Basic Writing

import com.alibaba.excel.EasyExcel;
import java.util.List;
import java.util.Arrays;

// Simple data writing
List<UserData> users = Arrays.asList(
    new UserData("Alice", 25, "alice@example.com"),
    new UserData("Bob", 30, "bob@example.com")
);

EasyExcel.write("users.xlsx", UserData.class)
    .sheet("Users")
    .doWrite(users);

// Writing to specific sheet by index
EasyExcel.write("workbook.xlsx", UserData.class)
    .sheet(0)
    .sheetName("UserData")
    .doWrite(users);

Writing Multiple Sheets

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;

// Manual control for multiple sheets
try (ExcelWriter excelWriter = EasyExcel.write("multi-sheet.xlsx").build()) {
    // Create sheet configurations
    WriteSheet userSheet = EasyExcel.writerSheet(0, "Users")
        .head(UserData.class)
        .build();
    
    WriteSheet orderSheet = EasyExcel.writerSheet(1, "Orders")
        .head(OrderData.class)
        .build();
    
    // Write data to different sheets
    excelWriter.write(users, userSheet);
    excelWriter.write(orders, orderSheet);
}

Template-Based Writing

import com.alibaba.excel.EasyExcel;
import java.util.HashMap;
import java.util.Map;

// Using template for data filling
Map<String, Object> fillData = new HashMap<>();
fillData.put("name", "John Doe");
fillData.put("date", new Date());
fillData.put("total", 1500.00);

EasyExcel.write("report.xlsx")
    .withTemplate("report-template.xlsx")
    .sheet()
    .doFill(fillData);

// Filling with collection data
List<UserData> users = getUserData();
EasyExcel.write("user-report.xlsx")
    .withTemplate("user-template.xlsx")
    .sheet()
    .doFill(users);

Advanced Configuration

import com.alibaba.excel.EasyExcel;
import java.nio.charset.StandardCharsets;

// Advanced writing configuration
EasyExcel.write("advanced.xlsx", UserData.class)
    .charset(StandardCharsets.UTF_8) // For CSV files
    .password("secret123") // Encrypt the file
    .inMemory(false) // Use file mode for large datasets
    .writeExcelOnException(true) // Write partial data on errors
    .sheet("UserData")
    .doWrite(users);

// CSV writing with BOM
EasyExcel.write("data.csv", UserData.class)
    .charset(StandardCharsets.UTF_8)
    .withBom(true)
    .sheet()
    .doWrite(users);

Streaming Large Datasets

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;

// Efficient writing of large datasets
try (ExcelWriter excelWriter = EasyExcel.write("large-dataset.xlsx", UserData.class)
        .inMemory(false) // Use file mode to reduce memory usage
        .build()) {
    
    WriteSheet writeSheet = EasyExcel.writerSheet("Users").build();
    
    // Write data in batches
    int batchSize = 1000;
    int totalRecords = getUserCount();
    
    for (int i = 0; i < totalRecords; i += batchSize) {
        List<UserData> batch = getUserBatch(i, batchSize);
        excelWriter.write(batch, writeSheet);
    }
}

Writing with Custom Styling

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;

// Custom styling example
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 14);
headWriteFont.setBold(true);
headWriteCellStyle.setWriteFont(headWriteFont);

WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

HorizontalCellStyleStrategy horizontalCellStyleStrategy = 
    new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

EasyExcel.write("styled.xlsx", UserData.class)
    .registerWriteHandler(horizontalCellStyleStrategy)
    .sheet("StyledUsers")
    .doWrite(users);

Writing Different Data Types

import com.alibaba.excel.EasyExcel;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

// Writing Map data when no model class is available
List<Map<String, Object>> mapData = new ArrayList<>();
Map<String, Object> row1 = new HashMap<>();
row1.put("Name", "Alice");
row1.put("Age", 25);
row1.put("Email", "alice@example.com");
mapData.add(row1);

EasyExcel.write("map-data.xlsx")
    .sheet("MapData")
    .doWrite(mapData);

// Writing List<List<Object>> data
List<List<Object>> listData = new ArrayList<>();
listData.add(Arrays.asList("Name", "Age", "Email")); // Header
listData.add(Arrays.asList("Alice", 25, "alice@example.com"));
listData.add(Arrays.asList("Bob", 30, "bob@example.com"));

EasyExcel.write("list-data.xlsx")
    .sheet("ListData")
    .doWrite(listData);

Install with Tessl CLI

npx tessl i tessl/maven-com-alibaba--easyexcel

docs

annotations-and-mapping.md

data-conversion.md

event-system.md

exception-handling.md

index.md

reading-operations.md

writing-operations.md

tile.json