2025-07-10
技术分享
00

目录

pom依赖
Excel2Utils

pom依赖

xml
<!-- https://mvnrepository.com/artifact/com.luhuiguo/aspose-cells --> <dependency> <groupId>com.luhuiguo</groupId> <artifactId>aspose-cells</artifactId> <!--非官方 luhuiguo--> <version>23.1</version> </dependency>

Excel2Utils

java
package top.zhoudeshui.utils; import com.aspose.cells.SaveFormat; import com.aspose.cells.Workbook; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.uuid.UUID; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FilenameUtils; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @Slf4j /** * Excel转换工具 - 支持Excel与PDF/其他格式互转 * @author zhoudeshui */ public class Excel2Utils { // 验证Aspose许可证(需自行配置) // static { // try { // // 加载许可证文件(如果有商业授权) //// License license = new License(); //// license.setLicense(new FileInputStream("path/to/license.xml")); // log.info("Aspose.Cells 许可证加载成功"); // } catch (Exception e) { // log.warn("Aspose.Cells 许可证未找到,将使用评估模式(可能有水印限制)"); // } // } // 输入流 -> 输出流 public static void convert(InputStream inputStream, OutputStream outputStream, int saveFormat) { try { Workbook workbook = new Workbook(inputStream); workbook.save(outputStream, saveFormat); outputStream.flush(); log.info("Excel转换成功:输入流 -> 输出流,格式: {}", getFormatName(saveFormat)); } catch (Exception e) { log.error("Excel转换失败:输入流 -> 输出流", e); throw new ServiceException("Excel转换失败:" + e.getMessage()); } } // 输入流 -> 输出文件 public static File convert(InputStream inputStream, String outputPath, int saveFormat) { try (FileOutputStream fos = new FileOutputStream(createOutputFile(outputPath))) { convert(inputStream, fos, saveFormat); return new File(outputPath); // 直接通过路径创建File对象(与原逻辑一致) } catch (Exception e) { log.error("Excel转换失败:输入流 -> 输出文件", e); throw new ServiceException("Excel转换失败:" + e.getMessage()); } } // 输入文件 -> 输出流 public static void convert(String inputPath, OutputStream outputStream, int saveFormat) { try (FileInputStream fis = new FileInputStream(inputPath)) { convert(fis, outputStream, saveFormat); } catch (Exception e) { log.error("Excel转换失败:输入文件 -> 输出流", e); throw new ServiceException("Excel转换失败:" + e.getMessage()); } } // 输入文件 -> 输出文件 public static File convert(String inputPath, String outputPath, int saveFormat) { try (FileInputStream fis = new FileInputStream(inputPath)) { return convert(fis, outputPath, saveFormat); } catch (Exception e) { log.error("Excel转换失败:输入文件 -> 输出文件", e); throw new ServiceException("Excel转换失败:" + e.getMessage()); } } // 处理MultipartFile输入 public static File convert(MultipartFile file, String outputPath, int saveFormat) { try { return convert(file.getInputStream(), outputPath, saveFormat); } catch (Exception e) { log.error("Excel转换失败:MultipartFile -> 输出文件", e); throw new ServiceException("Excel转换失败:" + e.getMessage()); } } // 辅助方法:创建输出文件 private static File createOutputFile(String outputPath) throws IOException { Path outputFilePath = Paths.get(outputPath); File outputFile = outputFilePath.toFile(); // 确保目录存在(合并嵌套的 if 语句) Path parentDirPath = outputFilePath.getParent(); if (parentDirPath != null) { File parentDir = parentDirPath.toFile(); if (!parentDir.exists() && !parentDir.mkdirs()) { throw new IOException("无法创建目录: " + parentDir.getAbsolutePath()); } } // 如果文件已存在,删除它(使用 Files.delete 获取更详细的错误信息) if (Files.exists(outputFilePath)) { try { Files.delete(outputFilePath); } catch (IOException e) { throw new IOException("无法删除已存在的文件: " + outputFile.getAbsolutePath(), e); } } return outputFile; } // 辅助方法:获取格式名称 private static String getFormatName(int saveFormat) { switch (saveFormat) { case SaveFormat.PDF: return "PDF"; case SaveFormat.XLSX: return "XLSX"; case SaveFormat.CSV: return "CSV"; case SaveFormat.HTML: return "HTML"; default: return String.valueOf(saveFormat); } } // 辅助方法:安静关闭流 private static void closeQuietly(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { log.warn("关闭流失败", e); } } } // 生成随机文件名 public static String generateRandomFileName(String originalFilename) { String extension = FilenameUtils.getExtension(originalFilename); return UUID.randomUUID().toString() + (extension.isEmpty() ? "" : "." + extension); } // 使用示例 public static void main(String[] args) { try { // 示例1:Excel转PDF(文件到文件) Excel2Utils.convert("C:\\Users\\zhoudeshui\\Desktop\\result.xlsx", "C:\\Users\\zhoudeshui\\Desktop\\output.pdf", SaveFormat.PDF); // 示例2:Excel转CSV(流到流) try (FileInputStream fis = new FileInputStream("input.xlsx"); FileOutputStream fos = new FileOutputStream("output.csv")) { Excel2Utils.convert(fis, fos, SaveFormat.CSV); } } catch (Exception e) { e.printStackTrace(); } } }

本文作者:周得水

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!