HTML
<div class="buttons">
<div class="layui-upload">
<a class="btn btn-primary fa fa-search search-link" href="javascript:void(0);" ><span>搜索</span></a>
<a type="button" class=" btn btn-primary fa " id="upExcel"><i class="layui-icon"></i>上传文件</a>
</div>
</div>
JS
/**
* 上传excel
*/
upExcel:function () {
var me=this;
layui.use('upload', function () {
var upload = layui.upload;
upload.render({
elem: '#upExcel'
,url: __ctx+'/platform/data/uemiReport/upExcel'
,accept: 'file' //普通文件
,exts: 'xls|xlsx' //允许上传的文件后缀
,done: function(res){
//如果上传成功
if (res.result==1) {
//追加图片成功追加文件名至图片容器
DialogUtil.alert(res.message);
}else {
//提示信息
DialogUtil.alert(res.message);
}
}
});
});
},
controller
@RequestMapping("upExcel")
public ResultMessage upExcel(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception {
ResultMessage message=null;
MultipartFile file = request.getFile("file");
String fileName = file.getOriginalFilename();
String filePath = "D:/excel";
File dest = new File(filePath + fileName);
file.transferTo(dest);
Long userId = ContextUtil.getLoginUserId();
message = cemsDataService.importData(dest,userId);
dest.delete();
return message;
}
service
@Override
public ResultMessage importData(File excel, Long userId) {
ResultMessage message=null;
try {
List<List<String>> lists = readExcel(excel);
String check=this.checkIsImport(lists,userId);
if(!check.equals("")){
message=new ResultMessage(ResultMessage.Fail, check);
}else {
int i = 0;
for (List<String> list : lists) {
if (i == 0) {
i++;
continue;
}
CemsData cemsData = new CemsData();
int j = 0;
for (String strs : list) {
switch (j) {
case 0://企业代码
j += 1;
if (!strs.equals("null")) {
Station factory = stationService.getByCode(strs);
if (BeanUtils.isNotEmpty(factory)) {
cemsData.setFactoryId(factory.getId());
}
}
break;
case 1:
j += 1;
break;
case 2://排口编号
j += 1;
if (!strs.equals("null")) {
Station emi = stationService.getByCode(strs);
if (BeanUtils.isNotEmpty(emi)) {
cemsData.setEmiId(emi.getId());
}
}
break;
case 3:
j += 1;
break;
case 4://采集时间
j += 1;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (!strs.equals("null")) {
cemsData.setCollectTime(sdf.parse(strs));
}
break;
case 5://烟尘
j += 1;
if (!strs.equals("null")) {
cemsData.setSmoke(Double.parseDouble(strs));
}
break;
case 6://二氧化硫
j += 1;
if (!strs.equals("null")) {
cemsData.setSo2(Double.parseDouble(strs));
}
break;
case 7://氮氧化物
j += 1;
if (!strs.equals("null")) {
cemsData.setNox(Double.parseDouble(strs));
}
break;
case 8://VOCs
j += 1;
if (!strs.equals("null")) {
cemsData.setVocs(Double.parseDouble(strs));
}
break;
case 9://当日排放量
j += 1;
if (!strs.equals("null")) {
cemsData.setEmiCount(Double.parseDouble(strs));
}
break;
case 10://小时均值是否达标
j += 1;
if (!strs.equals("null")) {
if (strs.equals("是")) {
cemsData.setIsStandard((short) 1);
}
if (strs.equals("否")) {
cemsData.setIsStandard((short) 0);
}
}
break;
case 11://备注
j += 1;
if (!strs.equals("null")) {
cemsData.setMemo(strs);
}
break;
}
}
if (BeanUtils.isNotEmpty(cemsData.getFactoryId())) {
cemsData.setCreateTime(new Date());
cemsData.setCreateBy(userId);
cemsData.setUpdateBy(userId);
cemsData.setUpdateTime(new Date());
cemsData.setId(UniqueIdUtil.genId());
cemsDataMapper.add(cemsData);
}
}
message = new ResultMessage(ResultMessage.Success, "上传数据成功");
}
}catch(Exception e){
e.printStackTrace();
message=new ResultMessage(ResultMessage.Fail, "上传数据失败");
}
return message;
}
public static List<List<String>> readExcel(File excel) throws Exception, InvalidFormatException {
List<List<String>> lists = new ArrayList<List<String>>();
InputStream is = null;
try {
is = new FileInputStream(excel);
Workbook wb = WorkbookFactory.create(is);
int length = wb.getNumberOfSheets();
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
ArrayList<String> list = new ArrayList<String>();
for (int i=0;i<15;i++) {
Cell cell=row.getCell(i);
if (cell == null || cell.toString().trim().equals("")) {
list.add("null");
continue;
}
int cellType = cell.getCellType();
String cellValue = "";
switch (cellType) {
case Cell.CELL_TYPE_STRING: // 字符串类型
cellValue = cell.getStringCellValue().trim();
break;
case Cell.CELL_TYPE_BOOLEAN: // 布尔类型
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC: // 数值类型
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(HSSFDateUtil.isCellDateFormatted(cell)){
Date time=cell.getDateCellValue();
cellValue=sdf.format(time);
}else{
cellValue = new DecimalFormat("#.######").format(cell.getNumericCellValue());
}
break;
default: // 其它类型
cellValue = "null";
break;
}
list.add(cellValue);
}
lists.add(list);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return lists;
}
private String checkIsImport(List<List<String>> lists,Long userId) {
String check="";
int i=0;
for (List<String> list : lists) {
boolean hasFac=false;
if(i==0){
i++;
continue;
}
i++;
int j=0;
String factoryName="";
for (String strs : list) {
switch (j) {
case 0://企业代码
j += 1;
if (!strs.equals("null")) {
Station factory = stationService.getByCode(strs);
if (BeanUtils.isEmpty(factory)) {
check+="无"+strs+"企业;";
}else{
List<Station> facList = stationService.getListByUserIdAndParam(userId, "isCems", "1");
for (Station fac : facList) {
if (fac.getCode().equals(strs)) {
hasFac = true;
}
}
if(hasFac==false){
check+="无录入"+strs+"数据的权限;";
}
}
}
break;
case 1:
j += 1;
if (!strs.equals("null")) {
factoryName=strs;
}
break;
case 2://排口编号
j += 1;
if (!strs.equals("null")) {
Station emi=stationService.getByCode(strs);
if(BeanUtils.isEmpty(emi)){
check+="无"+strs+"排口;";
}
}
break;
case 3:
j += 1;
break;
case 4://采集时间
j += 1;
break;
case 5://烟尘
j += 1;
break;
case 6://二氧化硫
j += 1;
break;
case 7://氮氧化物
j += 1;
break;
case 8://VOCs
j += 1;
break;
case 9://当日排放量
j += 1;
break;
case 10://小时均值是否达标
j += 1;
break;
case 11://备注
j += 1;
break;
}
}
}
return check;
}