Hutool的Office操作
昨天听到我们头说让其他人完成一个将数据导出为excel的功能,我一开始想的是poi实现,然后问了一下发现我们公司用的是一个叫HuTool的工具类,我查看了一下官方文档,这个工具类实现的功能特别多,此次先说一下poi的操作,其他的有时间我们再聊一聊:
官方文档地址:https://www.hutool.cn/docs/#/poi/Excel生成-ExcelWriter
我做的Demo的码云地址:https://gitee.com/zym213/hutool_poi
首先引入依赖:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
编写测试类:
DownloadUtil:
package com.zym.testpoi.util;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class DownloadUtil {
public static void download(List<Map<String,Object>> list,HttpServletResponse response) throws IOException {
String path = System.getProperty("java.io.tmpdir") + UUID.randomUUID() + ".xlsx";
File file = new File(path);//在系统的临时目录生成一个excel
ExcelWriter writer = ExcelUtil.getWriter();
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(list,true);
//response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
ServletOutputStream out=response.getOutputStream();
writer.flush(out, true);
writer.close();
//此处记得关闭输出Servlet流
IoUtil.close(out);
// 终止后删除临时文件
file.deleteOnExit();
}
}
User:
package com.zym.testpoi.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String username;
private int age;
private String hobby;
}
UserController:
package com.zym.testpoi.controller;
import com.zym.testpoi.pojo.User;
import com.zym.testpoi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/download")
public void download(HttpServletResponse response) throws IOException {
List<User> list = new ArrayList<>();
list.add(new User("zhangsan",23,"football"));
list.add(new User("lisi",24,"song"));
list.add(new User("wangwu",25,"computergame"));
userService.download(list,response);
}
}
UserServiceImpl:
package com.zym.testpoi.service.impl;
import com.zym.testpoi.pojo.User;
import com.zym.testpoi.service.UserService;
import com.zym.testpoi.util.DownloadUtil;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {
@Override
public void download(List<User> list, HttpServletResponse response) throws IOException {
List<Map<String,Object>> downloadlist = new ArrayList<>();
for (User user : list) {
Map<String,Object> map = new LinkedHashMap<>();
map.put("用户名",user.getUsername());
map.put("年龄",user.getAge());
map.put("爱好",user.getHobby());
downloadlist.add(map);
}
DownloadUtil.download(downloadlist,response);
}
}
然后我们启动项目,调用一下download这个接口:
浏览器弹出了下载窗口,下载下来看一下是什么样子:
虽然样式丑了点,但功能实现了,接下来就是对照官方文档修改你自己想要的样式了