-
-
-
cn.hutool -
hutool-all -
5.7.20 -
-
-
org.apache.poi -
poi-ooxml -
4.1.2 -
- package com.example.demo.demo1;
-
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.poi.excel.ExcelReader;
- import cn.hutool.poi.excel.ExcelUtil;
- import cn.hutool.poi.excel.ExcelWriter;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
-
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URLEncoder;
- import java.time.LocalDateTime;
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @Author: pengke
- * @Date: 2022/9/5 17:46
- * @Description:
- */
- @RestController
- @RequestMapping("/demo")
- public class Demo5 {
-
- //表格导出接口
- @RequestMapping("/export")
- public void export(HttpServletResponse response) throws IOException {
- //查询所有用户
- List
list= new ArrayList<>(); - list.add(new User("甲乙丙","123456","甲乙丙"));
- list.add(new User("路飞","123","monkey"));
- //在内存操作,写到浏览器
- ExcelWriter writer= ExcelUtil.getWriter(true);
-
- //自定义标题别名
- writer.addHeaderAlias("username","用户名");
- writer.addHeaderAlias("password","密码");
- writer.addHeaderAlias("nickname","昵称");
- //默认配置
- writer.write(list,true);
- //设置content—type
- response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
-
- //设置标题
- String fileName= URLEncoder.encode("用户信息","UTF-8");
- //Content-disposition是MIME协议的扩展,MIME协议指示MIME用户代理如何显示附加的文件。
- response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
- ServletOutputStream outputStream= response.getOutputStream();
-
- //将Writer刷新到OutPut
- writer.flush(outputStream,true);
- outputStream.close();
- writer.close();
- }
-
- /**
- * 导入excel
- * @param file
- */
- @PostMapping("/import")
- public String importExcel(MultipartFile file) throws IOException {
- //1.第一种 头必须和实体(英文)一样
- //文件处理成io流
- InputStream in = file.getInputStream();
- // //io流给ExcelReader
- ExcelReader excelReader=ExcelUtil.getReader(in);
- // //读取数据且转化为list
- // List
list = excelReader.readAll(User.class); -
- //2.第二种导入方式
- //忽略第一行头(第一行是中文的情况),直接读取表的内容
- List
> list = excelReader.read(1);
- List
listUser = CollUtil.newArrayList(); - for (List
- User user=new User();
- user.setUsername(row.get(0).toString());
- user.setPassword(row.get(1).toString());
- user.setNickname(row.get(2).toString());
- listUser.add(user);
- // ****类似一一对应****
- }
- //批量注册进数据库
- System.out.println(listUser);
- return listUser.toString();
- }
-
-
- }
- package com.example.demo.test1;
-
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.io.IoUtil;
- import cn.hutool.poi.excel.ExcelUtil;
- import cn.hutool.poi.excel.ExcelWriter;
- import com.example.demo.demo1.User;
- import org.apache.xmlbeans.XmlSimpleList;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.math.BigDecimal;
- import java.net.URLEncoder;
- import java.time.format.DateTimeFormatter;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * @Author: pengke
- * @Date: 2022/9/7 15:02
- * @Description:
- */
- @RestController
- @RequestMapping("/excel")
- public class ExcelController {
-
- @RequestMapping("/export")
- public String exportWaybill( HttpServletResponse response)throws IOException {
- ExcelWriter writer = ExcelUtil.getWriter();
- List
- List
userList = new ArrayList<>(); - userList.add(new User("悟空","123","大圣"));
- userList.add(new User("八戒","456","天蓬元帅"));
- userList.add(new User("沙僧","789","卷帘大将"));
- // 通过工具类创建writer,默认创建xls格式
- for (int i = 0; i < userList.size(); i++) {
- Map
map = new LinkedHashMap<>(); - map.put("名字", userList.get(i).getUsername());
- map.put("密码", userList.get(i).getPassword());
- map.put("昵称", userList.get(i).getNickname());
- rows.add(map);
- }
- //宽度自适应
- writer.setColumnWidth(-1, 22);
- // 合并单元格后的标题行,使用默认标题样式
- String name = URLEncoder.encode("西游记","UTF-8");
- writer.merge(2, "西游记");
- export(response, writer, rows, name);
- System.out.println("success");
- return "success";
-
- }
-
- private void export(HttpServletResponse response, ExcelWriter writer, List {
-
- writer.write(rows, true);
- response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml;charset=utf-8");
- response.setHeader("Content-Disposition", "attachment;filename=" + name + ".xls");
- ServletOutputStream out = null;
- try {
- out = response.getOutputStream();
- writer.flush(out, true);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- writer.close();
- IoUtil.close(out);
- }
- }
-
-
- }