|
@@ -2,42 +2,58 @@ package org.springblade.manager.controller;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
+import com.spire.xls.*;
|
|
|
import io.swagger.annotations.*;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import lombok.SneakyThrows;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+import org.apache.poi.ss.util.RegionUtil;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.jsoup.Jsoup;
|
|
|
import org.jsoup.nodes.Document;
|
|
|
import org.jsoup.nodes.Element;
|
|
|
import org.jsoup.select.Elements;
|
|
|
-import org.springblade.common.utils.CommonUtil;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
import org.springblade.core.boot.ctrl.BladeController;
|
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
|
import org.springblade.core.tool.api.R;
|
|
|
+import org.springblade.core.tool.utils.IoUtil;
|
|
|
+import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
+import org.springblade.core.tool.utils.ResourceUtil;
|
|
|
import org.springblade.manager.dto.WbsTreeContractDTO2;
|
|
|
import org.springblade.manager.entity.ContractInfo;
|
|
|
-import org.springblade.manager.entity.ExcelTab;
|
|
|
import org.springblade.manager.entity.WbsTreeContract;
|
|
|
import org.springblade.manager.feign.ContractClient;
|
|
|
import org.springblade.manager.service.IWbsTreeContractService;
|
|
|
import org.springblade.manager.service.impl.WbsTreeContractServiceImpl;
|
|
|
-import org.springblade.manager.utils.ExcelParser;
|
|
|
+import org.springblade.manager.utils.FileUtils;
|
|
|
import org.springblade.manager.vo.*;
|
|
|
+import org.springblade.resource.feign.NewIOSSClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.text.ParseException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
+import java.util.List;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
@RestController
|
|
|
@AllArgsConstructor
|
|
@@ -48,9 +64,11 @@ public class WbsTreeContractController extends BladeController {
|
|
|
@Autowired
|
|
|
StringRedisTemplate redisTemplate;
|
|
|
private final JdbcTemplate jdbcTemplate;
|
|
|
+ private final NewIOSSClient newIOSSClient;
|
|
|
private final IWbsTreeContractService iWbsTreeContractService;
|
|
|
private final WbsTreeContractServiceImpl wbsTreeContractServiceImpl;
|
|
|
private final ContractClient contractClient;
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(WbsTreeContractController.class);
|
|
|
|
|
|
@GetMapping("/search-node-tables")
|
|
|
@ApiOperationSupport(order = 1)
|
|
@@ -224,7 +242,7 @@ public class WbsTreeContractController extends BladeController {
|
|
|
if (StringUtils.isNotEmpty(primaryKeyId)) {
|
|
|
parentId = primaryKeyId;
|
|
|
}
|
|
|
- List<WbsTreeContractLazyVO> vos = iWbsTreeContractService.imageLazyQueryContractWbsTree(parentId, contractId, contractIdRelation,classId);
|
|
|
+ List<WbsTreeContractLazyVO> vos = iWbsTreeContractService.imageLazyQueryContractWbsTree(parentId, contractId, contractIdRelation, classId);
|
|
|
return R.data(vos);
|
|
|
}
|
|
|
|
|
@@ -262,148 +280,322 @@ public class WbsTreeContractController extends BladeController {
|
|
|
return R.data(list);
|
|
|
}
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
@GetMapping("/download-excel")
|
|
|
@ApiOperationSupport(order = 13)
|
|
|
@ApiOperation(value = "客户端-下载元素表对应的excel模板", notes = "传入表的pKeyId")
|
|
|
- public void downloadExcel(@RequestParam String pKeyId, HttpServletResponse response) throws Exception {
|
|
|
- WbsTreeContract tab = iWbsTreeContractService.getBaseMapper().selectOne(Wrappers.<WbsTreeContract>lambdaQuery().eq(WbsTreeContract::getPKeyId, pKeyId));
|
|
|
- if (tab != null && Objects.nonNull(tab.getExcelId())) {
|
|
|
- ExcelTab excelTab = jdbcTemplate.queryForObject("select file_url,name from m_excel_tab where id = " + tab.getExcelId(), new BeanPropertyRowMapper<>(ExcelTab.class));
|
|
|
- if (excelTab != null) {
|
|
|
- InputStream inputStream = CommonUtil.getOSSInputStream(excelTab.getFileUrl());
|
|
|
- if (inputStream != null) {
|
|
|
- response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- response.setHeader("Content-disposition", ";filename=" + URLEncoder.encode(excelTab.getName().replace(" ", ""), "UTF-8") + ".xlsx");
|
|
|
- ServletOutputStream servletOutputStream = response.getOutputStream();
|
|
|
- byte[] buffer = new byte[4096];
|
|
|
- int bytesRead;
|
|
|
- while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
- servletOutputStream.write(buffer, 0, bytesRead);
|
|
|
+ public void downloadExcel(@RequestParam String pKeyId, HttpServletResponse response) {
|
|
|
+ com.spire.xls.Workbook workbook = null;
|
|
|
+ try {
|
|
|
+ WbsTreeContract tab = iWbsTreeContractService.getBaseMapper().selectOne(Wrappers.<WbsTreeContract>lambdaQuery().eq(WbsTreeContract::getPKeyId, pKeyId));
|
|
|
+ if (ObjectUtil.isEmpty(tab) || ObjectUtil.isEmpty(tab.getHtmlUrl())) {
|
|
|
+ throw new ServiceException("未获取到对应的表信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ //将html转换为excel
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
+ InputStream inputStreamByUrl = FileUtils.getInputStreamByUrl(tab.getHtmlUrl());
|
|
|
+ workbook = new com.spire.xls.Workbook();
|
|
|
+ workbook.loadFromHtml(inputStreamByUrl);
|
|
|
+ workbook.saveToStream(byteArrayOutputStream, FileFormat.Version2007);
|
|
|
+ //将转换的spireExcel存储到流中
|
|
|
+ byte[] excelBytes = byteArrayOutputStream.toByteArray();
|
|
|
+
|
|
|
+ //把spireExcel转为poiExcel
|
|
|
+ try (InputStream inputStream = new ByteArrayInputStream(excelBytes)) {
|
|
|
+ org.apache.poi.ss.usermodel.Workbook poiWorkbook = new XSSFWorkbook(inputStream);
|
|
|
+ Sheet poiSheet = poiWorkbook.getSheetAt(0); //获取第一个工作表
|
|
|
+ //存储需要修改的单元格和相关信息的列表
|
|
|
+ List<CellModificationInfo> cellsToModify = new ArrayList<>();
|
|
|
+ for (Row row : poiSheet) {
|
|
|
+ for (Cell cell : row) {
|
|
|
+ int cellType = cell.getCellType();
|
|
|
+ if (cellType == CellType.STRING.getCode() && ObjectUtil.isNotEmpty(cell.getStringCellValue())) {
|
|
|
+ //存储需要修改的单元格信息
|
|
|
+ cellsToModify.add(new CellModificationInfo(cell, cell.getStringCellValue().trim()));
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取单元格所属的合并单元格区域
|
|
|
+ CellRangeAddress mergedRegion = findMergedRegion(poiSheet, cell.getRowIndex(), cell.getColumnIndex());
|
|
|
+ if (mergedRegion != null) {
|
|
|
+ // 存储需要修改的合并单元格信息
|
|
|
+ cellsToModify.add(new CellModificationInfo(cell, mergedRegion));
|
|
|
+ }
|
|
|
}
|
|
|
- servletOutputStream.flush();
|
|
|
- inputStream.close();
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- /*public void downloadExcel(@RequestParam String pKeyId, HttpServletResponse response) throws Exception {
|
|
|
- WbsTreeContract tab = iWbsTreeContractService.getBaseMapper().selectOne(Wrappers.<WbsTreeContract>lambdaQuery().eq(WbsTreeContract::getPKeyId, pKeyId));
|
|
|
- if (tab != null && Objects.nonNull(tab.getExcelId())) {
|
|
|
- ExcelTab excelTab = jdbcTemplate.queryForObject("select file_url,name from m_excel_tab where id = " + tab.getExcelId(), new BeanPropertyRowMapper<>(ExcelTab.class));
|
|
|
- if (excelTab != null) {
|
|
|
- InputStream inputStream = CommonUtil.getOSSInputStream(excelTab.getFileUrl());
|
|
|
- if (inputStream != null) {
|
|
|
- try (Workbook workbook = parseExcelFile(inputStream)) {
|
|
|
- response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- response.setHeader("Content-disposition", ";filename=" + URLEncoder.encode(excelTab.getName().replace(" ", ""), "UTF-8") + ".xlsx");
|
|
|
- ServletOutputStream servletOutputStream = response.getOutputStream();
|
|
|
- workbook.write(servletOutputStream);
|
|
|
- servletOutputStream.flush();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ //遍历结束后,实际修改单元格样式和内容
|
|
|
+ for (CellModificationInfo info : cellsToModify) {
|
|
|
+ Cell cell = info.getCell();
|
|
|
+ if (info.hasStringContent()) {
|
|
|
+ cell.setCellValue(info.getStringContent());
|
|
|
+ }
|
|
|
+
|
|
|
+ //复制单元格样式
|
|
|
+ CellStyle cellStyle = poiWorkbook.createCellStyle();
|
|
|
+ CellStyle sourceCellStyle = cell.getCellStyle(); //获取原单元格的样式
|
|
|
+ cellStyle.cloneStyleFrom(sourceCellStyle); //复制样式
|
|
|
+ setBlackBorder(cellStyle); // 设置边框
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+
|
|
|
+ if (info.hasMergedRegion()) { //修改合并单元格边框线
|
|
|
+ CellRangeAddress mergedRegion = info.getMergedRegion();
|
|
|
+ RegionUtil.setBorderTop(BorderStyle.THIN, mergedRegion, poiSheet);
|
|
|
+ RegionUtil.setTopBorderColor(IndexedColors.BLACK.getIndex(), mergedRegion, poiSheet);
|
|
|
+ RegionUtil.setBorderBottom(BorderStyle.THIN, mergedRegion, poiSheet);
|
|
|
+ RegionUtil.setBottomBorderColor(IndexedColors.BLACK.getIndex(), mergedRegion, poiSheet);
|
|
|
+ RegionUtil.setBorderLeft(BorderStyle.THIN, mergedRegion, poiSheet);
|
|
|
+ RegionUtil.setLeftBorderColor(IndexedColors.BLACK.getIndex(), mergedRegion, poiSheet);
|
|
|
+ RegionUtil.setBorderRight(BorderStyle.THIN, mergedRegion, poiSheet);
|
|
|
+ RegionUtil.setRightBorderColor(IndexedColors.BLACK.getIndex(), mergedRegion, poiSheet);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ //返回响应
|
|
|
+ try (ServletOutputStream outputStream = response.getOutputStream();
|
|
|
+ ByteArrayOutputStream byteArrayOutputStreamResult = new ByteArrayOutputStream()) {
|
|
|
+ //设置响应头
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(tab.getFullName(), "UTF-8") + ".xlsx");
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ poiWorkbook.write(byteArrayOutputStreamResult);
|
|
|
+ byte[] excelBytesResult = byteArrayOutputStreamResult.toByteArray();
|
|
|
+ outputStream.write(excelBytesResult);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("下载excel时出现异常:" + e.getMessage(), e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("下载excel时出现异常:" + e.getMessage(), e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("下载excel时出现异常:" + e.getMessage(), e);
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (workbook != null) {
|
|
|
+ workbook.dispose();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //解析Excel文件并设置单元格格式为文本
|
|
|
- private Workbook parseExcelFile(InputStream inputStream) throws IOException, InvalidFormatException {
|
|
|
- Workbook workbook = WorkbookFactory.create(inputStream);
|
|
|
- DataFormat dataFormat = workbook.createDataFormat();
|
|
|
- //遍历所有工作表和单元格,设置单元格格式为文本
|
|
|
- for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
|
|
|
- Sheet sheet = workbook.getSheetAt(sheetIndex);
|
|
|
- for (Row row : sheet) {
|
|
|
- for (Cell cell : row) {
|
|
|
- CellStyle cellStyle = cell.getCellStyle();
|
|
|
- cellStyle.setDataFormat(dataFormat.getFormat("@")); //设置单元格格式为文本
|
|
|
- cell.setCellStyle(cellStyle);
|
|
|
- }
|
|
|
+ //在CellModificationInfo类中,根据需要存储单元格内容和合并单元格信息
|
|
|
+ static class CellModificationInfo {
|
|
|
+ private final Cell cell;
|
|
|
+ private String stringContent;
|
|
|
+ private CellRangeAddress mergedRegion;
|
|
|
+
|
|
|
+ //构造函数,用于存储单元格内容
|
|
|
+ public CellModificationInfo(Cell cell, String stringContent) {
|
|
|
+ this.cell = cell;
|
|
|
+ this.stringContent = stringContent;
|
|
|
+ }
|
|
|
+
|
|
|
+ //构造函数,用于存储合并单元格信息
|
|
|
+ public CellModificationInfo(Cell cell, CellRangeAddress mergedRegion) {
|
|
|
+ this.cell = cell;
|
|
|
+ this.mergedRegion = mergedRegion;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Cell getCell() {
|
|
|
+ return cell;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getStringContent() {
|
|
|
+ return stringContent;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean hasStringContent() {
|
|
|
+ return stringContent != null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public CellRangeAddress getMergedRegion() {
|
|
|
+ return mergedRegion;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean hasMergedRegion() {
|
|
|
+ return mergedRegion != null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取合并单元格
|
|
|
+ private static CellRangeAddress findMergedRegion(Sheet sheet, int rowNum, int colNum) {
|
|
|
+ for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) {
|
|
|
+ if (mergedRegion.isInRange(rowNum, colNum)) {
|
|
|
+ return mergedRegion;
|
|
|
}
|
|
|
}
|
|
|
- return workbook;
|
|
|
- }*/
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置边框线
|
|
|
+ private static void setBlackBorder(CellStyle cellStyle) {
|
|
|
+ cellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ cellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ cellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+ cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ }
|
|
|
|
|
|
@PostMapping("/import-excel")
|
|
|
@ApiOperationSupport(order = 14)
|
|
|
@ApiOperation(value = "客户端-导入excel数据到对应元素表中", notes = "传入表的pKeyId、excel文件file")
|
|
|
public R<Map<String, Object>> importExcel(@RequestParam String pKeyId, @RequestPart MultipartFile file) throws Exception {
|
|
|
//获取当前表htmlString
|
|
|
- String htmlString = wbsTreeContractServiceImpl.getHtmlString(pKeyId);
|
|
|
- if (StringUtils.isEmpty(htmlString)) {
|
|
|
+ String htmlString_1 = wbsTreeContractServiceImpl.getHtmlString(pKeyId);
|
|
|
+ if (StringUtils.isEmpty(htmlString_1)) {
|
|
|
throw new ServiceException("获取元素表html信息失败");
|
|
|
}
|
|
|
|
|
|
- //获取excel数据
|
|
|
- Map<String, String> stringStringMap = ExcelParser.parseExcel(file);
|
|
|
-
|
|
|
- //匹配定位
|
|
|
- Map<String, Object> resultDataMap = new LinkedHashMap<>();
|
|
|
- Document doc = Jsoup.parse(htmlString);
|
|
|
- for (Map.Entry<String, String> stringStringEntry : stringStringMap.entrySet()) {
|
|
|
- String value = stringStringEntry.getValue();
|
|
|
- if (StringUtils.isNotEmpty(value)) {
|
|
|
- String[] split = stringStringEntry.getKey().split("_");
|
|
|
- int row = Integer.parseInt(split[0]) + 1;
|
|
|
- int cell = Integer.parseInt(split[1]) + 1;
|
|
|
- String selectorValue = "td:has(el-date-picker[x1=" + cell + "][y1=" + row + "])," +
|
|
|
- "td:has(el-input[x1=" + cell + "][y1=" + row + "])," +
|
|
|
- "td:has(hc-form-select-search[x1=" + cell + "][y1=" + row + "])," +
|
|
|
- "td:has(hc-table-form-upload[x1=" + cell + "][y1=" + row + "])," +
|
|
|
- "td:has(hc-form-checkbox-group[x1=" + cell + "][y1=" + row + "])," +
|
|
|
- "td:has(el-radio-group[x1=" + cell + "][y1=" + row + "])," +
|
|
|
- "td:has(el-select[x1=" + cell + "][y1=" + row + "])";
|
|
|
- Elements tdElementsValues = doc.select(selectorValue);
|
|
|
- if (!tdElementsValues.isEmpty()) {
|
|
|
- Elements childElements = tdElementsValues.select("el-input, el-date-picker, hc-form-select-search, hc-table-form-upload, hc-form-checkbox-group, el-radio-group, el-select");
|
|
|
- for (Element childElement : childElements) {
|
|
|
- String keyName = childElement.attr("keyname");
|
|
|
- if (StringUtils.isNotEmpty(keyName)) {
|
|
|
- String dateFormatPattern = "yyyy年MM月dd日-yyyy年MM月dd日";
|
|
|
- if (isValidDateFormat(value, dateFormatPattern)) {
|
|
|
- //解析日期格式是否为范围格式,范围日期返回数组
|
|
|
- String[] splitDate = value.split("-");
|
|
|
- List<String> strings = Arrays.asList(splitDate[0], splitDate[1]);
|
|
|
- resultDataMap.put(keyName, strings);
|
|
|
- } else if (value.contains("年") && value.contains("月") && value.contains("日")) {
|
|
|
- //解析日期格式是否为单日期格式,那么剔除空格返回
|
|
|
- String replace = value.replace(" ", "");
|
|
|
- if (!replace.equals("年月日")) {//如果为空那么不返回
|
|
|
- resultDataMap.put(keyName, replace);
|
|
|
+ //结果集
|
|
|
+ Map<String, Object> stringStringMap = new HashMap<>();
|
|
|
+
|
|
|
+ //日期格式正则
|
|
|
+ String doubleSlashRegex_XG = ".*\\/[^\\/]*\\/.*"; //匹配包含两个斜杠且不相邻的字符串
|
|
|
+ String dateFormatRegex_yyyyMdd = "\\d{4}/\\d{1,2}/\\d{1,2}"; //yyyy/M/dd格式
|
|
|
+ String dateFormatRegex_yyyyMMdd = "\\d{4}/\\d{2}/\\d{2}"; //yyyy/MM/dd格式
|
|
|
+ SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy/M/dd");
|
|
|
+ SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
|
|
|
+
|
|
|
+ //把导入的excel转换为html
|
|
|
+ Long id = SnowFlakeUtil.getId();
|
|
|
+ String importExcelFilePath = FileUtils.getSysLocalFileUrl();
|
|
|
+ String importExcelTOHtmlPath = importExcelFilePath + "/pdf//" + id + ".html";
|
|
|
+
|
|
|
+ //解析匹配数据
|
|
|
+ String url_1 = "";
|
|
|
+ com.spire.xls.Workbook workbook = null;
|
|
|
+ try {
|
|
|
+ workbook = new com.spire.xls.Workbook();
|
|
|
+ workbook.loadFromHtml(file.getInputStream());
|
|
|
+ workbook.saveToFile(importExcelTOHtmlPath, FileFormat.HTML);
|
|
|
+ Worksheet sheet = workbook.getWorksheets().get(0); //始终只匹配第一张表
|
|
|
+ //获取转换成功后的html路径
|
|
|
+ url_1 = importExcelTOHtmlPath.split("pdf//")[0];
|
|
|
+ String excelToHtmlFileUrl = url_1 + "/pdf/" + id + "_files/" + sheet.getName() + ".html";
|
|
|
+ String htmlString_2 = IoUtil.readToString(new FileInputStream(ResourceUtil.getFile(excelToHtmlFileUrl)));
|
|
|
+ //循环遍历解析tr、td
|
|
|
+ Document doc_1 = Jsoup.parse(htmlString_1);
|
|
|
+ Document doc_2 = Jsoup.parse(htmlString_2);
|
|
|
+ Elements trElements1 = doc_1.select("table tbody tr");
|
|
|
+ Elements trElements2 = doc_2.select("table tbody tr");
|
|
|
+ for (int i = 0; i < trElements1.size(); i++) {
|
|
|
+ Element tr1 = trElements1.get(i);
|
|
|
+ Element tr2 = trElements2.get(i);
|
|
|
+ Elements tdElements1 = tr1.select("td");
|
|
|
+ Elements tdElements2 = tr2.select("td");
|
|
|
+ for (int j = 0; j < tdElements1.size(); j++) {
|
|
|
+ Element td1 = tdElements1.get(j);
|
|
|
+ Element td2 = tdElements2.get(j);
|
|
|
+ //String x1 = getX1Attribute(td1);
|
|
|
+ //String y1 = getY1Attribute(td1);
|
|
|
+ String keyName = getKeyNameFromChildElement(td1);
|
|
|
+ if (StringUtils.isNotEmpty(keyName)) {
|
|
|
+ String divValue = td2.text(); //获取文本值
|
|
|
+ if (StringUtils.isNotEmpty(divValue)) {
|
|
|
+ //判断是否存在两个斜杠,且不在一起,那么视为日期格式
|
|
|
+ Pattern pattern_XG = Pattern.compile(doubleSlashRegex_XG);
|
|
|
+ Matcher matcher_XG = pattern_XG.matcher(divValue);
|
|
|
+ if (matcher_XG.matches()) {
|
|
|
+ //判断日期格式yyyy/M/dd、yyyy/MM/dd
|
|
|
+ Pattern pattern_yyyyMdd = Pattern.compile(dateFormatRegex_yyyyMdd);
|
|
|
+ Pattern pattern_yyyyMMdd = Pattern.compile(dateFormatRegex_yyyyMMdd);
|
|
|
+ Matcher matcher_yyyyMdd = pattern_yyyyMdd.matcher(divValue);
|
|
|
+ Matcher matcher_yyyyMMdd = pattern_yyyyMMdd.matcher(divValue);
|
|
|
+ if (matcher_yyyyMdd.matches()) {
|
|
|
+ Date date = inputDateFormat.parse(divValue);
|
|
|
+ divValue = outputDateFormat.format(date);
|
|
|
+ } else if (matcher_yyyyMMdd.matches()) {
|
|
|
+ Date date = inputDateFormat.parse(divValue);
|
|
|
+ divValue = outputDateFormat.format(date);
|
|
|
}
|
|
|
- } else {
|
|
|
- resultDataMap.put(keyName, value);
|
|
|
}
|
|
|
+ //String mapKey = keyName + "***" + x1 + "_" + y1;
|
|
|
+ stringStringMap.put(keyName, divValue);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (workbook != null) {
|
|
|
+ workbook.dispose();
|
|
|
+ }
|
|
|
}
|
|
|
- if (resultDataMap.size() > 0) {
|
|
|
- List<Map.Entry<String, Object>> entryList = new ArrayList<>(resultDataMap.entrySet());
|
|
|
+ if (stringStringMap.size() > 0) {
|
|
|
+ List<Map.Entry<String, Object>> entryList = new ArrayList<>(stringStringMap.entrySet());
|
|
|
entryList.sort(new KeyComparator());
|
|
|
LinkedHashMap<String, Object> sortedMap = new LinkedHashMap<>();
|
|
|
for (Map.Entry<String, Object> entry : entryList) {
|
|
|
sortedMap.put(entry.getKey(), entry.getValue());
|
|
|
}
|
|
|
+ //删除临时文件信息
|
|
|
+ if (deleteFolder(Paths.get(importExcelTOHtmlPath))) {
|
|
|
+ logger.info("执行方法【importExcel】结束,删除临时文件成功!");
|
|
|
+ } else {
|
|
|
+ logger.info("执行方法【importExcel】结束,删除临时文件失败!");
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(url_1)) {
|
|
|
+ //删除临时文件夹
|
|
|
+ if (deleteFolderAndContents(Paths.get(url_1 + "/pdf/" + id + "_files"))) {
|
|
|
+ logger.info("执行方法【importExcel】结束,删除临时文件夹成功!");
|
|
|
+ } else {
|
|
|
+ logger.info("执行方法【importExcel】结束,删除临时文件夹失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
return R.data(sortedMap);
|
|
|
}
|
|
|
- return R.fail("没有获取到填写的数据");
|
|
|
+ return R.data(null, "没有获取到excel中的数据");
|
|
|
}
|
|
|
|
|
|
- private static boolean isValidDateFormat(String dateString, String formatPattern) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
|
|
|
- sdf.setLenient(false);
|
|
|
- try {
|
|
|
- Date date = sdf.parse(dateString);
|
|
|
- return date != null && sdf.format(date).equals(dateString);
|
|
|
- } catch (ParseException e) {
|
|
|
- return false;
|
|
|
+ private static boolean deleteFolder(Path folderPath) throws IOException {
|
|
|
+ if (Files.exists(folderPath)) {
|
|
|
+ Files.walk(folderPath)
|
|
|
+ .sorted(Comparator.reverseOrder())
|
|
|
+ .map(Path::toFile)
|
|
|
+ .forEach(File::delete);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean deleteFolderAndContents(Path folderPath) throws IOException {
|
|
|
+ if (Files.exists(folderPath)) {
|
|
|
+ Files.walk(folderPath)
|
|
|
+ .sorted(Comparator.reverseOrder())
|
|
|
+ .map(Path::toFile)
|
|
|
+ .forEach(File::delete);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getX1Attribute(Element element) {
|
|
|
+ return element.select("[x1]").attr("x1");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getY1Attribute(Element element) {
|
|
|
+ return element.select("[y1]").attr("y1");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getKeyNameFromChildElement(Element element) {
|
|
|
+ //TODO Element UI的时间标签待补全
|
|
|
+ String[] tagNames = {"el-input", "el-date-picker", "el-time-picker", "hc-form-select-search", "hc-table-form-upload", "hc-form-checkbox-group", "el-radio-group", "el-select"};
|
|
|
+ for (String tagName : tagNames) {
|
|
|
+ Element childElement = element.select(tagName).first();
|
|
|
+ if (childElement != null) {
|
|
|
+ String keyName = childElement.attr("keyname");
|
|
|
+ if (StringUtils.isNotEmpty(keyName)) {
|
|
|
+ return keyName;
|
|
|
+ } else {
|
|
|
+ return childElement.attr("id");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+ return "";
|
|
|
}
|
|
|
|
|
|
static class KeyComparator implements Comparator<Map.Entry<String, Object>> {
|