赞
踩
// An highlighted block @Override public File exportBuildings() { List<Building> buildings = buildingMapper.selectList(null); if (CollectionUtil.isEmpty(buildings)) { return null; } File outFile = new File("./" + "楼栋信息" + RandomUtil.randomString(6) + ".xls"); System.out.println(outFile.getAbsolutePath()); ExcelWriter writer = ExcelUtil.getWriter(outFile, "楼栋信息"); writer.setOnlyAlias(true); String[] rowAlias = new String[]{"序号", "楼栋编号", "楼栋名称", "楼栋简称", "所在校区", "用途", "楼层", "所属单位名称", "备注", "使用状态"}; String[] rowName = new String[]{"number", "buildCode", "buildName", "shortName", "schoolName", "usedFor", "countFloor", "deptBelong", "remark", "buildStatus"}; for (int i = 0; i < rowAlias.length; i++) { writer.addHeaderAlias(rowName[i], rowAlias[i]); //设置列宽 writer.setColumnWidth(i, 20); } writer.merge((rowAlias.length - 2), "楼栋信息"); List<ExportBuildingVo> buildingVos = buildings.stream().map(e -> { ExportBuildingVo exportBuildingVo = new ExportBuildingVo(); BeanUtil.copyProperties(e, exportBuildingVo); exportBuildingVo.setCountFloor(e.getUpFloor() + e.getDownFloor()); exportBuildingVo.setBuildStatus(e.getBuildStatus().equals("0") ? "停用" : "正常"); return exportBuildingVo; }).collect(Collectors.toList()); writer.write(buildingVos, true); //导出产生自增的序号 for (int i = 0; i < buildings.size(); i++) { writer.writeCellValue(0, 2, i + 1); } writer.autoSizeColumnAll(); //设置自动换行 writer.getCellStyle().setWrapText(true); writer.flush(); //关闭writer writer.close(); return outFile; }
中途遇到导出数据字段不完全的情况,正常情况下应该导出11条数据,实际只导出5条。
经检查后发现,每个字段的名称必须要与vo数据的字段名一一对应,我之前是与数据库中的字段名对应,造成字段减少
String[] rowName = new String[]{"number", "build_code", "build_name", "short_name", "school_name", "used_for",
"count_floor", "dept_belong", "remark", "build_status"};
在调整一下行宽,就好看了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。