Parser des fichiers XlSX ou XLS avec des annotations à l'aide de la bibliothèque Apache POI
Annotation | Microsoft Excel |
---|---|
Book | WorkBook |
Page | Sheet |
Box | Cell |
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(name = "Sheet1")
public class Exemple {
}
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
}
ParseExcel.clazz(<Class>)
.file(<File xls or xlsx>)
.build();
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();
}
}
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();
}
}
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 |
Permet de convertir une liste d'entities en un fichier Excel.
Il ne peut créer d'un seul onglet
- 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();
}
}