Giter Site home page Giter Site logo

excel4j's Introduction

Excel4j

Java Library to read and write POJOs

Working of Excel4j

In order to identify what fields to write and read from Excel you need Provide meta-data. We use annotations to provide those meta-data to the Excel4j. Excel4j provide annotations:

@ExcelSheet to mark the Pojo class

// default sheet name will be the class name and sheet number is 0
@ExcelSheet(name = "", sheetNumber = 0)
public class Product {
}

@ExcelCell to mark fields of Class

import dev.archimedes.excel4j.annotations.ExcelCell;
import dev.archimedes.excel4j.annotations.ExcelSheet;

@ExcelSheet(name = "Product", sheetNumber = 1)
public class Product {
    @ExcelCell(name = "Product Name", cellNumber = 0)
    private String name;
}

Read Operation

import dev.archimedes.excel4j.options.ExcelOption;
import dev.archimedes.excel4j.processors.ExcelProcessor;
import dev.archimedes.excel4j.service.contracts.ExcelReader;
import dev.archimedes.entities.Product;

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

public class Main {
    public static void main(String[] args) {
        // create option to supply addition information to read and writer
        ExcelOption<Product> option = ExcelOption.<Product>builder()
                .with(Product.class) // with Java Object to read/write
                .sheetIndex(0) // default value is 0, but you can provide your own.
                .dateRegex("dd/MM/yyyy") // date format in Excel cell
                .listDelimiter(";") // if cell have value separated by delimiter, here ';'
                .start(5) // default 1, indicate the row number from where to start operation
                .build();

        // create the file object to refer your Excel File.

        File file = new File("your_file_location.xlsx");

        ExcelReader<Product> reader = ExcelProcessor.getReaderFromOption(option);

        // ExcelProcessor have static methods to return writer and reader instance

        // read the whole file
        List<Product> products = reader.read(file);
        
        // read one row at the given position (start in option)
        Product product = reader.readOne(file);
    }
}

Write Operation

import dev.archimedes.excel4j.options.ExcelOption;
import dev.archimedes.excel4j.processors.ExcelProcessor;
import dev.archimedes.excel4j.service.contracts.ExcelReader;
import dev.archimedes.excel4j.service.contracts.ExcelWriter;
import dev.archimedes.entities.Product;

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

public class Main {
    public static void main(String[] args) {
        ExcelOption<Product> option = ExcelOption.<Product>builder()
                .with(Product.class) // Java Pojo to write
                .start(1) // row index where write operation start.
                .dateRegex("dd/MM/yyyy")
                .sheetIndex(0) // sheet where ExcelWriter will write to.
                .overwrite(true) // overwrite the file if set true
                .build();

        ExcelWriter<Product> writer = ExcelProcessor.getWriterFromOption(option);

        File file = new File("product_data.xlsx");

        List<Product> products = List.of(
                Product.builder()
                        .name("Product 1")
                        .category("Juice")
                        .date(new Date())
                        .quantity(7)
                        .list(List.of("Something", "Another Thing", "Other Thing"))
                        .build(),

                Product.builder()
                        .name("Product 2")
                        .category("Cake")
                        .date(new Date())
                        .quantity(2)
                        .list(List.of("Other Thing", "Something", "Another Thing"))
                        .build(),

                Product.builder()
                        .name("Product 3")
                        .category("Cookie")
                        .date(new Date())
                        .quantity(5)
                        .list(List.of("Another Thing", "Something", "Other Thing"))
                        .build()
        );
        writer.write(file, products);
    }
}

Resolve Sheet name to write from ClassName

If no name parameter is specified in @ExcelSheet() Excel4j has a different approach to name the sheet.

import dev.archimedes.excel4j.annotations.ExcelSheet;

@ExcelSheet() // no name specified, sheet number default to 0
public class ProductDescription {
}

In this case sheet name will be 'Product Description'

This behavior is due to the Approach of naming classes based on 'CamelCase' Convention

If your class name does not follow 'CamelCase' then do provide the name value,

excel4j's People

Contributors

ayu-sh-kr avatar

Stargazers

 avatar deepanshu agarwal avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.