2

参考POI - 在 Excel 中打开文件时无法写入文件?

我正在开发的Java 应用程序所需的功能是上述的 PowerPoint 版本:使用 Java,创建幻灯片并添加从捕获的应用程序数据生成的内容,然后将幻灯片添加到当前在 Microsoft PowerPoint 中打开的现有 PowerPoint 文件中。

如果没有打开 PowerPoint 文件,Java 应用程序将在必要时首先打开 Microsoft PowerPoint,然后创建一个新的空 PowerPoint 文件,然后在 Microsoft PowerPoint 中打开它。

例如,我的应用程序将创建Test.pptx. 然后该应用程序将在 Microsoft PowerPoint 应用程序中打开它。接下来,我添加幻灯片。操作成功并且没有抛出异常。但是,所做的更改不会反映在 Microsoft PowerPoint 视图中。

  • 如果我在 Microsoft PowerPoint 中关闭该文件并再次打开它,则可以看到更新后的更改。然而,这不是我想要的。用户必须能够不断地添加新幻灯片并即时查看这些幻灯片更新到 Microsoft PowerPoint 视图中,而无需在每次添加幻灯片后重新启动 Microsoft PowerPoint 应用程序。
  • 我注意到在 Microsoft PowerPoint 中打开 PowerPoint 文件时,~$Test.pptx会在我的桌面上创建一个临时文件。我试图让我的 Java 应用程序将生成的幻灯片直接添加到其中~Test.pptx,但FileNotFoundException: the process cannot access the file because it is being used by another process.随后被抛出。

该应用程序使用 Java 1.8 实现,在 Windows 10 上运行,并使用 Microsoft Office 2013。此特定功能的开发刚刚从头开始,因此仍在寻找和探索解决方案。

目前,我们正在使用 Apache POI,但如果这意味着解决问题,我们可以放弃使用它。有人提议尝试使用 Microsoft Office 宏。这个想法仍在探索中,因为我们不了解如何使用宏来确定它是否充分满足我们的要求。

如何实现所需的功能?我需要什么技术或库来完成这项工作?

示例代码取自 TutorialsPoint 的 Apache POI PPT 教程。现有的 PowerPoint 文件应该已经在 Microsoft PowerPoint 中打开,当运行以下代码时,我需要立即在 Microsoft PowerPoint 视图中看到添加的幻灯片。提醒:使用 Apache POI 不是强制性的。

public class EditPresentation {
  public static void main(String ar[]) throws IOException{
    //opening an existing slide show
    File file = new File("example1.pptx");
    FileInputStream inputstream=new FileInputStream(file);
    XMLSlideShow ppt = new XMLSlideShow(inputstream);

    //adding slides to the slodeshow
    XSLFSlide slide1 = ppt.createSlide();
    XSLFSlide slide2 = ppt.createSlide();

    //saving the changes 
    FileOutputStream out = new FileOutputStream(file);
    ppt.write(out);

    System.out.println("Presentation edited successfully");
    out.close();    
  }
} 
4

0 回答 0