使用 Excel 进行报表的导入导出,首先下载相关的 jar 和 excel util。
把相应的 jar 和 util 类放到项目中就可以在需要打印出来的实体类的get方法加上注解@ExcelField
Excel 报表数据模板下载代码:
controller层:
/**
* 下载导入Excel表信息数据模板
*/
@RequiresPermissions("metermessage:meterMessage:view")
@RequestMapping(value = "import/template")
public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes, String houseType) {
try {
meterMessageService.importFileTemplate(response);
return null;
} catch (Exception e) {
addMessage(redirectAttributes, "导入模板下载失败!失败信息:" + e.getMessage());
}
return "redirect:" + Global.getAdminPath() + "/metermessage/meterMessage/?repage&houseType=" + houseType;
}
service层:
//Excel表信息数据模板
public void importFileTemplate(HttpServletResponse response) throws IOException {
String fileName = "Excel表信息数据导入模板.xlsx";
List<MeterMessage> list = Lists.newArrayList();
MeterMessage meter = new MeterMessage();
meter.setOfficeName("*****");
meter.setUnitNumber("1");
meter.setFloor("1");
meter.setRoomNumber("01");
meter.setMeterNo("001");
meter.setRate(new Double(10));
meter.setMeterTypeLabel("水表");
list.add(meter);
new ExportExcel("Excel表信息数据", MeterMessage.class, 2).setDataList(list).write(response, fileName).dispose();
}
Excel表信息导入代码:
controller层:
/**
* 导入Excel信息数据
*/
@RequiresPermissions("metermessage:meterMessage:edit")
@RequestMapping(value = "import", method = RequestMethod.POST)
public String importFile(MultipartFile file, RedirectAttributes redirectAttributes, String houseType) {
if (Global.isDemoMode()) {
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + Global.getAdminPath() + "/metermessage/meterMessage/?repage&houseType=" + houseType;
}
try {
String message = meterMessageService.importFile(file);
addMessage(redirectAttributes, message);
} catch (Exception e) {
addMessage(redirectAttributes, "导入Excel信息失败!失败信息:" + e.getMessage());
}
return "redirect:" + Global.getAdminPath() + "/metermessage/meterMessage/?repage&houseType=" + houseType;
}
service层:
//导入excel信息数据
@Transactional(readOnly = false)
public String importFile(MultipartFile file) throws Exception {
int successNum = 0;
int failureNum = 0;
int row = 3;
StringBuilder failureMsg = new StringBuilder();
ImportExcel ei = new ImportExcel(file, 1, 0);
List<MeterMessage> list = ei.getDataList(MeterMessage.class);
Map<String, Object> map = new HashMap<String, Object>();// 去重复查询用
for (int i = 0; i < list.size(); i++) {
MeterMessage meter = list.get(i);
String officeName = meter.getOfficeName();
String unitNumber = meter.getUnitNumber();
String floor = meter.getFloor();
String roomNumber = meter.getRoomNumber();
String meterTypeLabel = meter.getMeterTypeLabel();
String meterNo = meter.getMeterNo();
Double rate = meter.getRate();
if (StringUtils.isNoneBlank(officeName)) {
if (!map.containsKey(officeName)) {
List<Office> officeList = officeDao.findOfficeByName(officeName);
if (officeList != null && officeList.size() > 0) {
meter.setOffice(officeList.get(0));
map.put(officeName, officeList.get(0));
} else {
failureMsg.append("<br/>第" + row + "行数据:小区' " + officeName
+ "' 不存在,请先去'平台设置-->单位管理'创建该小区 ;");
failureNum++;
}
} else {
meter.setOffice((Office) (map.get(officeName)));
}
} else {
failureMsg.append("<br/>第" + row + "行数据:小区 不可为空 ");
failureNum++;
}
String officeId = meter.getOffice().getId();
if (StringUtils.isNoneBlank(officeId) && StringUtils.isNoneBlank(unitNumber) && StringUtils.isNoneBlank(floor) && StringUtils.isNoneBlank(roomNumber)) {
if(!map.containsKey(officeId+unitNumber+floor+roomNumber)) {
HouseInformation house = houseInformationDao.findHouseIdByCondition(officeId, unitNumber, floor, roomNumber);
if(house != null) {
meter.setHouse(house);
map.put(officeId+unitNumber+floor+roomNumber, house);
} else {
failureMsg.append("<br/>第" + row + "行数据:房屋' " + officeName
+ "' 不存在;");
failureNum++;
}
} else {
meter.setHouse((HouseInformation) (map.get(officeId+unitNumber+floor+roomNumber)));
}
} else {
failureMsg.append("<br/>第" + row + "行数据:房屋 不可为空 ");
failureNum++;
}
if (StringUtils.isNoneBlank(meterTypeLabel)) {
if(!map.containsKey(meterTypeLabel)) {
String meterType = DictUtils.getDictValue(meterTypeLabel, "meter_type", "1");
if (StringUtils.isNoneBlank(meterType)) {
meter.setMeterType(meterType);
map.put(meterTypeLabel, meterType);
} else {
failureMsg.append("<br/>第" + row + "行数据:仪表类型 '" + meterTypeLabel
+ "' 不存在,请先去'平台设置-->系统设置-->字典管理'创建该仪表类型 ");
failureNum++;
}
} else {
meter.setMeterType((String)(map.get(meterTypeLabel)));
}
} else {
failureMsg.append("<br/>第" + row + "行数据:仪表类型 不可为空 ");
failureNum++;
}
if (StringUtils.isNoneBlank(meterNo)) {
meter.setMeterNo(meterNo);
} else {
failureMsg.append("<br/>第" + row + "行数据:仪表编号 不可为空 ");
failureNum++;
}
if (rate != null) {
meter.setRate(rate);
} else {
failureMsg.append("<br/>第" + row + "行数据:倍率 不可为空 ");
failureNum++;
}
meter.preInsert();
row++;
}
if (failureNum > 0) {
failureMsg.insert(0, ",失败 " + failureNum + " 条Excel信息,导入信息如下:");
} else {
try {
if (list != null && list.size() > 0) {
dao.insertMeterList(list);
successNum = list.size();
}
} catch (ConstraintViolationException ex) {
failureMsg.append("<br/>excel信息 导入失败:");
List<String> messageList = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
for (String message : messageList) {
failureMsg.append(message + "; ");
failureNum++;
}
} catch (Exception ex) {
failureNum++;
failureMsg.append("<br/>excel信息 导入失败:" + ex.getMessage());
}
}
return "已成功导入 " + successNum + " 条excel信息" + failureMsg;
}