Excel tools,对excel封装让excel导出更简单!
1、 新建excel导出。
Excel excel = new Excel(); //新建excel
ExcelSheet sheet = excel.createSheet(); //新建sheet
sheet.row(0).cell(2).cellValue("1"); //调用cellValue(),设置excel样式
sheet.row(1).cell(2).cellValue("2");
excel.saveExcel("c://test1.xlsx"); //存储excel
2、 调用模板导出。
Excel excel = new Excel("c://test1.xlsx");
ExcelSheet sheet = excel.getSheet(); //默认获取第一个工作簿
sheet.row(0).cell(2).cellValue("111111111"); //设置excel value值
excel.saveExcel("c://test2.xlsx");
3、 entity list通过注解导出。
/**
* 学生 excel测试
*/
public class Student {
private static final long serialVersionUID = -4026917215285783232L;
@ExcelField(title = "姓名",sort = 1)
private String name;
@ExcelField(title = "学校" ,sort = 3)
private String school;
@ExcelField(title = "年龄",sort=2)
private Integer age;
@ExcelField(title = "入学时间",sort = 4)
private Date joinDate;
public Student() {}
//set/get 方法省略。
.....
}
/**
* 初始化数据
* @return
*/
static List<Student> init(){
List<Student> list = new ArrayList<>();
Student st1 = new Student("tom","huax",10);
Student st2 = new Student("tom","huax",10);
Student st3 = new Student("tom","huax",10);
list.add(st1);
list.add(st2);
list.add(st3);
//list.forEach(s->System.out.println(s));
return list;
}
/**
* 模板指定位置 list数据循环导出(需要entity注解)
* @throws IOException
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
@Test
public void myExcel() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Excel excel = new Excel("c://student_temp.xls");
ExcelSheet sheet = excel.getSheet();
sheet.setDateList(init(), 2, 0); //此处2,0位置为row,cell起始位置
excel.saveExcel("c://student_temp_rs.xlsx");
}
4.基于注解导出excel
Excel excel = new Excel();
ExcelSheet sheet = excel.createSheet();
sheet.title("学生统计表"); //设置excel title名称(可不设)
sheet.header(Student.class).setData(init()); //设置 data
excel.saveExcel("c://student_annotation.xlsx");
Excel excel = new Excel();
ExcelSheet sheet = excel.createSheet();
//获取excel样式
Map<String, CellStyle> styles = createStyles(excel.getWorkbook());
sheet.title("学生统计表").cellStyle(styles.get("title")); //设置title 以及样式
sheet.header(Student.class, styles.get("header")) //设置hear 以及样式
.setData(init()).cellStyle(styles.get("data")); //设置data 样式
excel.saveExcel("c://student_annotation.xlsx");
5.excel导入 (测试导入上图内容)
Excel excel = new Excel("c://student_annotation.xlsx");
ExcelSheet sheet = excel.getSheet();
List<Map<String, String>> list = sheet.getList(1, 0).toMap(); //1,0(为起始位置,从header开始算起)核心方法
list.forEach(map -> System.out.println(map));
{姓名=tom, 年龄=10, 学校=huax, 入学时间=2017-08-15}
{姓名=tom, 年龄=10, 学校=huax, 入学时间=2017-08-15}
{姓名=tom, 年龄=10, 学校=huax, 入学时间=2017-08-15}
List<Map<String, String>> list = sheet.getList(1, 0).toMap4Annotation(Student.class);
{name=tom, age=10, school=huax, joinDate=2017-08-15}
{name=tom, age=10, school=huax, joinDate=2017-08-15}
{name=tom, age=10, school=huax, joinDate=2017-08-15}
String keyValue = "姓名:name,学校:school,年龄:age,入学时间:joinDate";
List<Map<String, String>> list = sheet.getList(1, 0,keyValue).toMap();
6.excel导入映射至对象
List<Student> list = sheet.getList(1, 0).toObject(Student.class);
String keyValue = "姓名:name,学校:school,年龄:age,入学时间:joinDate";
List<Student> list = sheet.getList(1, 0,keyValue).toObject(Student.class);
Student{name='tom', school='huax', age=10, joinDate=Tue Aug 15 00:00:00 CST 2017}
Student{name='tom', school='huax', age=10, joinDate=Tue Aug 15 00:00:00 CST 2017}
Student{name='tom', school='huax', age=10, joinDate=Tue Aug 15 00:00:00 CST 2017}
7.获取单条数据
String title = sheet.getRow(0).getCell(0).getCellValue(); //根据指定位置获取数据,统一为String
System.out.println(title);
学生统计表
8.excel导出样式优化,简化单元格样式设置。
//设置样式
CellStyle cellStyle = ExcelStyle.builder(excel.getWorkbook())
.align(HSSFCellStyle.ALIGN_CENTER) //设置居中
.fondFamily("宋体") //设置字体
.fondSize((short) 12) //设置字体大小
.fondWeight((short) 10) //加粗
.border(ExcelStyle.BORDER_TOP, ExcelStyle.BORDER_LEFT, ExcelStyle.BORDER_BOTTOM, ExcelStyle.BORDER_RIGHT) //设置表格边框
.build();
9.excel 推荐快速导出
Excel excel = new Excel();
ExcelSheet sheet = excel.createSheet();
sheet.header("姓名,年龄,学校,日期", 0, 0).cellStyle(ExcelStyle.getCommHeader(excel.getWorkbook()));
sheet.setDateList(init(), 1, 0).cellStyle(ExcelStyle.getCommData(excel.getWorkbook()));
excel.saveExcel("c://fast_export_excel.xlsx");