| | |
| | | import com.yuanchu.mom.vo.SampleVo; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.poi.xwpf.usermodel.*; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.*; |
| | | import java.math.BigInteger; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Paths; |
| | | import java.text.SimpleDateFormat; |
| | |
| | | Set<String> delSet = new HashSet<>(); |
| | | List<ExcelDto> excelDtos = JSON.parseArray(tem, ExcelDto.class); |
| | | List<ExcelDto> mcList = new ArrayList<>(); |
| | | /* int aaaa = excelDtos.get(excelDtos.size() - 1).getR();//这里是模版里面的所有检测项的长度,先以15个项目分页 |
| | | int bbbb = 17; |
| | | int cccc = 0; |
| | | for (int p = 0; p < aaaa; p++) { |
| | | if (p % bbbb == 0) { |
| | | for (int y = 0; y < excelDtos.size(); y++) { |
| | | if (y == 0 || y == 1) { |
| | | if (excelDtos.get(y).getV().getMc() != null && excelDtos.get(y).getV().getMc().getCs() != null && excelDtos.get(y).getV().getMc().getRs() != null) { |
| | | mcList.add(excelDtos.get(y)); |
| | | } |
| | | } else { |
| | | try { |
| | | excelDtos.get(y + cccc - 2); |
| | | } catch (Exception e) { |
| | | continue; |
| | | } |
| | | if (excelDtos.get(y + cccc - 2).getV().getMc() != null && excelDtos.get(y + cccc - 2).getV().getMc().getCs() != null && excelDtos.get(y + cccc - 2).getV().getMc().getRs() != null) { |
| | | mcList.add(excelDtos.get(y + cccc - 2)); |
| | | } |
| | | } |
| | | } |
| | | cccc += 17; |
| | | } |
| | | }*/ |
| | | excelDtos.forEach(b -> { |
| | | if (b.getV().getMc() != null && b.getV().getMc().getCs() != null && b.getV().getMc().getRs() != null) { |
| | | mcList.add(b); |
| | |
| | | } else if (value.equals("试验方法")) { |
| | | textRenderData.setText(p.getMethodS()); |
| | | } else if (value.equals("检验项")) { |
| | | textRenderData.setText(p.getInspectionItem() + "&" + p.getInspectionItemEn()); |
| | | if (ObjectUtils.isEmpty(p.getInspectionItemEn())) { |
| | | textRenderData.setText(p.getInspectionItem()); |
| | | } else { |
| | | textRenderData.setText(p.getInspectionItem() + "&" + p.getInspectionItemEn()); |
| | | } |
| | | } else if (value.equals("检验子项")) { |
| | | if (ObjectUtils.isEmpty(p.getInspectionItemSubclassEn())) { |
| | | textRenderData.setText(p.getInspectionItemSubclass()); |
| | |
| | | textRenderData.setText(textRenderData.getText() + "∑" + jo1.get("mc")); |
| | | } |
| | | Style style = new Style(); |
| | | // style.setFontFamily(v.get("ff") == null ? "宋体" : v.get("ff") + ""); |
| | | style.setFontFamily("宋体"); |
| | | if (!((v.get("fc") + "").indexOf("rgb") > -1)) { |
| | | style.setColor(v.get("fc") == null ? "000000" : (v.get("fc") + "").replace("#", "")); |
| | |
| | | if (rowRenderData.getCells().size() != 0) { |
| | | rows.add(rowRenderData); |
| | | } |
| | | List<TableRenderData> tables1 = new ArrayList<>(); |
| | | TableRenderData tableRenderData = new TableRenderData(); |
| | | tableRenderData.setRows(rows); |
| | | int countSize = tableRenderData.getRows().get(0).getCells().size(); |
| | | for (RowRenderData row : tableRenderData.getRows()) { |
| | | /*for (CellRenderData cell : row.getCells()) { |
| | | System.out.print(cell.getParagraphs().get(0).getContents()); |
| | | } |
| | | System.out.println("");*/ |
| | | if (row.getCells().size() != countSize) { |
| | | throw new ErrorException("每行单元格不相等"); |
| | | } |
| | | tableRenderData.setRows(new ArrayList<>()); |
| | | |
| | | double totalHeight = 0.0; // 用于跟踪当前表格的总行高 |
| | | double heightThreshold = 8000.0; // 阈值,例如40cm*28px*15twips |
| | | List<RowRenderData> firstTwoRows = new ArrayList<>(); // 保存前两行以便复制到新表格 |
| | | |
| | | |
| | | // 保存前两行以便复制到新表格 |
| | | if (rows.size() >= 2) { |
| | | firstTwoRows.add(rows.get(0)); |
| | | firstTwoRows.add(rows.get(1)); |
| | | } |
| | | TableStyle tableStyle = new TableStyle(); |
| | | tableStyle.setColWidths(new int[]{650, 1600, 2000, 750, 2800, 1100, 1100}); |
| | | tableStyle.setWidth("10000"); |
| | | tableStyle.setAlign(TableRowAlign.CENTER); |
| | | BorderStyle borderStyle = new BorderStyle(); |
| | | borderStyle.setColor("000000"); |
| | | borderStyle.setType(XWPFTable.XWPFBorderType.THICK); |
| | | borderStyle.setSize(14); |
| | | tableStyle.setLeftBorder(borderStyle); |
| | | tableStyle.setTopBorder(borderStyle); |
| | | tableStyle.setRightBorder(borderStyle); |
| | | tableStyle.setBottomBorder(borderStyle); |
| | | tableRenderData.setTableStyle(tableStyle); |
| | | Map<String, Object> table = new HashMap<>(); |
| | | table.put("table", tableRenderData); |
| | | table.put("report", insReport); |
| | | tables.add(table); |
| | | |
| | | for (RowRenderData row : rows) { |
| | | double rowHeight = row.getRowStyle().getHeight(); // 获取当前行的行高 |
| | | totalHeight += rowHeight; // 更新总行高 |
| | | |
| | | if (totalHeight >= heightThreshold) { |
| | | // 创建新表格并复制前两行 |
| | | TableRenderData newTableRenderData = new TableRenderData(); |
| | | newTableRenderData.setRows(new ArrayList<>(firstTwoRows)); |
| | | |
| | | TableStyle tableStyle = new TableStyle(); |
| | | tableStyle.setColWidths(new int[]{650, 1600, 2000, 750, 2800, 1100, 1100}); |
| | | tableStyle.setWidth("10000"); |
| | | tableStyle.setAlign(TableRowAlign.CENTER); |
| | | BorderStyle borderStyle = new BorderStyle(); |
| | | borderStyle.setColor("000000"); |
| | | borderStyle.setType(XWPFTable.XWPFBorderType.THICK); |
| | | borderStyle.setSize(14); |
| | | tableStyle.setLeftBorder(borderStyle); |
| | | tableStyle.setTopBorder(borderStyle); |
| | | tableStyle.setRightBorder(borderStyle); |
| | | tableStyle.setBottomBorder(borderStyle); |
| | | tableRenderData.setTableStyle(tableStyle); |
| | | newTableRenderData.setTableStyle(tableStyle); |
| | | |
| | | tables1.add(tableRenderData); |
| | | tableRenderData = newTableRenderData; |
| | | totalHeight = rowHeight; |
| | | } |
| | | tableRenderData.getRows().add(row); |
| | | } |
| | | if (!tableRenderData.getRows().isEmpty()) { |
| | | TableStyle tableStyle = new TableStyle(); |
| | | tableStyle.setColWidths(new int[]{650, 1600, 2000, 750, 2800, 1100, 1100}); |
| | | tableStyle.setWidth("10000"); |
| | | tableStyle.setAlign(TableRowAlign.CENTER); |
| | | BorderStyle borderStyle = new BorderStyle(); |
| | | borderStyle.setColor("000000"); |
| | | borderStyle.setType(XWPFTable.XWPFBorderType.THICK); |
| | | borderStyle.setSize(14); |
| | | tableStyle.setLeftBorder(borderStyle); |
| | | tableStyle.setTopBorder(borderStyle); |
| | | tableStyle.setRightBorder(borderStyle); |
| | | tableStyle.setBottomBorder(borderStyle); |
| | | tableRenderData.setTableStyle(tableStyle); |
| | | tables1.add(tableRenderData); |
| | | } |
| | | |
| | | tables1.forEach(table -> { |
| | | Map<String, Object> tableMap = new HashMap<>(); |
| | | tableMap.put("table", table); |
| | | tableMap.put("report", insReport); |
| | | tables.add(tableMap); |
| | | }); |
| | | }); |
| | | }); |
| | | String url; |
| | |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | //处理合并单元格的问题 |
| | | // 处理合并单元格的问题 |
| | | String path = wordUrl + "/" + insReport.getCode().replace("/", "") + ".docx"; |
| | | try { |
| | | FileInputStream stream = new FileInputStream(path); |
| | | XWPFDocument document = new XWPFDocument(stream); |
| | | List<XWPFTable> xwpfTables = document.getTables(); |
| | | |
| | | for (int i = 1; i < xwpfTables.size() - (deviceList == null ? 1 : 2); 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++) { |
| | | for (int k = 0; k < xwpfTables.get(i).getRows().get(j).getTableCells().size(); k++) { |
| | | if (xwpfTables.get(i).getRows().get(j).getTableCells().get(k).getText().indexOf("∑") > -1) { |
| | | String[] split = xwpfTables.get(i).getRows().get(j).getTableCells().get(k).getText().split("∑"); |
| | | |
| | | XWPFTable table = xwpfTables.get(i); |
| | | for (int j = 0; j < table.getRows().size(); j++) { |
| | | XWPFTableRow row = table.getRow(j); |
| | | if (row == null) continue; |
| | | |
| | | for (int k = 0; k < row.getTableCells().size(); k++) { |
| | | XWPFTableCell cell = row.getCell(k); |
| | | if (cell == null || cell.getText() == null) continue; |
| | | |
| | | if (cell.getText().indexOf("∑") > -1) { |
| | | String[] split = cell.getText().split("∑"); |
| | | if (set1.add(split[1])) { |
| | | Map<String, Integer> map = new HashMap<>(); |
| | | map.put("sr", j); |
| | |
| | | map1.put("er", map1.get("er") + 1); |
| | | } |
| | | } |
| | | String str = xwpfTables.get(i).getRows().get(j).getTableCells().get(k).getText().split("∑")[0]; |
| | | xwpfTables.get(i).getRows().get(j).getTableCells().get(k).removeParagraph(0); |
| | | xwpfTables.get(i).getRows().get(j).getTableCells().get(k).setText(str); |
| | | xwpfTables.get(i).getRows().get(j).getTableCells().get(k).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); |
| | | xwpfTables.get(i).getRows().get(j).getTableCells().get(k).getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER); |
| | | |
| | | String str = split[0]; |
| | | cell.removeParagraph(0); |
| | | cell.setText(str); |
| | | cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); |
| | | |
| | | if (cell.getParagraphArray(0) != null) { |
| | | cell.getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | List<String> list = new ArrayList<>(); |
| | | for (String s : maps.keySet()) { |
| | | list.add(s); |
| | | } |
| | | |
| | | List<String> list = new ArrayList<>(maps.keySet()); |
| | | 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++) { |
| | | /* for (int j = 0; j < v.get("er") - v.get("sr") + 1; j++) { |
| | | if (v.get("ec") > v.get("sc")) { |
| | | TableTools.mergeCellsHorizonal(xwpfTables.get(i), v.get("sr") + j, v.get("sc"), v.get("ec")); |
| | | TableTools.mergeCellsHorizonal(table, v.get("sr") + j, v.get("sc"), v.get("ec")); |
| | | } |
| | | } |
| | | if (v.get("er") > v.get("sr")) { |
| | | TableTools.mergeCellsVertically(xwpfTables.get(i), v.get("sc"), v.get("sr"), v.get("er")); |
| | | TableTools.mergeCellsVertically(table, v.get("sc"), v.get("sr"), v.get("er")); |
| | | }*/ |
| | | for (int j = 0; j < v.get("er") - v.get("sr") + 1; j++) { |
| | | if (v.get("ec") > v.get("sc")) { |
| | | XWPFTableRow row = table.getRow(v.get("sr") + j); |
| | | if (row != null) { |
| | | for (int col = v.get("sc"); col <= v.get("ec"); col++) { |
| | | XWPFTableCell cell = row.getCell(col); |
| | | if (cell == null) { |
| | | row.createCell(); // 创建新的单元格以防空指针异常 |
| | | } |
| | | } |
| | | TableTools.mergeCellsHorizonal(table, v.get("sr") + j, v.get("sc"), v.get("ec")); |
| | | } |
| | | } |
| | | } |
| | | if (v.get("er") > v.get("sr")) { |
| | | for (int rowIdx = v.get("sr"); rowIdx <= v.get("er"); rowIdx++) { |
| | | XWPFTableRow row = table.getRow(rowIdx); |
| | | if (row != null) { |
| | | XWPFTableCell cell = row.getCell(v.get("sc")); |
| | | if (cell == null) { |
| | | row.createCell(); // 创建新的单元格以防空指针异常 |
| | | } |
| | | } |
| | | } |
| | | TableTools.mergeCellsVertically(table, v.get("sc"), v.get("sr"), v.get("er")); |
| | | } |
| | | } |
| | | } |
| | | |
| | | FileOutputStream fileOutputStream = new FileOutputStream(path); |
| | | document.write(fileOutputStream); |
| | | fileOutputStream.close(); |
| | |
| | | } |
| | | xwpfParagraph.setAlignment(ParagraphAlignment.CENTER); |
| | | } |
| | | /*if (k == 2 || k == 3 || k == 4) { |
| | | if (k == 2 && xwpfTables1.get(i).getRows().get(j).getTableCells().get(k-1).getText().contains("@@")) { |
| | | XWPFTableCell cell = xwpfTables1.get(i).getRow(j).getCell(k); |
| | | CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW(); |
| | | tblWidth.setType(STTblWidth.DXA); |
| | | tblWidth.setW(BigInteger.valueOf((int) (4.79 * 1440 / 2.54))); |
| | | } |
| | | if (k == 3 && xwpfTables1.get(i).getRows().get(j).getTableCells().get(k - 2).getText().contains("@@")) { |
| | | XWPFTableCell cell = xwpfTables1.get(i).getRow(j).getCell(k); |
| | | CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW(); |
| | | tblWidth.setType(STTblWidth.DXA); |
| | | tblWidth.setW(BigInteger.valueOf((int) (1.49 * 1440 / 2.54))); |
| | | } |
| | | if (k == 4 && xwpfTables1.get(i).getRows().get(j).getTableCells().get(k - 3).getText().contains("@@")) { |
| | | XWPFTableCell cell = xwpfTables1.get(i).getRow(j).getCell(k); |
| | | CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW(); |
| | | tblWidth.setType(STTblWidth.DXA); |
| | | tblWidth.setW(BigInteger.valueOf((int) (3.51 * 1440 / 2.54))); |
| | | } |
| | | } |
| | | if (xwpfTables1.get(i).getRows().get(j).getTableCells().get(k).getText().contains("@@")) { |
| | | String text = xwpfTables1.get(i).getRows().get(j).getTableCells().get(k).getText(); |
| | | text = text.replace("@@",""); |
| | | xwpfTables1.get(i).getRows().get(j).getTableCells().get(k).removeParagraph(0); |
| | | XWPFParagraph xwpfParagraph = xwpfTables1.get(i).getRows().get(j).getTableCells().get(k).addParagraph(); |
| | | XWPFRun run = xwpfParagraph.createRun(); |
| | | run.setText(text); |
| | | xwpfParagraph.setAlignment(ParagraphAlignment.CENTER); |
| | | }*/ |
| | | } |
| | | } |
| | | } |
| | | //处理检验结果跨页的问题 |
| | | FileOutputStream fileOutputStream1 = new FileOutputStream(path); |
| | | document1.write(fileOutputStream1); |
| | | fileOutputStream1.close(); |