Poi和EasyPoi具有相同的功能,都是Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能,本篇文章中主要介绍利用这两种技术对对数据进行导入(将Excel表格中的数据导入到数据库中)和导出(将数据库中的数据导入到Excel表格中)。
<--------------------------------- EasyPoi:--------------------------------------------->
EasyPoi的作者这样说:
Easypoi 为谁而开发
不太熟悉poi的
不想写太多重复太多的
只是简单的导入导出的
喜欢使用模板的
从实用的角度出发,我们先介绍EasyPoi的使用:
<dependency>
<groupId>cn.afterturngroupId>
<artifactId>easypoi-baseartifactId>
<version>4.0.0version>
dependency>
<dependency>
<groupId>cn.afterturngroupId>
<artifactId>easypoi-webartifactId>
<version>4.0.0version>
dependency>
<dependency>
<groupId>cn.afterturngroupId>
<artifactId>easypoi-annotationartifactId>
<version>4.0.0version>
dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@ExcelTarget(value="student")
public class Student {
@Excel(name="ID")
private String id;
@Excel(name = "学生姓名")
private String name;
@Excel(name = "学生年龄")
private String age;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@ExcelTarget(value = "teacher")
public class Teacher {
@ExcelIgnore
private String id;
@Excel(name = "老师姓名",needMerge = true)
private String name;
@ExcelCollection(name = "计算机学生")
private List<Student> students;
}
@Test
public void testOut() throws IOException {
//创建学生集合,模拟数据库查询的结果
List<Student> stus = new ArrayList<Student>();
stus.add(new Student("1", "xxx", "12"));
stus.add(new Student("2", "***", "23"));
stus.add(new Student("3", "$$$", "33"));
stus.add(new Student("4", "&&&", "18"));
//创建老师集合
Teacher teacher = new Teacher("1","刘火火",stus);
List<Teacher> teachers = new ArrayList<Teacher>();
teachers.add(teacher);
//参数:(一级标题,二级标题,表名),实体类类对象,导出的集合
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生", "632宿舍", "计算机学院宿舍信息表"),
Teacher.class, teachers);
workbook.write(new FileOutputStream(new File("D:/a.xls")));
//释放资源
workbook.close();
}
public class TestEasyPoiInput {
public static void main(String[] args) throws Exception {
//创建导入对象
ImportParams params = new ImportParams();
params.setTitleRows(1); //表格标题行数,默认0
params.setHeadRows(2); //表头行数,默认1
//获取导入数据
List<Teacher> teachers = ExcelImportUtil.importExcel(new FileInputStream(new File("D:/TestEasyPoi.xls")),Teacher.class, params);
for (Teacher teacher : teachers) {
System.out.println(teacher);
}
}
}
<--------------------------------- Poi:--------------------------------------------->
EasyPoi实际上就是大牛对于Poi封装的一个产物,其特点就是使用起来简单、容易上手。但是封装之后带来的缺点就是灵活使用性不如以前。因此这里对于Poi也进行说明。
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>3.15version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>4.0.1version>
dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Banner {
private String id;
private String title;
private String img_path;
private String status;
private Date up_date;
private String description;
}
@Test
public void testPoiOut(){
/*一、数据准备和表头设置*/
//查询数据库集合
List<Banner> banners = bannerDAO.findByPage(0, 7);
//创建workbook对象
HSSFWorkbook workbook = new HSSFWorkbook();
//创建sheet对象
HSSFSheet sheet = workbook.createSheet("banner信息表");
//创建标题 合并行
HSSFRow row = sheet.createRow(0);
//创建单元格
HSSFCell cell = row.createCell(0);
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 5);
sheet.addMergedRegion(cellRangeAddress);
//给标题添加居中样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cell.setCellStyle(cellStyle);
//给表起个名字
cell.setCellValue("轮播图信息表");
/*二、设置格式*/
//创建日期格式对象
HSSFDataFormat dataFormat = workbook.createDataFormat();
short format = dataFormat.getFormat("yyyy-MM-dd");
//创建样式对象
HSSFCellStyle dataStyle = workbook.createCellStyle();
dataStyle.setDataFormat(format);
//设置列宽
sheet.setColumnWidth(4, 20*256);
//设置居中
HSSFCellStyle fontstyle = workbook.createCellStyle();
fontstyle.setAlignment(HorizontalAlignment.CENTER);
fontstyle.setVerticalAlignment(VerticalAlignment.CENTER);
//设置字体
HSSFFont font = workbook.createFont();
font.setBold(true);
font.setColor(Font.COLOR_RED);
font.setItalic(true);
font.setFontName("楷体");
font.setFontHeightInPoints((short)10);
fontstyle.setFont(font);
/*三、创建表格列属性*/
HSSFRow row1 = sheet.createRow(1);
String[] title = {
"ID","标题","轮播图片","状态","更新时间","描述"};
for (int i = 0; i < title.length; i++) {
HSSFCell cell1 = row1.createCell(i);
cell1.setCellStyle(fontstyle);
cell1.setCellValue(title[i]);
}
/*四、填充表格信息*/
for (int i = 0; i < banners.size(); i++) {
HSSFRow rowi = sheet.createRow(i + 2);
rowi.createCell(0).setCellValue(banners.get(i).getId());
rowi.createCell(1).setCellValue(banners.get(i).getTitle());
rowi.createCell(2).setCellValue(banners.get(i).getImg_path());
rowi.createCell(3).setCellValue(banners.get(i).getStatus());
//设置日期格式:将日期格式样式设置到cell中
HSSFCell cell1 = rowi.createCell(4);
cell1.setCellStyle(dataStyle);
cell1.setCellValue(banners.get(i).getUp_date());
rowi.createCell(5).setCellValue(banners.get(i).getDescription());
}
/*五、导出单元格*/
try {
//指定文件导出的路径
workbook.write(new File("D://轮播图信息表.xls"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testPoiInt() throws IOException {
//获取要导入的文件
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File("D://轮播图信息表.xls")));
//获取工作簿
HSSFSheet sheet = workbook.getSheet("banner信息表");
for (int i = 2; i < sheet.getLastRowNum(); i++) {
//获取行数据
HSSFRow row = sheet.getRow(i);
//将表格中的行数据封装到banner对象中
Banner banner = new Banner();
banner.setId(row.getCell(0).getStringCellValue());
banner.setTitle(row.getCell(1).getStringCellValue());
banner.setImg_path(row.getCell(2).getStringCellValue());
banner.setStatus(row.getCell(3).getStringCellValue());
banner.setUp_date(row.getCell(4).getDateCellValue());
banner.setDescription(row.getCell(5).getStringCellValue());
//调用插入的dao方法
bannerDAO.save(banner);
}
//关闭资源
workbook.close();
}