1、数据导入导出是很多管理系统都需要的一项基本功能,相对于比较easyExcle和easyPOI,用过的可能都知道,easyPOI做导出那是真的方便还好用,你想要的姿势,‘她’都可以实现。话不多说上代码:先来单个sheet页导出(pom依赖官网就有,为什么要这样写呢,因为,官网有点吸引人,功能太多值得一看:http://doc.wupaas.com/docs/easypoi/easypoi-1c0u9dt0lqnpl)
注解实体参数: @Excel(name = "excle表头名", height = 20, width = 30) private String contructionStatusName; @Excel(name = "日期格式化", height = 20, width = 30, databaseFormat = "yyyy-MM-dd", format = "yyyy-MM-dd") private String enterFactoryTime; @Excel(name = 数字格式化", height = 20, width = 30, numFormat = "0.00") 保留两位小数 private BigDecimal num;
/** * 导出 * @param response * @param list 到处数据源 * @param fileName * @param object 注解类实体 */ public static void exportDefault(HttpServletResponse response, List<?> list, String fileName, Object object, String sheetName){ Workbook workbook = new XSSFWorkbook(); workbook = ExcelExportUtil.exportExcel(new ExportParams(fileName, sheetName), object.getClass(),list); if (workbook != null) { downLoadExcel(fileName, response, workbook); } }
/** * 功能描述:Excel导出 * * @param fileName 文件名称 * @param response 将流数据导出到前端 * @param workbook Excel对象 * @return */ private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) { try { response.setCharacterEncoding("UTF-8"); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLSX.getValue(), "UTF-8")); workbook.write(response.getOutputStream()); } catch (IOException e) { throw new RuntimeException(e); } }
接下来请关注多sheet页导出