| | |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.poi.openxml4j.util.ZipSecureFile; |
| | | import org.apache.poi.xwpf.usermodel.*; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.mock.web.MockMultipartFile; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.*; |
| | |
| | | if (inspectionItemSubclass.contains("互调")) { |
| | | aa += (angles + 1) * portRow * often; |
| | | } |
| | | } |
| | | else { |
| | | } else { |
| | | if (inspectionItemSubclass.contains("电压驻波比")) { |
| | | aa += (angles + 1) * portRow; |
| | | } |
| | |
| | | paragraphRenderDataList.add(paragraphRenderData); |
| | | cellRenderData.setParagraphs(paragraphRenderDataList); |
| | | cells.add(cellRenderData); |
| | | } |
| | | else { |
| | | } else { |
| | | if (insOrderState.getVersion() == 1) { |
| | | //非电调版本(简单版) |
| | | if (inspectionItemSubclass.contains("电压驻波比") && i <= 2 * portRow) { |
| | |
| | | cells.add(cellRenderData); |
| | | } |
| | | } |
| | | } |
| | | else { |
| | | } else { |
| | | //电调版本(复杂版) |
| | | if (inspectionItemSubclass.contains("电压驻波比") && i <= (angles + 1) * portRow) { |
| | | cc = (angles + 1) * portRow; |
| | |
| | | lable.getAndIncrement(); |
| | | }); |
| | | }); |
| | | String url; |
| | | try { |
| | | ZipSecureFile.setMinInflateRatio(0.0001); |
| | | InputStream inputStream = this.getClass().getResourceAsStream("/static/word1.docx"); |
| | | File file = File.createTempFile("temp", ".tmp"); |
| | | OutputStream outputStream = new FileOutputStream(file); |
| | | IOUtils.copy(inputStream, outputStream); |
| | | url = file.getAbsolutePath(); |
| | | } catch (FileNotFoundException e) { |
| | | throw new ErrorException("找不到模板文件"); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | ZipSecureFile.setMinInflateRatio(0.0001); |
| | | InputStream inputStream = this.getClass().getResourceAsStream("/static/word1.docx"); |
| | | |
| | | ConfigureBuilder builder = Configure.builder(); |
| | | builder.useSpringEL(true); |
| | | XWPFTemplate template = XWPFTemplate.compile(url, builder.build()).render( |
| | | XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render( |
| | | new HashMap<String, Object>() {{ |
| | | put("title", title); |
| | | put("tables", tables); |
| | |
| | | FileInputStream stream = new FileInputStream(path); |
| | | XWPFDocument document = new XWPFDocument(stream); |
| | | List<XWPFTable> xwpfTables = document.getTables(); |
| | | for (int i = 1; i < xwpfTables.size(); i++) { |
| | | for (int i = 0; i < xwpfTables.size(); i++) { |
| | | Set<String> set1 = new HashSet<>(); |
| | | Map<String, Map<String, Integer>> maps = new HashMap<>(); |
| | | for (int j = 0; j < xwpfTables.get(i).getRows().size(); j++) { |
| | |
| | | .in(InsProductResult::getInsProductId, productIds)); |
| | | for (InsProductResult insProductResult : insProductResults) { |
| | | List<JSONObject> jsonObjects = JSON.parseArray(insProductResult.getEquipValue(), JSONObject.class); |
| | | for (JSONObject jsonObject : jsonObjects) { |
| | | String value = jsonObject.getString("v"); |
| | | if (value != null && !value.isEmpty()) { |
| | | deviceSet.add(value); |
| | | if (ObjectUtils.isNotEmpty(jsonObjects)) { |
| | | for (JSONObject jsonObject : jsonObjects) { |
| | | String value = jsonObject.getString("v"); |
| | | if (value != null && !value.isEmpty()) { |
| | | deviceSet.add(value); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | if (strings.contains("近场") || strings.contains("远场")) { |
| | | Map<String, String> table = new HashMap<>(); |
| | | table.put("indexs", indexs + ""); |
| | | indexs += 1; |
| | | table.put("term", "辐射方向图参数"); |
| | | table.put("result", "不判定"); |
| | | tables.add(table); |
| | | /*辐射的试验表*/ |
| | | List<InsOrderFile> insOrderFiles = insOrderFileMapper.selectList(Wrappers.<InsOrderFile>lambdaQuery() |
| | | .eq(InsOrderFile::getInsOrderId, orderId) |
| | | .like(InsOrderFile::getFileName, "解析的辐射站点报告") |
| | | .and(wrapper -> wrapper |
| | | .eq(InsOrderFile::getSonLaboratory, "远场") |
| | | .or() |
| | | .eq(InsOrderFile::getSonLaboratory, "近场") |
| | | )); |
| | | if (insOrderFiles.size() > 0) { |
| | | int aa = 0; |
| | | for (InsOrderFile insOrderFile : insOrderFiles) { |
| | | try { |
| | | XWPFDocument circuitParamsDoc = new XWPFDocument(new FileInputStream(wordUrl + "/" + insOrderFile.getFileUrl())); |
| | | // 遍历电路参数文件的所有元素,段落和表格 |
| | | for (IBodyElement element : circuitParamsDoc.getBodyElements()) { |
| | | Map<String, Object> table3 = new HashMap<>(); |
| | | TableRenderData tableData = new TableRenderData(); |
| | | List<RowRenderData> rows = new ArrayList<>(); |
| | | if (element instanceof XWPFTable) { |
| | | aa += 1; |
| | | XWPFTable tab = (XWPFTable) element; |
| | | List<XWPFTableRow> row = tab.getRows(); |
| | | for (int i = 0; i < row.size(); i++) { |
| | | RowRenderData rowRenderData = new RowRenderData(); |
| | | List<CellRenderData> cells = new ArrayList<>(); |
| | | List<XWPFTableCell> cell = row.get(i).getTableCells(); |
| | | for (int j = 0; j < cell.size(); j++) { |
| | | CellRenderData cellRenderData = new CellRenderData(); |
| | | List<ParagraphRenderData> paragraphRenderDataList = new ArrayList<>(); |
| | | ParagraphRenderData paragraphRenderData = new ParagraphRenderData(); |
| | | ParagraphStyle paragraphStyle = new ParagraphStyle(); |
| | | paragraphStyle.setAlign(ParagraphAlignment.CENTER); |
| | | paragraphRenderData.setParagraphStyle(paragraphStyle); |
| | | List<RenderData> renderData = new ArrayList<>(); |
| | | TextRenderData textRenderData = new TextRenderData(); |
| | | Style style = new Style(); |
| | | style.setFontFamily("宋体"); |
| | | style.setColor("000000"); |
| | | textRenderData.setStyle(style); |
| | | CTTcPr tcPr = cell.get(j).getCTTc().getTcPr(); |
| | | if (tcPr != null) { |
| | | //合并列 |
| | | CTHMerge hMerge = tcPr.getHMerge(); |
| | | if (ObjectUtils.isNotEmpty(hMerge)) { |
| | | if (STMerge.RESTART.equals(hMerge.getVal()) || STMerge.CONTINUE.equals(hMerge.getVal())) { |
| | | int index = j; |
| | | String text = null; |
| | | while (text == null || text.equals("")) { |
| | | if (index < 0) { |
| | | text = "/"; |
| | | } |
| | | text = cell.get(index).getText(); |
| | | index--; |
| | | } |
| | | textRenderData.setText(text + "∑88" + aa + i); |
| | | } |
| | | } |
| | | //合并行 |
| | | CTVMerge vMerge = tcPr.getVMerge(); |
| | | if (ObjectUtils.isNotEmpty(vMerge)) { |
| | | if (STMerge.RESTART.equals(vMerge.getVal()) || STMerge.CONTINUE.equals(vMerge.getVal())) { |
| | | int index = i; |
| | | String text = null; |
| | | while (text == null || text.equals("")) { |
| | | if (index < 0) { |
| | | text = "/"; |
| | | break; |
| | | } |
| | | text = row.get(index).getCell(j).getText(); |
| | | index--; |
| | | } |
| | | if (text.equals("合格") || text.equals("不合格")) { |
| | | textRenderData.setText(text + "∑25" + aa + j); |
| | | } else { |
| | | textRenderData.setText(text + "∑22" + aa + j); |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | textRenderData.setText(cell.get(j).getText()); |
| | | } |
| | | renderData.add(textRenderData); |
| | | paragraphRenderData.setContents(renderData); |
| | | paragraphRenderDataList.add(paragraphRenderData); |
| | | cellRenderData.setParagraphs(paragraphRenderDataList); |
| | | cells.add(cellRenderData); |
| | | } |
| | | rowRenderData.setCells(cells); |
| | | rows.add(rowRenderData); |
| | | } |
| | | tableData.setRows(rows); |
| | | table3.put("table3", tableData); |
| | | tables3.add(table3); |
| | | } |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if (strings.contains("电路试验")) { |
| | | strings.remove("电路试验"); |
| | |
| | | Map<String, Object> table4 = new HashMap<>(); |
| | | table4.put("table4", tableRenderData); |
| | | tables4.add(table4); |
| | | } |
| | | else { |
| | | } else { |
| | | Map<String, String> table = new HashMap<>(); |
| | | table.put("indexs", indexs + ""); |
| | | table.put("term", "电路参数"); |
| | |
| | | for (InsReportDto1 insReportDto1 : insReportDto1s) { |
| | | InsOrderUser insOrderUser = insOrderUserMapper.selectById(insReportDto1.getInsOrderUsersId()); |
| | | InsOrderState orderState = insOrderStateMapper.selectById(insOrderUser.getInsOrderStateId()); |
| | | if (orderState.getLaboratory().equals("电路试验")){ |
| | | if (orderState.getLaboratory().equals("电路试验")) { |
| | | orderState.setNum(insOrderUser.getNum()); |
| | | getWord1(insOrderUser.getTerm(),orderState,insSamples,tables2); |
| | | getWord1(insOrderUser.getTerm(), orderState, insSamples, tables2); |
| | | } |
| | | } |
| | | } |
| | |
| | | tables4.add(table4); |
| | | } |
| | | } |
| | | |
| | | String url; |
| | | try { |
| | | ZipSecureFile.setMinInflateRatio(0.0001); |
| | | InputStream inputStream = this.getClass().getResourceAsStream("/static/report-template.docx"); |
| | | File file = File.createTempFile("temp", ".tmp"); |
| | | OutputStream outputStream = new FileOutputStream(file); |
| | | IOUtils.copy(inputStream, outputStream); |
| | | url = file.getAbsolutePath(); |
| | | } catch (FileNotFoundException e) { |
| | | throw new ErrorException("找不到模板文件"); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | ZipSecureFile.setMinInflateRatio(0.0001); |
| | | InputStream inputStream = this.getClass().getResourceAsStream("/static/report-template.docx"); |
| | | StringBuilder standardMethod2 = new StringBuilder(); |
| | | for (String s : standardMethod) { |
| | | standardMethod2.append(";\n").append(s); |
| | |
| | | environment = (ObjectUtils.isNotEmpty(insProduct.getTemperature()) ? insProduct.getTemperature() + "℃ " : "") + (ObjectUtils.isNotEmpty(insProduct.getHumidity()) ? insProduct.getHumidity() + "%" : ""); |
| | | String finalEnvironment = environment; |
| | | List<SampleProductDto> finalSamples = samples; |
| | | XWPFTemplate template = XWPFTemplate.compile(url, builder.build()).render( |
| | | String title3 = ""; |
| | | if (tables3.size() > 0) { |
| | | title3 = "辐射方向图参数"; |
| | | } |
| | | String finalTitle = title3; |
| | | XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render( |
| | | new HashMap<String, Object>() {{ |
| | | put("order", insOrder); |
| | | put("report", insReport); |
| | |
| | | put("deviceList", finalDeviceList); |
| | | put("tables", tables); |
| | | put("tables2", tables2); |
| | | put("title3", finalTitle); |
| | | put("tables3", tables3); |
| | | put("tables4", tables4); |
| | | put("sampleList", sampleList); |
| | |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | // 处理合并单元格的问题 |
| | | String path = wordUrl + "/" + insReport.getCode().replace("/", "") + ".docx"; |
| | | // 处理合并单元格的问题 |
| | | try { |
| | | ZipSecureFile.setMinInflateRatio(0.0001); |
| | | FileInputStream stream = new FileInputStream(path); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 单元格排序, 避免格式错乱 |
| | | List<Map.Entry<String, Map<String, Integer>>> entries = new ArrayList<>(maps.entrySet()); |
| | | entries.sort((o1, o2) -> o1.getValue().get("sc") - o2.getValue().get("sc")); |
| | | |
| | | // 按照顺序添加进集合 |
| | | List<String> list = new ArrayList<>(); |
| | | for (Map.Entry<String, Map<String, Integer>> entry : entries) { |
| | | list.add(entry.getKey()); |
| | | } |
| | | |
| | | for (int a = list.size() - 1; a >= 0; a--) { |
| | | Map<String, Integer> v = maps.get(list.get(a)); |
| | | for (int j = 0; j < v.get("er") - v.get("sr") + 1; j++) { |
| | | if (v.get("ec") > v.get("sc")) { |
| | | try { |
| | | TableTools.mergeCellsHorizonal(xwpfTables.get(i), v.get("sr") + j, v.get("sc"), v.get("ec")); |
| | | mergeCellsHorizontally(xwpfTables.get(i), v.get("sr") + j, v.get("sc"), v.get("ec")); |
| | | // TableTools.mergeCellsHorizonal(xwpfTables.get(i), v.get("sr") + j, v.get("sc"), v.get("ec")); |
| | | } catch (Exception e) { |
| | | } |
| | | } |
| | | } |
| | | if (v.get("er") > v.get("sr")) { |
| | | try { |
| | | TableTools.mergeCellsVertically(xwpfTables.get(i), v.get("sc"), v.get("sr"), v.get("er")); |
| | | mergeCellsVertically(xwpfTables.get(i), v.get("sc"), v.get("sr"), v.get("er")); |
| | | // TableTools.mergeCellsVertically(xwpfTables.get(i), v.get("sc"), v.get("sr"), v.get("er")); |
| | | } catch (Exception e) { |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | //电路报告用于总报告的方法 |
| | | private void getWord1(String term, InsOrderState insOrderState,List<InsSample> insSamples,List<Map<String, Object>> tables2){ |
| | | private void getWord1(String term, InsOrderState insOrderState, List<InsSample> insSamples, List<Map<String, Object>> tables2) { |
| | | AtomicInteger lable = new AtomicInteger(1); |
| | | AtomicInteger index = new AtomicInteger(); |
| | | String title = term + "电路参数"; |
| | |
| | | if (inspectionItemSubclass.contains("互调")) { |
| | | aa += (angles + 1) * portRow * often; |
| | | } |
| | | } |
| | | else { |
| | | } else { |
| | | if (inspectionItemSubclass.contains("电压驻波比")) { |
| | | aa += (angles + 1) * portRow; |
| | | } |
| | |
| | | paragraphRenderDataList.add(paragraphRenderData); |
| | | cellRenderData.setParagraphs(paragraphRenderDataList); |
| | | cells.add(cellRenderData); |
| | | } |
| | | else { |
| | | } else { |
| | | if (insOrderState.getVersion() == 1) { |
| | | //非电调版本(简单版) |
| | | if (inspectionItemSubclass.contains("电压驻波比") && i <= 2 * portRow) { |
| | |
| | | cells.add(cellRenderData); |
| | | } |
| | | } |
| | | } |
| | | else { |
| | | } else { |
| | | //电调版本(复杂版) |
| | | if (inspectionItemSubclass.contains("电压驻波比") && i <= (angles + 1) * portRow) { |
| | | cc = (angles + 1) * portRow; |
| | |
| | | }); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | // 水平合并单元格 |
| | | private static void mergeCellsHorizontally(XWPFTable table, int row, int fromCol, int toCol) { |
| | | for (int i = fromCol; i <= toCol; i++) { |
| | | if (i == fromCol) { |
| | | table.getRow(row).getCell(i).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); |
| | | } else { |
| | | table.getRow(row).getCell(i).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 垂直合并单元格 |
| | | private static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) { |
| | | for (int i = fromRow; i <= toRow; i++) { |
| | | if (i == fromRow) { |
| | | table.getRow(i).getCell(col).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); |
| | | } else { |
| | | table.getRow(i).getCell(col).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); |
| | | } |
| | | } |
| | | } |
| | | } |