Skip to content

XlSX or XLS Parser with custom annotations using Apache POI Library

Notifications You must be signed in to change notification settings

stef38-code/ApiExcel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

APIExcel

Description

Parser des fichiers XlSX ou XLS avec des annotations à l'aide de la bibliothèque Apache POI

Annotations

Annotation Microsoft Excel
Book WorkBook
Page Sheet
Box Cell

Definition

Book

import org.api.excel.core.annotations.Book;
import org.api.excel.core.annotations.Page;

@Book(value = {
        @Page(name = "Sheet1"),
        @Page(name = "Sheet2", rowNumber = 9),
        @Page(number = 2)
})
class Exemple {

}

Page

@Page(name = "Sheet1")
public class Exemple {

}

Box

public class Personne2 {
    @Box(stringFormat = true)
    private String name;
    @Box(name = "company")
    private String company;
    @Box(number = 2)
    private String address;
    @Box(number = 3, stringFormat = true, name ="postalZip")
    private String pZip;
    @Box(number = 4)
    private String city;
    @Box(number = 5)
    private String guid;
// Add getter and setter
}

Execution

Reader

ParseExcel.clazz(<Class>)
                .file(<File xls or xlsx>)
                .build();
Un fichier
class SimpleFile {
    public static void main(String[] args) {
        String excelFile = FileUtil.getAbsolutePath("personnes.xls");
        Optional<List<Personne>> optional = ParseExcel.clazz(Personne.class)
                .file(excelFile)
                .build();
    }
}
Plusieurs fichiers
class MultiFile {
    public static void main(String[] args) {

        String fileXls = FileUtil.getAbsolutePath("personnes.xls");
        String fileXlsx = FileUtil.getAbsolutePath("personnes.xlsx");

        Optional<List<Personne>> optional = ParseExcel.clazz(Personne.class)
                .file(fileXls)
                .file(fileXlsx)
                .build();
    }
}

Tests

src TU Description
Empty file Test sur des fichiers vide ou sans lignes
sample Parse simple ou multi-fichiers
Multi Sheet Parse un fichier mais plusieurs onglets
Bench Mark Parse 3 fichier 10000 lignes,100000 lignes et 1000000 lignes

Writer

Permet de convertir une liste d'entities en un fichier Excel.

Il ne peut créer d'un seul onglet

Exemple

ref

  • Entité
@Page(name = "Feuil1")
public class Personne {

    @Box(name = "name")
    private String name;
    @Box(number = 1, name = "company")
    private String company;
    @Box(number = 2, name = "address")
    private String address;
    @Box(number = 3, name = "postalZip")
    private String postalZip;
    @Box(number = 4, name = "city")
    private String city;
    @Box(number = 5, name = "guid")
    private String guid;
    //setter and getter
}
  • Exécution
class main {
    public static void main(String[] args) {
        ParseExcel.write(Personne .class).
                file(file).
                entities(personnes).
                build();
    }
}

Ressources

java-builder-pattern-tricks