fei
1 天以前 af47b774448bfa3ece4741664ce4d24128c8f629
ruoyi-admin/src/main/java/com/ruoyi/web/controller/archive/archiveAllExportController.java
@@ -8,10 +8,15 @@
import com.itextpdf.text.pdf.PdfWriter;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelExp;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.poi.ExcelUtilManySheet;
import com.ruoyi.common.utils.poi.ExcelUtilManySheetSecond;
import com.ruoyi.domain.DocumentMaterials;
import com.ruoyi.domain.vo.ArchiveInfoVo;
import com.ruoyi.domain.vo.DocumentMaterialsVo;
import com.ruoyi.domain.vo.DocumentMaterialsVoLarge;
import com.ruoyi.domain.vo.DocumentMaterialsVoSmall;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.service.IArchiveRecordsService;
import com.ruoyi.service.IDocumentMaterialsService;
@@ -36,9 +41,11 @@
import java.net.URLEncoder;
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.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
@@ -59,6 +66,68 @@
    @Autowired
    private IArchiveRecordsService iArchiveRecordsService;
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        //导出卷面封面代码
        ArchiveInfoVo aIV = iArchiveRecordsService.selectByRecordId(55L);
        List<ArchiveInfoVo> arsi = new ArrayList<>();
        arsi.add(aIV);
        String recordId = aIV.getRecordId();
        byte[] imgr = barcodeService.generateBarcodeImage(recordId);
        byte[] sedcode = pdfGenerateService.createQrCodeN(recordId, 30, 30);
        ExcelExp e1 = new ExcelExp("案卷封面数据",arsi, ArchiveInfoVo.class);
        ExcelExp e2 = new ExcelExp("案卷封面",  arsi, recordId, imgr,sedcode, ArchiveInfoVo.class);
        List<ExcelExp> mysheet = new ArrayList<ExcelExp>();
        mysheet.add(e1);
        mysheet.add(e2);
        ByteOutputStream bos1 = new ByteOutputStream();
        ExcelUtilManySheetSecond<List<ExcelExp>> util2 = new ExcelUtilManySheetSecond<List<ExcelExp>>(mysheet);
      //  util2.exportExcelManySheet(response, mysheet);
        //导出卷面目录代码
//        DocumentMaterials documentMaterials = new DocumentMaterials();
//        documentMaterials.setRecordId(55L);
//      //  List<DocumentMaterials> docs = documentMaterialsService.selectDocumentMaterialsList(documentMaterials);
//        List<DocumentMaterialsVo> dsvs = documentMaterialsService.findArchMInfo("55");
//      //  dsvs.get(0).setUrl("/profile/upload/2025/08/14/30_20250814212128A031.jpg");
//
//
//      //  dsvs.stream().map()
//        List<DocumentMaterialsVoSmall> list2 = dsvs.stream().map(res -> new DocumentMaterialsVoSmall(res.getNum(), res.getDocumentNumber(),res.getCreator(),
//                res.getTitle(), res.getDate(), res.getPageNumber(), res.getRemarks())).collect(Collectors.toList());
//
//        String recordId = dsvs.get(0).getRecordId();
//        byte[] imgr = barcodeService.generateBarcodeImage(recordId);
//        ExcelExp e1 = new ExcelExp("卷内目录数据",dsvs, DocumentMaterialsVo.class);
//        ExcelExp e2 = new ExcelExp("卷内数据", list2, recordId, imgr, DocumentMaterialsVoSmall.class);
//        List<ExcelExp> mysheet = new ArrayList<ExcelExp>();
//        mysheet.add(e1);
//        mysheet.add(e2);
//        ExcelUtilManySheet<List<ExcelExp>> util2 = new ExcelUtilManySheet<List<ExcelExp>>(mysheet);
       // util2.exportExcelManySheet(response, mysheet);
    }
    /**
     * 打包下载
     * @param response
@@ -70,7 +139,11 @@
        // 获取文件的保存位置,读取数据库,
        DocumentMaterials documentMaterials = new DocumentMaterials();
        documentMaterials.setRecordId(id);
        List<DocumentMaterials> docs = documentMaterialsService.selectDocumentMaterialsList(documentMaterials);
        List<DocumentMaterialsVoLarge> docs = documentMaterialsService.selectDocumentMaterialsAllByRecordId(id);
       System.out.println(docs.size()+"----009");
                //.selectDocumentMaterialsList(documentMaterials);
        List<DocumentMaterialsVo> dsvs = documentMaterialsService.findArchMInfo(id.toString());
        ArchiveInfoVo aIV = iArchiveRecordsService.selectByRecordId(id);
@@ -93,8 +166,10 @@
            String datumName = "user";
            //压缩文件
            List<String> filePathList = paths;
            File file = compressedFileToZip(docs, dsvs, aIV);
            File file = compressedFileToZip(docs, dsvs, aIV, id);
            System.out.println(file.getName());
            String fileName =aIV.getRecordId()+".zip";
            //输出文件流
            writeFileToRes(response, file.getName(), file);
            //删除压缩包
@@ -114,26 +189,7 @@
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        FileUtils.setAttachmentResponseHeader(response, realFileName);
        FileUtils.writeBytes(filePath, response.getOutputStream());
        //        FileInputStream inputStream = new FileInputStream(file);
//        //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型.
//        response.setContentType("application/octet-stream");
//        //2.设置文件头:最后一个参数是设置下载文件名
//        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//        response.addHeader("Content-Length", "" + file.length());
//
//        //3.通过response获取ServletOutputStream对象(out)
//        ServletOutputStream out = response.getOutputStream();
//
//        int b = 0;
//        byte[] buffer = new byte[1024];
//        while (b != -1) {
//            b = inputStream.read(buffer);
//            //4.写到输出流(out)中
//            out.write(buffer, 0, b);
//        }
//        out.flush();
//        out.close();
//        inputStream.close();
    }
    public String frontCompWithZore(int formatLength,int formatNumber){
        /**
@@ -146,82 +202,7 @@
        return newString;
    }
    private void handleXSSFImages(XSSFSheet sheet, Document pdfDoc) throws Exception {
        XSSFDrawing drawing = sheet.getDrawingPatriarch();
        if (drawing == null) return;
        for (XSSFShape shape : drawing.getShapes()) {
            if (shape instanceof XSSFPicture) {
                XSSFPicture pic = (XSSFPicture)shape;
                XSSFClientAnchor anchor = pic.getPreferredSize();
                // 获取图片数据
                byte[] bytes = pic.getPictureData().getData();
                Image pdfImage = Image.getInstance(bytes);
                // 设置图片位置和大小
                float imageWidth = pdfImage.getWidth();
                float imageHeight = pdfImage.getHeight();
                float scale = Math.min(
                        (PageSize.A4.getWidth() - 100) / imageWidth,
                        (PageSize.A4.getHeight() - 100) / imageHeight
                );
                pdfImage.scaleAbsolute(imageWidth * scale, imageHeight * scale);
                // 添加图片到PDF
                pdfDoc.add(pdfImage);
                pdfDoc.add(Chunk.NEWLINE);
            }
        }
    }
    private static String getCellValue(Cell cell,FormulaEvaluator evaluator) {
      //  System.out.println(cell);
        if (cell == null) return "";
        CellValue cellValue = evaluator.evaluate(cell);
        if(cellValue==null)
            return "";
      //  System.out.println(cellValue.getCellType());
        switch (cellValue.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                // 获取数值并转为整型
                double numericValue = cell.getNumericCellValue();
                // 判断是否为整数
                if (numericValue == (int)numericValue) {
                    if((int)numericValue==0)
                        return "";
                    else
                        return String.valueOf((int)numericValue);
                } else {
                    return String.valueOf(numericValue); // 或者根据需要返回四舍五入的整型
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula()+"";
            case BLANK:
                return "";
            default:
                return "";
        }
    }
    private int getRealLastCellNum(Row row) {
        int lastCellNum = 0;
        for (int i = 0; i < row.getLastCellNum(); i++) {
            Cell cell = row.getCell(i);
            if (cell != null && cell.getCellType() != CellType.BLANK) {
                lastCellNum = i + 1;
            }
        }
        return lastCellNum;
    }
@@ -252,10 +233,10 @@
    // 压缩文件
    private File compressedFileToZip(List<DocumentMaterials> docs, List<DocumentMaterialsVo> dsvs,   ArchiveInfoVo aIV) throws Exception {
    private File compressedFileToZip(List<DocumentMaterialsVoLarge> docs, List<DocumentMaterialsVo> dsvs,   ArchiveInfoVo aIV, Long id) throws Exception {
        //压缩包具体名称(拼接时间戳防止重名)
        String datumName = "";
        String zipFileName = datumName + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";
        String zipFileName =dsvs.get(0).getDocumentNumber()+aIV.getRecordId()+ ".zip";
        //生成压缩包存储地址(最后会删掉)
        String fileZip = RuoYiConfig.getProfile() + "/download/" + zipFileName;
        OutputStream os=null;
@@ -286,157 +267,112 @@
                        zos.write(buffer, 0, len);
                    }
                }
            //08-卷内卷内目录的pdf
            String pdf08Path= "08-卷内目录.pdf";
            List<DocumentMaterialsVo> list3 = dsvs;
            pdfGenerateService.generateFileDirectoryPdf(pdf08Path, list3);
            ZipEntry zipEntry2 = new ZipEntry(pdf08Path);
            zos.putNextEntry(zipEntry2);
            // 读取PDF文件内容并写入ZIP
            try (FileInputStream fis = new FileInputStream(pdf08Path)) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
            }
                //09-备考表.pdf
            String pdf09Path = "09-备考表.pdf";
            pdfGenerateService.generateFileStyleInfo(pdf09Path, aIV.getRecordId(), id);
            // 2. 压缩PDF到ZIP文件
            // 添加PDF文件到ZIP
            ZipEntry zipEntry1 = new ZipEntry(pdf09Path);
            zos.putNextEntry(zipEntry1);
            // 读取PDF文件内容并写入ZIP
            try (FileInputStream fis = new FileInputStream(pdf09Path)) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
            }
                //excel 卷内封面导出zip
            ZipEntry entryiv = new ZipEntry("卷内封面" + ".xls");
            ExcelUtil<ArchiveInfoVo> utilsv = new ExcelUtil<ArchiveInfoVo>(ArchiveInfoVo.class);
            List<ArchiveInfoVo> arsi = new ArrayList<>();
            arsi.add(aIV);
            String recordId1 = aIV.getRecordId();
            byte[] imgr1 = barcodeService.generateBarcodeImage(recordId1);
            byte[] sedcode = pdfGenerateService.createQrCodeN(recordId1, 30, 30);
            ExcelExp e3 = new ExcelExp("案卷封面数据",arsi, ArchiveInfoVo.class);
            ExcelExp e4 = new ExcelExp("案卷封面",  arsi, recordId1, imgr1,sedcode, ArchiveInfoVo.class);
            List<ExcelExp> mysheet1 = new ArrayList<ExcelExp>();
            mysheet1.add(e3);
            mysheet1.add(e4);
            ByteOutputStream bos2 = new ByteOutputStream();
            ExcelUtilManySheetSecond<List<ExcelExp>> util3 = new ExcelUtilManySheetSecond<List<ExcelExp>>(mysheet1);
            util3.exportExcelManySheet(bos2, mysheet1);
          //  System.out.println(bos2);
            zos.putNextEntry(entryiv);
            ByteOutputStream boss = new ByteOutputStream();
            List<ArchiveInfoVo> aivs = new ArrayList<>();
            aivs.add(aIV);
            utilsv.byteOutputStreamExcel(boss, aivs,"Date List", "");
            boss.writeTo(zos);
//            ExcelUtil<ArchiveInfoVo> utilsv = new ExcelUtil<ArchiveInfoVo>(ArchiveInfoVo.class);
//
//
//            ByteOutputStream boss = new ByteOutputStream();
//            List<ArchiveInfoVo> aivs = new ArrayList<>();
//            aivs.add(aIV);
//            utilsv.byteOutputStreamExcel(boss, aivs,"Date List", "");
            bos2.writeTo(zos);
                //写入电子目录  xsxl
            ZipEntry entry = new ZipEntry("电子文件目录" + ".xls");
            ExcelUtil<DocumentMaterials> util = new ExcelUtil<DocumentMaterials>(DocumentMaterials.class);
            ExcelUtil<DocumentMaterialsVoLarge> util = new ExcelUtil<DocumentMaterialsVoLarge>(DocumentMaterialsVoLarge.class);
            zos.putNextEntry(entry);
            ByteOutputStream bos = new ByteOutputStream();
            util.byteOutputStreamExcel(bos, docs,"Date List", "");
            util.byteOutputStreamExcel(bos, docs,"电子文件目录", "");
            bos.writeTo(zos);
            //拿到卷内目录的excel
            List<DocumentMaterialsVoSmall> list2 = dsvs.stream().map(res -> new DocumentMaterialsVoSmall(res.getNum(), res.getDocumentNumber(),res.getCreator(),
                    res.getTitle(), res.getDate(), res.getPageNumber(), res.getRemarks())).collect(Collectors.toList());
            String recordId = dsvs.get(0).getRecordId();
            byte[] imgr = barcodeService.generateBarcodeImage(recordId);
            ExcelExp e1 = new ExcelExp("卷内目录数据",dsvs, DocumentMaterialsVo.class);
            ExcelExp e2 = new ExcelExp("卷内数据", list2, recordId, imgr, DocumentMaterialsVoSmall.class);
            List<ExcelExp> mysheet = new ArrayList<ExcelExp>();
            mysheet.add(e1);
            mysheet.add(e2);
            ExcelUtilManySheet<List<ExcelExp>> util2 = new ExcelUtilManySheet<List<ExcelExp>>(mysheet);
            ZipEntry entr = new ZipEntry("卷内目录" + ".xls");
            ExcelUtil<DocumentMaterialsVo> util1 = new ExcelUtil<DocumentMaterialsVo>(DocumentMaterialsVo.class);
            System.out.println(dsvs);
            zos.putNextEntry(entr);
            ByteOutputStream bos1 = new ByteOutputStream();
            util1.byteOutputStreamExcel(bos1, dsvs,"Date List", "");
            util2.exportExcelManySheet(bos1, mysheet);
         //   util1.byteOutputStreamExcel(bos1, dsvs,"Date List", "");
            bos1.writeTo(zos);
            //把excel转为pdf
            try {
                // 1. 读取Excel文件
                String excelPath = RuoYiConfig.getProfile() + "/download/"+"卷內目录.xls";
                String pdfPath = RuoYiConfig.getProfile() + "/download/"+"1.pdf";
                Workbook workbook = WorkbookFactory.create(new File(excelPath));
                FileOutputStream fos = new FileOutputStream(pdfPath);
                FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
                // 获取第一个工作表
                    Sheet sheet = workbook.getSheetAt(1);
                    // 创建PDF文档对象
                    Document document = new Document(PageSize.A2, 50, 50, 50, 50);
                    // 创建PDF输出流
                    PdfWriter writer = PdfWriter.getInstance(document, fos);
                // 4. 处理工作表中的图片
                if (workbook instanceof XSSFWorkbook) {
                    handleXSSFImages((XSSFSheet)sheet, document);
                }
                    // 打开PDF文档
                    document.open();
                    // 创建PDF表格对象
                System.out.println(sheet.getRow(0).getLastCellNum()+"--00");
                    PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
                    table.setHeaderRows(1);
                    // 设置表格宽度
                    table.setWidthPercentage(100);
                    // 设置表格标题
                    Paragraph title = new Paragraph(sheet.getSheetName(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
                    title.setAlignment(Element.ALIGN_CENTER);
                    document.add(title);
                   // System.out.println(sheet.)
                    // 添加表格内容
                    for (Row row : sheet) {
                        {
                            if (row == null) continue;
                            System.out.println(getRealLastCellNum(row)+"---987");
//                            if(row.getRowNum()==0)
//                                continue;
                            if(row.getRowNum()==2)
                            {
                                PdfPCell pdfCell = ImageSet(100);
                                pdfCell.setColspan(7);
                                pdfCell.setRowspan(3);
                                pdfCell.setBorderWidth(1f);
                                pdfCell.setBorderColor(BaseColor.BLACK);
                                pdfCell.setPadding(5f);
                                pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                                pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                table.addCell(pdfCell);
                                System.out.println("{{{{{{{{{{{{{");
                                continue;
                            }
                            for (int i = 0; i < 7; i++)
                            {
                                Cell cell = row.getCell(i);
                                if(row.getRowNum()==3)
                                {
                                    PdfPCell pdfCell = new PdfPCell(new Paragraph("卷内目录", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
                                    pdfCell.setColspan(7);
                                    pdfCell.setBorderWidth(1f);
                                    pdfCell.setBorderColor(BaseColor.BLACK);
                                    pdfCell.setPadding(5f);
                                    pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                                    pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    table.addCell(pdfCell);
                                    System.out.println("{{{{{{{{{{{{{");
                                    break;
                                }
                                if(cell==null) {
                                    table.addCell("");
                                    continue;
                                }
                                PdfPCell pdfCell = new PdfPCell(new Paragraph(getCellValue(cell, evaluator), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
                                if(row.getRowNum()!=1) {
                                    pdfCell.setBorderWidth(1f);
                                    pdfCell.setBorderColor(BaseColor.BLACK);
                                    pdfCell.setPadding(5f);
                                    pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                                    pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                }
                                if (cell.getRowIndex() == 5) {
                                    pdfCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                                }
                                table.addCell(pdfCell);
                            }
                        }
                    }
                    // 添加表格到PDF文档
                    table.setSpacingBefore(20f);
                    table.setSpacingAfter(20f);
                    table.setKeepTogether(true);
                    document.add(table);
                    // 关闭PDF文档
                    document.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            boolean res = true;
            byte[] buf = new byte[1024];
            for (DocumentMaterials dc : docs) {
            for (DocumentMaterialsVoLarge dc : docs) {
                String filePath = dc.getUrl();
                filePath = filePath.replace("/profile/", RuoYiConfig.getProfile() + "/");