| | |
| | | import com.deepoove.poi.config.ConfigureBuilder; |
| | | import com.deepoove.poi.data.*; |
| | | import com.deepoove.poi.data.style.*; |
| | | import com.deepoove.poi.data.style.Style; |
| | | import com.deepoove.poi.util.TableTools; |
| | | import com.spire.doc.FileFormat; |
| | | import com.yuanchu.mom.dto.*; |
| | |
| | | import com.yuanchu.mom.mapper.*; |
| | | import com.yuanchu.mom.pojo.*; |
| | | import com.yuanchu.mom.vo.InsProductResult2VO; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | 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.docx4j.XmlUtils; |
| | | import org.docx4j.jaxb.Context; |
| | | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
| | | import org.docx4j.wml.*; |
| | | import org.docx4j.wml.STBrType; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.xml.bind.JAXBElement; |
| | | import java.io.*; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Paths; |
| | |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class WordUtils { |
| | | |
| | |
| | | } |
| | | //辐射表格 |
| | | List<Map<String, Object>> tables3 = new ArrayList<>(); |
| | | List<InsOrderFile> insOrderDocFiles = new ArrayList<>();; // 暂存需要合并表单到生成word文件的docx文件 |
| | | // 统计辐射实验表数量 |
| | | int radiationReportNum = 0; |
| | | if (strings.contains("近场") || strings.contains("远场")) { |
| | | /*辐射的试验表*/ |
| | | List<InsOrderFile> insOrderFiles = insOrderFileMapper.selectList(Wrappers.<InsOrderFile>lambdaQuery() |
| | | List<InsOrderFile> insOrderFiles = insOrderFileMapper.selectList(Wrappers.<InsOrderFile>lambdaQuery() |
| | | .eq(InsOrderFile::getInsOrderId, orderId) |
| | | .eq(InsOrderFile::getInsSampleId, insReportDto1.getSampleId()) |
| | | .like(InsOrderFile::getFileName, "解析的辐射站点报告") |
| | |
| | | .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(); |
| | | } |
| | | } |
| | | } |
| | | insOrderDocFiles.addAll(insOrderFiles); |
| | | radiationReportNum = insOrderFiles.size(); |
| | | // 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(); |
| | | // } |
| | | // } |
| | | // } |
| | | } |
| | | //辐射的标题 |
| | | String title3 = ""; |
| | | if (tables3.size() > 0) { |
| | | if (radiationReportNum > 0) { |
| | | title3 = "辐射方向图参数"; |
| | | } |
| | | //环境表格 |
| | |
| | | put("insProductList", insProductList); //检验情况一览表 |
| | | put("tables2", tables2); //检测结果 |
| | | put("title3", finalTitle); //检测结果 |
| | | put("tables3", tables3); //检测结果 |
| | | put("tables3", null); //检测结果 |
| | | put("tables4", tables4); //检测结果 |
| | | put("urlList", urlList); //样品照片 |
| | | put("insUsers", insUsers); //检测人员信息 |
| | |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | // //目录更新 |
| | | // try{ |
| | | // com.spire.doc.Document document = new com.spire.doc.Document(); |
| | | // document.loadFromFile(path); |
| | | // // 更新目录 |
| | | // document.updateTableOfContents(); |
| | | // // 保存文档 |
| | | // document.saveToFile(path, FileFormat.Docx); |
| | | // document.close(); |
| | | // } |
| | | // catch (Exception e) { |
| | | // throw new RuntimeException(e); |
| | | // } |
| | | // 更新表单 |
| | | try { |
| | | WordprocessingMLPackage targetDoc = WordprocessingMLPackage.load(new File(path)); |
| | | List<Object> targetContent = targetDoc.getMainDocumentPart().getContent(); |
| | | int targetIndex = findFirstTargetIndex(targetDoc, title3); |
| | | if (targetIndex != -1) { |
| | | List<Object> forms = new ArrayList<>(); |
| | | for (InsOrderFile insOrderFile : insOrderDocFiles) { |
| | | // 加载源文档并提取表单内容(假设为表格) |
| | | WordprocessingMLPackage sourceDoc = WordprocessingMLPackage.load(new File(wordUrl + "/" + insOrderFile.getFileUrl())); |
| | | List<Object> sourceContent = sourceDoc.getMainDocumentPart().getContent(); |
| | | for (Object obj : sourceContent) { |
| | | if (obj instanceof JAXBElement) { // 检查是否为 JAXBElement |
| | | JAXBElement jaxbElement = (JAXBElement)obj; |
| | | Tbl table = (Tbl) XmlUtils.deepCopy(jaxbElement.getValue()); |
| | | targetIndex++; |
| | | // 插入到目标段落之后 |
| | | targetContent.add(targetIndex, table); |
| | | forms.add(table); |
| | | // 插入分页符 |
| | | P paragraphWithPageBreak = Context.getWmlObjectFactory().createP(); |
| | | R run = Context.getWmlObjectFactory().createR(); |
| | | Br br = Context.getWmlObjectFactory().createBr(); |
| | | br.setType(STBrType.PAGE); // 设置分页符类型 |
| | | run.getContent().add(br); |
| | | paragraphWithPageBreak.getContent().add(run); |
| | | targetIndex++; |
| | | targetContent.add(targetIndex, paragraphWithPageBreak); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 保存修改后的文档 |
| | | targetDoc.save(new File(path)); |
| | | com.spire.doc.Document document = new com.spire.doc.Document(); |
| | | document.loadFromFile(path); |
| | | // 更新目录 |
| | | document.updateTableOfContents(); |
| | | // 保存文档 |
| | | document.saveToFile(path, FileFormat.Docx); |
| | | document.close(); |
| | | }catch (Exception e){ |
| | | log.error(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | //电路报告用于总报告的方法(天线) |
| | | private void getWord1(String term, InsOrderState insOrderState, List<Map<String, Object>> tables2) { |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 查找文本首次出现行 |
| | | private int findFirstTargetIndex(WordprocessingMLPackage targetDoc,String textStr){ |
| | | int targetIndex = -1; |
| | | if(StringUtils.isEmpty(textStr)){ |
| | | return targetIndex; |
| | | } |
| | | try { |
| | | List<Object> targetContent = targetDoc.getMainDocumentPart().getContent(); |
| | | // 查找目标段落 |
| | | for (int i = 0; i < targetContent.size(); i++) { |
| | | Object obj = targetContent.get(i); |
| | | if (obj instanceof P) { |
| | | P paragraph = (P) obj; |
| | | for (Object runObj : paragraph.getContent()) { |
| | | if (runObj instanceof R) { |
| | | R run = (R) runObj; |
| | | for (Object textObj : run.getContent()) { |
| | | if (textObj instanceof JAXBElement) { |
| | | JAXBElement jaxbElement = (JAXBElement) textObj; |
| | | if(jaxbElement.getValue() instanceof Text){ |
| | | Text text = (Text) jaxbElement.getValue(); |
| | | if (text.getValue().contains(textStr)) { |
| | | targetIndex = i; |
| | | return targetIndex; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("findFirstTargetIndex<<<<<<<<<<:{}",e.getMessage()); |
| | | }finally { |
| | | return targetIndex; |
| | | } |
| | | } |
| | | } |