EasyPoi笔记
EasyPoi
介绍:EasyPOI对POI进行了优化,更加设计精巧,使用简单,接口丰富,扩展简单。EasyPOI的同类产品有Execel4J,Hutools等。官网地址是 https://gitee.com/lemur/easypoi
文档:https://gitee.com/lemur/easypoi/blob/master/basedemo.md#easypoi导出实例
测试Demo(动态列Map方式):
import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFSheet; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class EasyPoiExcelUtil { public void exportExcel(HttpServletResponse response) throws IOException { List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>(); entity.add(new ExcelExportEntity("姓名姓名姓名姓名姓名姓名姓名9089", "name")); entity.add(new ExcelExportEntity("性别", "sex")); entity.add(new ExcelExportEntity("微信", "sex1")); entity.add(new ExcelExportEntity("电话", "sex2")); entity.add(new ExcelExportEntity("备注备注备注备注备注备注备注备注备注备注备注备注备注", "sex3")); ExcelExportEntity szColGroup = new ExcelExportEntity("标题1", "sz"); List<ExcelExportEntity> szColList = new ArrayList<ExcelExportEntity>(); szColList.add(new ExcelExportEntity("A", "sz_xc")); szColList.add(new ExcelExportEntity("B", "sz_jydwd")); szColGroup.setList(szColList); entity.add(szColGroup); ExcelExportEntity jdColGroup = new ExcelExportEntity("标题2", "jd"); List<ExcelExportEntity> jdColList = new ArrayList<ExcelExportEntity>(); jdColList.add(new ExcelExportEntity("A", "jd_xc")); jdColList.add(new ExcelExportEntity("B", "jd_jydwd")); jdColGroup.setList(jdColList); entity.add(jdColGroup); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map; Map<String, Object> szmap; List<Map<String, Object>> szDetailList = new ArrayList<Map<String, Object>>(); Map<String, Object> jdmap; List<Map<String, Object>> jdDetailList = new ArrayList<Map<String, Object>>(); for (int i = 0; i < 10; i++) { map = new HashMap<String, Object>(); map.put("name", "1" + i); map.put("sex", "2" + i); map.put("sex1", "2" + i); map.put("sex2", "2" + i); map.put("sex3", "2" + i); szmap = new HashMap<String, Object>(); szmap.put("sz_xc", "2" + i); szmap.put("sz_jydwd", "2" + i); szDetailList.add(szmap); map.put("sz",szDetailList); jdmap = new HashMap<String, Object>(); jdmap.put("jd_xc", "2" + i); jdmap.put("jd_jydwd", "2" + i); jdDetailList.add(jdmap); map.put("jd",jdDetailList); list.add(map); } //设置信息头,告诉浏览器内容为excel类型 response.setHeader("content-Type", "application/vnd.ms-excel"); //文件名称 String fileName = "学生信息表.xls"; //sheet名称 String sheetName = "学生列表"; fileName = new String(fileName.getBytes(), "ISO-8859-1"); //设置下载名称 response.setHeader("Content-Disposition", "attachment;filename=" + fileName); //字节流输出 ServletOutputStream out = response.getOutputStream(); //设置excel参数 ExportParams params = new ExportParams(); //设置sheet名 params.setSheetName(sheetName); //设置标题 params.setTitle("学生信息表"); HSSFWorkbook workbook = (HSSFWorkbook) ExcelExportUtil.exportExcel(new ExportParams( "我是title", "测试"), entity, list); // 自动列宽 HSSFSheet sheet=workbook.getSheetAt(0); for (int i = 0; i < entity.size(); i++) { sheet.autoSizeColumn(i); sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10); // 中文宽度 int minWidth = entity.get(i).getName().length() * 600; if (sheet.getColumnWidth(i) < minWidth) sheet.setColumnWidth(i, minWidth); // 无数据额外处理 } //写入 workbook.write(out); } }
待补充...
作者:chenze 出处:https://www.cnblogs.com/chenze-Index/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |