Home> Java> javaTutorial> body text

How to use POI to import and export Excel tables in Java

WBOY
Release: 2023-04-14 23:07:01
forward
2124 people have browsed it

1. Java uses POI to import and export Excel tables demo

1.Introduce dependencies

 org.apache.poi poi-ooxml 4.1.2 
Copy after login

2.Import demo

2.1 controller layer

/** * Excel导入 */ @PostMapping("/import") public Result userImport2(@RequestParam("file") MultipartFile file) throws Exception{ Result result=userService.userImportExcel(file); return result; }
Copy after login

2.2 service implementation class layer

public Result userImportExcel(MultipartFile file){ try { InputStream inputStream = file.getInputStream(); XSSFWorkbook sheets = new XSSFWorkbook(inputStream); //获取表单sheet 第一个 XSSFSheet sheetAt = sheets.getSheetAt(0); //获取第一行 int firstRowNum = sheetAt.getFirstRowNum(); //最后一行 int lastRowNum = sheetAt.getLastRowNum(); //存入数据集合 List users=new ArrayList<>(); //遍历数据 for(int i=firstRowNum+1;i
        
Copy after login

3. Export demo

3.1 controller layer

/** * 导出 * @param response * @return * @throws Exception */ @GetMapping("/export") public Result userExport2(HttpServletResponse response) throws Exception{ Result result=userService.userExportExcel(response); return result; }
Copy after login

3.2 service implementation class

public Result userExportExcel(HttpServletResponse response) { try { //创建excel XSSFWorkbook sheets = new XSSFWorkbook(); //创建行 XSSFSheet sheet = sheets.createSheet("用户信息"); //格式设置 XSSFCellStyle cellStyle = sheets.createCellStyle(); //横向居中 cellStyle.setAlignment(HorizontalAlignment.CENTER); //创建单元格第一列 XSSFRow row = sheet.createRow(0); //表头 this.titleExcel(row,cellStyle); //查询全部的用户数据 mybatis-plus List list = list(); //遍历设置值 for(int i=0;i
        
Copy after login

2. Hutool tool class encapsulation method export and import Excel

1.Introduce dependencies

Encapsulate poi into the tool class In the method

  cn.hutool hutool-all 5.7.20   org.apache.poi poi-ooxml 4.1.2 
Copy after login

2. Import demo

/** * Excel导入 */ @PostMapping("/import") public Result userImport(@RequestParam("file") MultipartFile file) throws Exception{ System.out.println(file.toString()); //InputStream inputStream = multipartFile.getInputStream(); InputStream inputStream = file.getInputStream(); ExcelReader reader = ExcelUtil.getReader(inputStream); //读取表的内容 List> list = reader.read(1); List users = new ArrayList<>(); for(List row : list){ User user = new User(); user.setUname(row.get(0).toString()); user.setUpassword(row.get(1).toString()); user.setUsex(row.get(2).toString()); user.setRole(row.get(3).toString()); user.setUlove(Integer.valueOf(row.get(4).toString())); user.setUphoto(row.get(5).toString()); user.setUaddress(row.get(6).toString()); users.add(user); } //批量插入用户信息 mybatis-plus userService.saveBatch(users); return Result.success(); }
        
Copy after login

3. Export demo

/** * Excel导出 方法一 */ @GetMapping("/export") public Result userExport(HttpServletResponse response) throws Exception{ //查询全部的用户数据 List list = userService.list(); //在内存里做操作,保存到浏览器 ExcelWriter writer = ExcelUtil.getWriter(true); //自定义标题别名 writer.addHeaderAlias("uname","用户名"); writer.addHeaderAlias("upassword","密码"); writer.addHeaderAlias("usex","性别"); writer.addHeaderAlias("role","角色"); writer.addHeaderAlias("ulove","爱心值"); writer.addHeaderAlias("uphoto","电话号码"); writer.addHeaderAlias("uaddress","地址"); //一次性写出list内的对象的Excel,使用默认样式,强制输出标题 writer.write(list,true); //设置浏览器响应格式 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); String filName= URLEncoder.encode("用户信息","UTF-8"); response.setHeader("Content-Disposition","attachment;filename="+filName+".xls"); ServletOutputStream outputStream=response.getOutputStream(); writer.flush(outputStream,true); outputStream.close(); writer.close(); return Result.success(); }
Copy after login

The above is the detailed content of How to use POI to import and export Excel tables in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!