add:内外部会议EXCEL导入
This commit is contained in:
+5
-2
@@ -26,7 +26,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.text.ParseException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@@ -125,7 +125,10 @@ public class InsideOutSideMeetingController extends BaseController<InsideOutside
|
||||
|
||||
@ApiOperation("Excel文件导入内外部会议信息")
|
||||
@PostMapping("/importMeetingInfo")
|
||||
public ResponseMessage<?> importMeetingInfo(@RequestParam(value = "file",required = false) MultipartFile file) {
|
||||
public ResponseMessage<?> importMeetingInfo(@RequestParam(value = "file",required = false) MultipartFile file) throws ParseException {
|
||||
if (file == null) {
|
||||
return Result.error("文件为空,请重新上传");
|
||||
}
|
||||
return meetingService.importMeetingInfo(file);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -6,6 +6,7 @@ import com.adc.da.slrs.InsideOntSideMeeting.entity.InsideOutsideMeetingVO;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -29,5 +30,5 @@ public interface InsideOutsideMeetingService extends IService<InsideOutsideMeeti
|
||||
|
||||
List<InsideOutsideMeetingVO> queryMeetingById(List<String> exportIdList);
|
||||
|
||||
ResponseMessage<?> importMeetingInfo(MultipartFile file);
|
||||
ResponseMessage<?> importMeetingInfo(MultipartFile file) throws ParseException;
|
||||
}
|
||||
|
||||
+100
-29
@@ -15,10 +15,8 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -27,6 +25,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
@@ -146,12 +145,11 @@ public class InsideOutsideMeetingServiceImpl extends ServiceImpl<InsideOutsideMe
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseMessage<?> importMeetingInfo(MultipartFile file) {
|
||||
//给出头部信息
|
||||
String[] headerExcel = (FieldConvertUtil.exportFieldNamesInsideOutsideMeeting + "," + FieldConvertUtil.exportFieldNamesMeetingTopic).split(",");
|
||||
public ResponseMessage<?> importMeetingInfo(MultipartFile file) throws ParseException {
|
||||
//获取文件全称
|
||||
String fileNameStr = file.getOriginalFilename();
|
||||
//获取最后.的位置
|
||||
assert fileNameStr != null;
|
||||
int pos = fileNameStr.lastIndexOf(".");
|
||||
//获取压缩文件名称并以小写显示
|
||||
String fileStr = fileNameStr.substring(pos + 1).toLowerCase();
|
||||
@@ -175,11 +173,11 @@ public class InsideOutsideMeetingServiceImpl extends ServiceImpl<InsideOutsideMe
|
||||
} catch (IOException e) {
|
||||
return Result.error("文件存储失败!");
|
||||
}
|
||||
int countSuccess = 0;
|
||||
//解压缩
|
||||
String zipEntryName = null;
|
||||
try {
|
||||
zipEntryName = FileUnZip.unZipFiles(path + "/" + fileNameStr, path);
|
||||
FileUnZip.delete(new File(path + "/" + fileNameStr));
|
||||
} catch (IOException e) {
|
||||
return Result.error("文件解压失败");
|
||||
}
|
||||
@@ -190,7 +188,7 @@ public class InsideOutsideMeetingServiceImpl extends ServiceImpl<InsideOutsideMe
|
||||
return Result.error("上传的文件只能有一个");
|
||||
}
|
||||
File importFile = fileList.get(0);
|
||||
if (!importFile.getName().contains(".xls") || !importFile.getName().contains(".xlsx")) {
|
||||
if (!importFile.getName().contains(".xls") && !importFile.getName().contains(".xlsx")) {
|
||||
return Result.error("文件必须是EXCEL文件");
|
||||
}
|
||||
Workbook workbook = null;
|
||||
@@ -206,31 +204,104 @@ public class InsideOutsideMeetingServiceImpl extends ServiceImpl<InsideOutsideMe
|
||||
return Result.error("工作表为空");
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
List<String> headerList = new ArrayList<>();
|
||||
// 获取excel表头
|
||||
Row headerRow = sheet.getRow(1);
|
||||
for (int i = 0; i <= 25; i++) {
|
||||
sb.append(headerRow.getCell(i).getStringCellValue()).append(",");
|
||||
for (int i = 0; i <= 12; i++) {
|
||||
headerList.add(headerRow.getCell(i).getStringCellValue());
|
||||
}
|
||||
//获取总条数
|
||||
int rowNum = sheet.getPhysicalNumberOfRows();
|
||||
// 开始遍历表格行
|
||||
for (int rowIndex = 1; rowIndex < rowNum; rowIndex++) {
|
||||
Row row = sheet.getRow(rowIndex);
|
||||
String fields = StringUtils.join(headerExcel);
|
||||
if (!fields.equals(FieldConvertUtil.exportFieldNamesInsideOutsideMeeting + "," + FieldConvertUtil.exportFieldNamesMeetingTopic)) {
|
||||
try {
|
||||
workbook.close();
|
||||
} catch (IOException e) {
|
||||
return Result.error("文件关闭出错");
|
||||
}
|
||||
//删除原上传文件
|
||||
FileUnZip.deleteDir(saveDirectory);
|
||||
return Result.error("fail", "读取失败,请严格按照模板文件导入数据");
|
||||
|
||||
// 判断表头字段是否相同
|
||||
if (!String.join(",",headerList).equals(FieldConvertUtil.exportFieldNamesInsideOutsideMeeting + "," + FieldConvertUtil.exportFieldNamesMeetingTopic)) {
|
||||
try {
|
||||
workbook.close();
|
||||
} catch (IOException e) {
|
||||
return Result.error("文件关闭出错");
|
||||
}
|
||||
Map<String, String> rowList = new LinkedHashMap<>();
|
||||
// TODO 内外部会议导入
|
||||
//删除原上传文件
|
||||
FileUnZip.deleteDir(saveDirectory);
|
||||
return Result.error("fail", "读取失败,请严格按照模板文件导入数据");
|
||||
}
|
||||
return null;
|
||||
// 获取总行数
|
||||
int rowNum = sheet.getPhysicalNumberOfRows();
|
||||
List<Map<String,String>> importDataList = new ArrayList<>();
|
||||
// 开始遍历表格行
|
||||
for (int rowIndex = 2; rowIndex < rowNum; rowIndex++) {
|
||||
// 获取每一行
|
||||
Row row = sheet.getRow(rowIndex);
|
||||
// 用于存储每行的键值对数据,对应一条数据
|
||||
Map<String, String> rowMap = new LinkedHashMap<>();
|
||||
// 总列数
|
||||
int columnNum = 13;
|
||||
// 遍历每个单元格
|
||||
for (int columnIndex = 0; columnIndex < columnNum; columnIndex++) {
|
||||
// 获取单元格
|
||||
Cell cell = row.getCell(columnIndex);
|
||||
// 获取表头单元格
|
||||
Cell headerCell = headerRow.getCell(columnIndex);
|
||||
if (cell != null) {
|
||||
if (CellType.NUMERIC == cell.getCellType() && HSSFDateUtil.isCellDateFormatted(cell)) {
|
||||
Date d = cell.getDateCellValue();
|
||||
if (d.before(sdf.parse("1900-01-01")) || d.after(sdf.parse("2500-01-01"))) {
|
||||
return Result.error("输入的日期异常,请按照实际日期填写");
|
||||
}
|
||||
// 将单元格数据存入Map中
|
||||
rowMap.put(headerCell.getStringCellValue(), sdf.format(d));
|
||||
} else {
|
||||
cell.setCellType(CellType.STRING);
|
||||
rowMap.put(headerCell.getStringCellValue(), cell.getStringCellValue());
|
||||
}
|
||||
} else {
|
||||
rowMap.put(headerCell.getStringCellValue(), "");
|
||||
}
|
||||
}
|
||||
importDataList.add(rowMap);
|
||||
}
|
||||
for (Map<String, String> stringStringMap : importDataList) {
|
||||
LambdaQueryWrapper<InsideOutsideMeeting> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(InsideOutsideMeeting::getMeetingName, stringStringMap.get("会议名称"));
|
||||
List<InsideOutsideMeeting> insideOutsideMeetingList = this.baseMapper.selectList(wrapper);
|
||||
if (insideOutsideMeetingList.size() > 0) {
|
||||
InsideOutsideMeeting insideOutsideMeeting = insideOutsideMeetingList.get(0);
|
||||
MeetingTopic meetingTopic = new MeetingTopic();
|
||||
meetingTopic.setAgendaName(stringStringMap.get("议题名称"));
|
||||
meetingTopic.setReporter("汇报人");
|
||||
meetingTopic.setReportingUnit("汇报单位");
|
||||
meetingTopic.setAgendaContent("议题主要内容");
|
||||
meetingTopic.setValidFlag(0);
|
||||
meetingTopic.setMeetingId(insideOutsideMeeting.getId());
|
||||
meetingTopic.setCreateTime(new Date());
|
||||
meetingTopic.setModifyTime(new Date());
|
||||
meetingTopicService.save(meetingTopic);
|
||||
} else {
|
||||
InsideOutsideMeeting insideOutsideMeeting = new InsideOutsideMeeting();
|
||||
insideOutsideMeeting.setMeetingTopicList(new ArrayList<>());
|
||||
insideOutsideMeeting.setMeetingName(stringStringMap.get("会议名称"));
|
||||
insideOutsideMeeting.setTopicName(stringStringMap.get("课题名称"));
|
||||
insideOutsideMeeting.setMeetingOrganizer(stringStringMap.get("会议主办单位"));
|
||||
insideOutsideMeeting.setMeetingTime(sdf.parse(stringStringMap.get("会议时间")));
|
||||
insideOutsideMeeting.setMeetingAddress(stringStringMap.get("会议地点"));
|
||||
insideOutsideMeeting.setParticipants(stringStringMap.get("参会人员"));
|
||||
insideOutsideMeeting.setMeetingContent(stringStringMap.get("会议主要内容"));
|
||||
insideOutsideMeeting.setFirstMeetingType(stringStringMap.get("一级会议类别"));
|
||||
insideOutsideMeeting.setSecondMeetingType(stringStringMap.get("二级会议类别"));
|
||||
insideOutsideMeeting.setValidFlag(0);
|
||||
insideOutsideMeeting.setCreateTime(new Date());
|
||||
insideOutsideMeeting.setModifyTime(new Date());
|
||||
this.baseMapper.insert(insideOutsideMeeting);
|
||||
MeetingTopic meetingTopic = new MeetingTopic();
|
||||
meetingTopic.setAgendaName(stringStringMap.get("议题名称"));
|
||||
meetingTopic.setReporter("汇报人");
|
||||
meetingTopic.setReportingUnit("汇报单位");
|
||||
meetingTopic.setAgendaContent("议题主要内容");
|
||||
meetingTopic.setValidFlag(0);
|
||||
meetingTopic.setMeetingId(insideOutsideMeeting.getId());
|
||||
meetingTopic.setCreateTime(new Date());
|
||||
meetingTopic.setModifyTime(new Date());
|
||||
meetingTopicService.save(meetingTopic);
|
||||
}
|
||||
}
|
||||
return Result.success("导入完成");
|
||||
}
|
||||
|
||||
private static List<File> readImpExcelFile(String path) {
|
||||
|
||||
Reference in New Issue
Block a user