| | |
| | | import com.yuanchu.mom.service.ManageRecordCheckService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.yuanchu.mom.utils.QueryWrappers; |
| | | import com.yuanchu.mom.utils.XWPFDocumentUtils; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.poi.xwpf.usermodel.*; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Paths; |
| | | import java.time.LocalDate; |
| | |
| | | * 文件审批记录(含修订后再次审批记录) 服务实现类 |
| | | * </p> |
| | | * |
| | | * @author 芯导软件(江苏)有限公司 |
| | | * @author |
| | | * @since 2024-11-12 02:31:36 |
| | | */ |
| | | @Service |
| | |
| | | } |
| | | |
| | | @Override |
| | | public String exportOutManageRecordCheck(ManageRecordCheck manageRecordCheck, HttpServletResponse response) { |
| | | public void exportOutManageRecordCheck(ManageRecordCheck manageRecordCheck, HttpServletResponse response) { |
| | | List<ManageRecordCheck> manageRecordCheckList = manageRecordCheckMapper.pageManageRecordCheck(new Page(-1, -1), QueryWrappers.queryWrappers(manageRecordCheck)).getRecords(); |
| | | //生成检验报告发放登记表 |
| | | String url; |
| | | try { |
| | | InputStream inputStream = this.getClass().getResourceAsStream("/static/check-deal.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); |
| | | } |
| | | InputStream inputStream = this.getClass().getResourceAsStream("/static/check-deal.docx"); |
| | | ConfigureBuilder builder = Configure.builder(); |
| | | builder.useSpringEL(true); |
| | | |
| | |
| | | tableRenderData.setTableStyle(tableStyle); |
| | | Map<String, Object> table = new HashMap<>(); |
| | | table.put("check", tableRenderData); |
| | | table.put("index1", index1); |
| | | checkList.add(table); |
| | | index1++; |
| | | } |
| | | } |
| | | Integer finalIndex = index1; |
| | | XWPFTemplate template = XWPFTemplate.compile(url, builder.build()).render( |
| | | XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render( |
| | | new HashMap<String, Object>() {{ |
| | | put("size", finalIndex); |
| | | put("checkList", checkList); |
| | | }}); |
| | | String name = UUID.randomUUID() + "_文件审批记录" + ".docx"; |
| | | // 处理换行问题 |
| | | XWPFDocumentUtils.updateMergeByDocument(template.getXWPFDocument()); |
| | | try { |
| | | template.writeAndClose(Files.newOutputStream(Paths.get(wordUrl + "/" + name))); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | response.setContentType("application/msword"); |
| | | String fileName = URLEncoder.encode( |
| | | "文件审批记录", "UTF-8"); |
| | | response.setHeader("Content-disposition", |
| | | "attachment;filename=" + fileName + ".docx"); |
| | | OutputStream os = response.getOutputStream(); |
| | | template.write(os); |
| | | os.flush(); |
| | | os.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("导出失败"); |
| | | } |
| | | //处理中英文换行的问题 |
| | | String path = wordUrl + "/" + name; |
| | | try { |
| | | FileInputStream stream1 = new FileInputStream(path); |
| | | XWPFDocument document1 = new XWPFDocument(stream1); |
| | | List<XWPFTable> xwpfTables1 = document1.getTables(); |
| | | for (int i = 0; i < xwpfTables1.size(); i++) { |
| | | for (int j = 0; j < xwpfTables1.get(i).getRows().size(); j++) { |
| | | for (int k = 0; k < xwpfTables1.get(i).getRows().get(j).getTableCells().size(); k++) { |
| | | if (xwpfTables1.get(i).getRows().get(j).getTableCells().get(k).getText().contains("@")) { |
| | | String text = xwpfTables1.get(i).getRows().get(j).getTableCells().get(k).getText(); |
| | | String[] split = text.split("@"); |
| | | 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(split[0]); |
| | | if (ObjectUtils.isNotNull(split[1])) { |
| | | run.addBreak(); |
| | | run.setText(split[1]); |
| | | } |
| | | xwpfParagraph.setAlignment(ParagraphAlignment.CENTER); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | FileOutputStream fileOutputStream1 = new FileOutputStream(path); |
| | | document1.write(fileOutputStream1); |
| | | fileOutputStream1.close(); |
| | | } catch (FileNotFoundException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return name; |
| | | } |
| | | |
| | | @Override |