springboot整合easypoi实现简单的excel导出导入

1、pom.xml配置

org.jeecg

easypoi-base

2.3.1

org.jeecg

easypoi-web

2.3.1

org.jeecg

easypoi-annotation

2.3.1

2、导出功能

controller层:

@RequestMapping("/downloadExcel")

public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {

// 告诉浏览器用什么软件可以打开此文件

response.setHeader("content-Type", "application/vnd.ms-excel");

// 下载文件的默认名称

response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户数据表","UTF-8") + ".xls");

//编码

response.setCharacterEncoding("UTF-8");

List list = userService.findAllUser();//获得用户

Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, list);

workbook.write(response.getOutputStream());

}

service层:

@Override

public List findAllUser() {

return userMapper.findAllUser();

}

dao层:

3、导入功能

controller层:

@RequestMapping("/excelImport")

@ResponseBody

public String excelImport() {

ImportParams importParams = new ImportParams();

ImportParams params = new ImportParams();

params.setHeadRows(2);

 

try {

List objects = ExcelImportUtil.importExcel(new File("F:\\aa.xls"), User.class,

importParams);

userService.importUser(objects);

System.out.println(objects);

return "ok";

}catch (Exception e){

e.printStackTrace();

return "no";

 

}

 

}

service层:

/**

* 导入User

* @param objects

*/

@Override

public void importUser(List objects) {

 

for (User user : objects) {

userMapper.importUser(user);

}

 

 

}

dao层:

INSERT INTO t_user(name)

VALUES(#{name})

 

 

你可能感兴趣的:(springboot,poi,easypoi)