前言
最近在开发一个管理后台,有一些excel的导出和导入操作,以前都是使用POI操作excel,这一次尝试了别的人组件:easyExcel和Hutool,今天就来分享一下,这三种方式中POI操作导出excel。
使用POI导出2007格式的excel文件。
pom.xml
使用的是poi的3.13版本,此方法适用于老项目(使用时注意jar包冲突!)
org.apache.poipoi3.13org.apache.poipoi-ooxml3.13org.apache.poipoi-ooxml-schemas3.13`
// 在没有掌握这个方法之前,请不要擅自更改集合的key!!!!!!
集合准备:
// 封装 item 的集合 (数据最终封装的集合)Map data = new HashMap();// 封装 dataMap List> item = new ArrayList>();// 要导出的数据 map 集合(标题 + 数据)Map dataMap = new HashMap();// 标题 list 集合List title = new ArrayList();// 数据 list 集合List> varList = new ArrayList>();// 每一行数据的map集合 Map vpd = new HashMap(32);
数据准备
class Father{ private String name; private int age; private String address;}List> list = new ArrayList<>();for (int i = 0; i < 10; i++) { Father fa = new Father(); fa.setName("姓名是:"+ i); fa.setAge(i); fa.setAddress("地址:"+ i);list.add(fa);}标题列List title = new ArrayList();title.add("姓名");// 标题列1title.add("年龄");// 标题列2title.add("地址");// 标题列3dataMap.put("titles", title);
取出数据
for (int i = 0; i < list.size(); i++) { Map vpd = new HashMap(32); // 请注意,有多少列,var按照顺序进行排列 vpd.put("var1", list.get(i).getName()); vpd.put("var2", list.get(i).getAge()); vpd.put("var2", list.get(i).getAddress()); // 将每一行的数据,追加到集合 varList中。 varList.add(vpd);} 封装数据集dataMap.put("varList", varList);item.add(dataMap);// 最终封装的 导出的excel 的数据集data.put("data", item);
调用导出工具类,进行导出(ExportExcel 实体类)
ExportExcel erv = new ExportExcel(); // 执行excel操作String fileName = "XXXXXXXXXXXXXXXXXXXXX";// 方法一:// 因为要将excel 结果返回到浏览器中,所以要把response传递到工具类中。erv.excelViewPoi(fileName, data, response);// 方法二:// 也可以 设置 @RequestMapping 的参数produces = "application/vnd.ms-excel;charset=UTF-8" 服务端通知客户端,响应的数据类型为excel
ExportExcel(导出excel 工具类)
package liren.office;import java.io.IOException;import java.io.OutputStream;import java.util.List;import java.util.Map;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.xssf.streaming.SXSSFCell;import org.apache.poi.xssf.streaming.SXSSFRow;import org.apache.poi.xssf.streaming.SXSSFSheet;import org.apache.poi.xssf.streaming.SXSSFWorkbook;/*** 导入到EXCEL* 类名称:ExportExcel .java* 类描述:导出2007格式的excel* @author LiRen* @version 1.0 */public class ExportExcel {public void excelViewPoi(String fileName,Map model, HttpServletResponse response) {List> list = (List>)model.get("data");SXSSFWorkbook workbook = new SXSSFWorkbook(10000);for (Map map : list) {SXSSFSheet sheet;SXSSFCell cell;sheet = workbook.createSheet(String.valueOf(map.get("sheet")));List titles = (List) map.get("titles");int len = titles.size(); CellStyle headerStyle = workbook.createCellStyle(); //标题样式headerStyle.setAlignment(CellStyle.ALIGN_CENTER);headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);Font headerFont = workbook.createFont();//标题字体headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);headerFont.setFontHeightInPoints((short)11);headerStyle.setFont(headerFont);short width = 20,height=25*20;sheet.setDefaultColumnWidth(width);for(int i=0; i> varList = (List>) map.get("varList");int varCount = varList.size();for(int i=0; i vpd = varList.get(i);for(int j=0;j
