anot-excel helps in creating excel sheet by just passing a list of objects. The data in the list will be compared with annotations in the class to create excel. Configurations currently supported are filename, names of columns, position of columns, sorting and grouping the columns. Consider the following class. (More examples are provided in code)
@Excel(name = "sortAndGroup")
public class SortAndGroupVo {
@ExcelColumn(name="Name", position=3, sort = true, sortPriority=2)
String studentName;
@ExcelColumn(name="Year", position=2, sort = true, sortPriority=1)
Integer year;
@ExcelColumn(name="School",position=1,group=true, sort = true)
String school;
}
Meaning of Annotations
-
@Excel
- Specifies that objects of this class is used for excel export.
- Parameter name specifies the name of excel file.
-
@ExcelColumn
- Marks the data member as a column in excel.
- Parameter 'name' specifies the name of the column. This is a Mandatory param.
- 'position' specifies the position of the column from left. This is a Mandatory param.
- 'sort' specifies whether that column should be sorted in excel. This is Optional.
- 'sortPriority' is used when multiple columns need to be sorted. In the above example, rows will be sorted by year first as it has higher priority(smaller the number, higher will be the priority). But if two years are same, name will be used to sort the rows. Default sortPriority is 0.
After creating a list of objects based on above class, i.e SortAndGroupVo, pass it to the generator to get the excel.
List list = new ArrayList();
list.add(new SortAndGroupVo("Abc", 9, "Oxford"));
list.add(new SortAndGroupVo("Def", 5, "Oxford"));
list.add(new SortAndGroupVo("Ghi", 1, "Oxford"));
list.add(new SortAndGroupVo("Jkl", 5, "Harvard"));
list.add(new SortAndGroupVo("Mno", 1, "Harvard"));
AnotExcelGenerator simpleExcelGenerator = new AnotExcelGenerator();
simpleExcelGenerator.generateExcel(list);
Excel obtained :
School | Year | Name |
Harvard | 1 | Mno |
5 | Jkl | |
Oxford | 1 | Ghi |
5 | Def | |
9 | Abc |