Commit 884ca37e authored by 田涛's avatar 田涛

导入导出使用easypoi模板

parent 88311086
package com.yeejoin.amos.fas.dao.entity;
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
@Data
public class EquipmentExcelData {
@Excel(name = "设备编号", orderNum = "1", width = 36)
private String equipCode;
@Excel(name = "设备名称", orderNum = "2", width = 24)
private String equipName;
@Excel(name = "所属区域", orderNum = "3", width = 24)
private String areaCode;
@Excel(name = "位置", orderNum = "4", width = 50)
private String position;
}
\ No newline at end of file
......@@ -102,21 +102,6 @@
</exclusion>
</exclusions>
</dependency> -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
......
package com.yeejoin.amos.fas.business.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.yeejoin.amos.fas.business.util.ExcelUtils;
import com.yeejoin.amos.fas.core.util.*;
import com.yeejoin.amos.fas.exception.YeeException;
import com.yeejoin.amos.fas.dao.entity.EquipmentExcelData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.CollectionUtils;
......@@ -33,7 +33,6 @@ import com.yeejoin.amos.fas.dao.entity.PreplanPicture;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.typroject.tyboot.core.foundation.utils.Bean;
import javax.servlet.http.HttpServletResponse;
......@@ -334,8 +333,7 @@ public class EquipmentController extends BaseController {
@RequestMapping(value = "/downTemplate")
@ApiOperation(httpMethod = "GET", value = "下载模板", notes = "下载模板")
public void downTemplate(HttpServletResponse response) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
iEquipService.downTemplate(response);
ExcelUtils.exportExcel(new ArrayList<EquipmentExcelData>(), "电力设备信息", "导入模板", EquipmentExcelData.class, "电力设备模板.xls", response);
}
/**
......@@ -346,8 +344,8 @@ public class EquipmentController extends BaseController {
@RequestMapping(value = "/uploadList")
@ApiOperation(httpMethod = "POST", value = "导入", notes = "导入")
public Object uploadList(MultipartFile file) {
iEquipService.uploadList(file, getOrgCode(getSelectedOrgInfo()));
List<EquipmentExcelData> list = ExcelUtils.importExcel(file, 1, 1, EquipmentExcelData.class);
iEquipService.uploadList(list, getOrgCode(getSelectedOrgInfo()));
return CommonResponseUtil.success();
}
......
......@@ -17,6 +17,7 @@ import com.yeejoin.amos.fas.core.util.DaoCriteria;
import com.yeejoin.amos.fas.core.util.query.BaseQuerySpecification;
import com.yeejoin.amos.fas.dao.dto.EquipmentDTO;
import com.yeejoin.amos.fas.dao.entity.Equipment;
import com.yeejoin.amos.fas.dao.entity.EquipmentExcelData;
import com.yeejoin.amos.fas.dao.entity.EquipmentFireEquipment;
import com.yeejoin.amos.fas.dao.entity.PreplanPicture;
import com.yeejoin.amos.fas.exception.YeeException;
......@@ -549,92 +550,47 @@ public class EquipmentServiceImpl implements IEquipmentService {
}
@Override
public void uploadList(MultipartFile file, String orgCode) {
if (null != file) {
try {
Workbook workbook = new HSSFWorkbook(file.getInputStream());
Sheet sheet = workbook.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
if (lastRowNum >= 1) {
List<Equipment> equipmentList = new LinkedList<>();
// 获取所有的区域编码
Map<String, Object> res = (Map<String, Object>) iEquipManageFeign.getBuildingCodeKey();
if (((int)res.get("status")) != 200) {
throw new YeeException("解析位置编码失败");
}
Map<String, String> areaCodeIdMap = (Map<String, String>) res.getOrDefault("result", new HashMap<String, String>());
// 获取所有存在的重点设备编码
Set<String> equipCodeSet = impEquipMapper.getAllCode();
for (int i = 1; i <= lastRowNum; i++) {
Row row = sheet.getRow(i);
String equipCode = getCellValue(row.getCell(0));
String equipName = getCellValue(row.getCell(1));
String areaCode = getCellValue(row.getCell(2));
String position = getCellValue(row.getCell(3));
if (ValidationUtil.isEmpty(equipCode) || ValidationUtil.isEmpty(equipName) || ValidationUtil.isEmpty(areaCode)) {
throw new YeeException("错误行" + i + ":检查非空字段");
}
if (equipCodeSet.contains(equipCode.trim())) {
throw new YeeException("错误行" + i + ":重复的设备编号");
}
if (!areaCodeIdMap.containsKey(areaCode.trim())) {
throw new YeeException("错误行" + i + ":所属区域编号有误");
}
equipCodeSet.add(equipCode.trim());
Equipment equipment = new Equipment();
equipment.setName(equipName.trim());
equipment.setCode(equipCode.trim());
equipment.setRiskSourceId(Long.valueOf(areaCodeIdMap.get(areaCode.trim())));
if (!ValidationUtil.isEmpty(position)) {
equipment.setAddress(position);
}
equipment.setIsIndoor(false);
equipment.setOrgCode(orgCode);
equipmentList.add(equipment);
}
iEquipmentDao.saveAll(equipmentList);
public void uploadList(List<EquipmentExcelData> list, String orgCode) {
if (!list.isEmpty()) {
List<Equipment> equipmentList = new LinkedList<>();
// 获取所有的区域编码
Map<String, Object> res = (Map<String, Object>) iEquipManageFeign.getBuildingCodeKey();
if (((int)res.get("status")) != 200) {
throw new YeeException("解析位置编码失败");
}
Map<String, String> areaCodeIdMap = (Map<String, String>) res.getOrDefault("result", new HashMap<String, String>());
// 获取所有存在的重点设备编码
Set<String> equipCodeSet = impEquipMapper.getAllCode();
for (int i = 0; i < list.size(); i++) {
int row = i + 3;
EquipmentExcelData x = list.get(i);
String equipCode = x.getEquipCode();
String equipName = x.getEquipName();
String areaCode = x.getAreaCode();
String position = x.getPosition();
if (ValidationUtil.isEmpty(equipCode) || ValidationUtil.isEmpty(equipName) || ValidationUtil.isEmpty(areaCode)) {
throw new YeeException("错误行" + row + ":检查非空字段");
}
} catch (IOException e) {
e.printStackTrace();
if (equipCodeSet.contains(equipCode.trim())) {
throw new YeeException("错误行" + row + ":重复的设备编号");
}
if (!areaCodeIdMap.containsKey(areaCode.trim())) {
throw new YeeException("错误行" + row + ":所属区域编号有误");
}
equipCodeSet.add(equipCode.trim());
Equipment equipment = new Equipment();
equipment.setName(equipName.trim());
equipment.setCode(equipCode.trim());
equipment.setRiskSourceId(Long.valueOf(areaCodeIdMap.get(areaCode.trim())));
if (!ValidationUtil.isEmpty(position)) {
equipment.setAddress(position);
}
equipment.setIsIndoor(false);
equipment.setOrgCode(orgCode);
equipmentList.add(equipment);
}
iEquipmentDao.saveAll(equipmentList);
}
}
public static String getCellValue(Cell cell) {
if (cell == null) {
return null;
}
String cellValue = "";
// 以下是判断数据的类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: // 数字
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellValue = sdf.format(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(cell.getNumericCellValue())).toString();
} else {
DataFormatter dataFormatter = new DataFormatter();
cellValue = dataFormatter.formatCellValue(cell);
}
break;
case Cell.CELL_TYPE_STRING: // 字符串
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN: // Boolean
cellValue = cell.getBooleanCellValue() + "";
break;
case Cell.CELL_TYPE_FORMULA: // 公式
cellValue = cell.getCellFormula() + "";
break;
case Cell.CELL_TYPE_BLANK: // 空值
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR: // 故障
cellValue = "";
break;
default:
cellValue = "";
break;
}
return cellValue;
}
}
......@@ -6,6 +6,7 @@ import com.yeejoin.amos.fas.core.common.request.CommonPageable;
import com.yeejoin.amos.fas.core.common.response.EquipDetailsResponse;
import com.yeejoin.amos.fas.core.util.DaoCriteria;
import com.yeejoin.amos.fas.dao.entity.Equipment;
import com.yeejoin.amos.fas.dao.entity.EquipmentExcelData;
import com.yeejoin.amos.fas.dao.entity.EquipmentFireEquipment;
import com.yeejoin.amos.fas.dao.entity.PreplanPicture;
import org.springframework.data.domain.Page;
......@@ -125,7 +126,6 @@ public interface IEquipmentService {
/**
* 导入数据
* @param file
*/
void uploadList(MultipartFile file, String orgCode);
void uploadList(List<EquipmentExcelData> list, String orgCode);
}
package com.yeejoin.amos.fas.business.util;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* @description: easyPoi工具类
* @author: duanwei
* @create: 2020-05-28 13:57
**/
public class ExcelUtils {
/**
* excel 导出
*
* @param list 数据
* @param title 标题
* @param sheetName sheet名称
* @param pojoClass pojo类型
* @param fileName 文件名称
* @param isCreateHeader 是否创建表头
* @param response
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,
String fileName, boolean isCreateHeader, HttpServletResponse response) {
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
/**
* excel 导出
*
* @param list 数据
* @param title 标题
* @param sheetName sheet名称
* @param pojoClass pojo类型
* @param fileName 文件名称
* @param response
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
HttpServletResponse response) {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
/**
* excel 导出
*
* @param list 数据
* @param fileName 文件名称
* @param response
*/
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
defaultExport(list, fileName, response);
}
/**
* 默认的 excel 导出
*
* @param list 数据
* @param pojoClass pojo类型
* @param fileName 文件名称
* @param response
* @param exportParams 导出参数
*/
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName,
HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
if (workbook != null){
downLoadExcel(fileName, response, workbook);
};
}
/**
* 下载
*
* @param fileName 文件名称
* @param response
* @param workbook excel数据
*/
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
workbook.write(response.getOutputStream());
} catch (IOException e) {
//throw new NormalException(e.getMessage());
}
}
/**
* 默认的 excel 导出
*
* @param list 数据
* @param fileName 文件名称
* @param response
*/
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
if (workbook != null) {
downLoadExcel(fileName, response, workbook);}
}
/**
* excel 导入
*
* @param filePath excel文件路径
* @param titleRows 标题行
* @param headerRows 表头行
* @param pojoClass pojo类型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
//throw new NormalException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
//throw new NormalException(e.getMessage());
}
return list;
}
/**
* excel 导入
*
* @param file 上传的文件
* @param titleRows 标题行
* @param headerRows 表头行
* @param pojoClass pojo类型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (file == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
} catch (NoSuchElementException e) {
// throw new NormalException("excel文件不能为空");
} catch (Exception e) {
//throw new NormalException(e.getMessage());
System.out.println(e.getMessage());
}
return list;
}
}
......@@ -199,6 +199,21 @@
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<dependencyManagement>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment