设为首页 加入收藏

TOP

用Aspose-Java免费实现 PDF、Word、Excel、Word互相转换并将转换过得文件上传OSS,返回转换后的文件路径(三)
2023-07-25 21:28:40 】 浏览:73
Tags:Aspose-Java 费实现 PDF Word Excel 相转换 文件上 OSS 后的文 路径
ath
= ossUpLoadTools.getSavePath() + "/" + fileName; FileOutputStream os = new FileOutputStream(filePath); Document doc = new Document(file[i].getInputStream()); doc.save(os); chilPdfPath[i] = filePath; os.close(); doc.close(); } // 合并多个PDF PdfFileEditor pdfFileEditor = new PdfFileEditor(); pdfFileEditor.concatenate(chilPdfPath, mergePdfPath); // 读取文件上传OSS File outputfile = new File(mergePdfPath); String resUrl = ossUpLoadTools.uploadOssFile(mergeFileName, outputfile); outputfile.delete(); return resUrl; } catch (Exception e) { e.printStackTrace(); } return null; }

三、Excel相关操作

1.引入相关依赖

        <dependency>
            <groupId>com.luhuiguo</groupId>
            <artifactId>aspose-cells</artifactId>
            <version>22.10</version>
        </dependency>

2.相关关键代码

    /**
      * @description: Excel转其他文件
      * @return: java.lang.String
      * @author: zhouhong
      * @date: 2023/5/1 23:44
      */
    @Override
    public String excelToFile(MultipartFile file, String type) {
        String checkType = FilenameUtils.getExtension(file.getOriginalFilename());
        if (!"xlsx".equals(checkType) && !"xls".equals(checkType)) {
            throw new ServiceException(1, "输入文件不是Excel文件!");
        }
        try {
            switch (type.toUpperCase()) {
                /******************** 文档类型 ***************/
                case "WORD" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.DOCX, "docx");
                }
                case "PDF" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.PDF, "pdf");
                }
                case "PPT" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.PPTX, "pptx");
                }
                case "HTML" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.HTML, "html");
                }
                case "JSON" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.JSON, ".json");
                }
                case "MARKDOWN" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.MARKDOWN, "md");
                }
                /***************** 图片类型 (注意图片格式的默认只转换第一个 Sheet1)*********************/
                case "PNG" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.PNG, "png");
                }
                case "JPG" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.JPG, "jpg");
                }
                case "BMP" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.BMP, "bmp");
                }
                case "CSV" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.CSV, "csv");
                }
                case "SVG" : {
                    return SwitchFile(file, com.aspose.cells.SaveFormat.SVG, "svg");
                }
                // 好像有问题,有需要大家自己调试一下
//                case "XML" : {
//                    return SwitchFile(file, com.aspose.cells.SaveFormat.XML, "xml");
//                }
                default:{}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private String SwitchFile(MultipartFile file, int saveFormat, String suffix) {
        String url = "";
        try {
            long old = System.currentTimeMillis();
            String fileName = UUID.randomUUID() + "." + suffix;
            String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
            FileOutputStream os = new FileOutputStream(filePath);
            //加载源文件数据
            Workbook excel = new Workbook(file.getInputStream());
            //设置转换文件类型并转换
            excel.save(os, saveFormat);
            os.close();
            File outputfile  = new File(filePath);
            url = o
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.MD语法入门,教你写好readme文档 下一篇Spring源码:Bean的生命周期(二)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目