Xcelite Framework: Basic Overview in the Java Class Library
The Xcelite framework is a tool that facilitates the Excel file in the Java class library.It is built based on the Apache Poi library and aims to simplify the creation, reading and writing operation of the Excel file.
The design goal of the Xcelite framework is to provide a lightweight and powerful API that enables developers to handle Excel files more flexible and efficiently.It provides a simpler and intuitive interface by encapsulating some complexity and details of the POI library.
Using the Xcelite framework, you can easily create a new Excel workbook, and add works meters and data in it.Below is a simple example to demonstrate how to use Xcelite to create a workheet containing data:
import com.googlecode.xcelite.annotate.Column;
import com.googlecode.xcelite.annotate.Row;
import com.googlecode.xcelite.annotations.HeaderRow;
import com.googlecode.xcelite.annotations.Sheet;
import com.googlecode.xcelite.converters.ColumnValueConverter;
import com.googlecode.xcelite.model.BeanSheetMapper;
import com.googlecode.xcelite.model.ContainerSheetReader;
import com.googlecode.xcelite.model.RowCallback;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class ExcelWriterExample {
public static void main(String[] args) throws IOException {
List<Person> persons = new ArrayList<>();
persons.add(new Person("John", 25));
persons.add(new Person("Alice", 30));
File file = new File("example.xlsx");
Xcelite xcelite = new Xcelite();
Sheet sheet = xcelite.createSheet("Persons");
sheet.setHeaderRowHeight(2);
sheet.setColWidth(0, 15);
sheet.setColWidth(1, 10);
BeanSheetMapper<Person> mapper = new BeanSheetMapper<>(Person.class);
sheet.setData(persons, mapper);
xcelite.write(file);
}
@Sheet(name = "Persons")
public static class Person {
@HeaderRow(name = "Name")
private String name;
@HeaderRow(name = "Age")
private int age;
// Getters and setters
}
}
Through the above code, we created a worksheet called "Persons" and added data from two personnel, namely "John" and "Alice", and their age was 25 and 30, respectively.We then write the data into the Excel file named "Example.xlsx".
In addition to creating and writing operations, the Xcelite framework also provides some convenient reading and analytical functions.You can read data from the Excel file in a similar way and convert it to the Java object.The following is a simple example:
public class ExcelReaderExample {
public static void main(String[] args) throws IOException {
File file = new File("example.xlsx");
Xcelite xcelite = new Xcelite(file);
Sheet sheet = xcelite.getSheet(0);
ContainerReader<Person> reader = new ContainerReader<>(sheet, Person.class);
reader.read();
List<Person> persons = reader.getList();
for (Person person : persons) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
The above code reads the data from the previously created "Example.xlsx" file and converts it into a Person object.We can then use the getlist () method to obtain a list containing all Person objects and print the name and age of each person.
In summary, the Xcelite framework provides a convenient and flexible way to operate the Excel file.Whether it is creation, reading, or writing operations, it can be implemented through it.By simplifying the use of the POI library, Xcelite makes the processing excel files simpler and efficient.