如何使用 Apache Poi 和 XSLF 创建 OpenXML 格式的 PowePoint 演示文稿?

发布于 2024-11-26 09:02:33 字数 1844 浏览 1 评论 0原文

如果我访问 Apache POI XSLF 应该有 OLE2 和 OpenXML 规范的示例,但是只有基于 OLE2 的可怕幻灯片布局格式示例。

有人可以帮我解决 XML 幻灯片布局格式示例吗? API 完全不同。

它与 spreadsheet 不同,只需将 HSSFWorkbook 的实现更改为 XSSFWorkbook。

如果采用 XSLF 实现,这会是什么样子? POI 显然无法从头开始创建文档,因此我们需要一个现有的空虚拟文档,对吧?

      //table data              
      String[][] data = {
          {"INPUT FILE", "NUMBER OF RECORDS"},
          {"Item File", "11,559"},
          {"Vendor File", "300"},
          {"Purchase History File", "10,000"},
          {"Total # of requisitions", "10,200,038"}
      };

      SlideShow ppt = new SlideShow();

      Slide slide = ppt.createSlide();
      //create a table of 5 rows and 2 columns
      Table table = new Table(5, 2);
      for (int i = 0; i < data.length; i++) {
          for (int j = 0; j < data[i].length; j++) {
              TableCell cell = table.getCell(i, j);
              cell.setText(data[i][j]);

              RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
              rt.setFontName("Arial");
              rt.setFontSize(10);

              cell.setVerticalAlignment(TextBox.AnchorMiddle);
              cell.setHorizontalAlignment(TextBox.AlignCenter);
          }
      }

      //set table borders
      Line border = table.createBorder();
      border.setLineColor(Color.black);
      border.setLineWidth(1.0);
      table.setAllBorders(border);

      //set width of the 1st column
      table.setColumnWidth(0, 300);
      //set width of the 2nd column
      table.setColumnWidth(1, 150);

      slide.addShape(table);
      table.moveTo(100, 100);

      FileOutputStream out = new FileOutputStream(file);
      ppt.write(out);
      out.close();

If I go to Apache POI XSLF there should be samples for both OLE2 and OpenXML specs, but there are only the OLE2 based Horrible Slide Layout Format examples.

Could please anybody help me out with XML Slide Layout Format example ? The API is quite different.

It is not like with spreadsheet where one just change the implementation of HSSFWorkbook to XSSFWorkbook.

How would this look like with XSLF implementation ? POI apparently can't create a document from scratch, so we need an existing empty dummy document, right ?

      //table data              
      String[][] data = {
          {"INPUT FILE", "NUMBER OF RECORDS"},
          {"Item File", "11,559"},
          {"Vendor File", "300"},
          {"Purchase History File", "10,000"},
          {"Total # of requisitions", "10,200,038"}
      };

      SlideShow ppt = new SlideShow();

      Slide slide = ppt.createSlide();
      //create a table of 5 rows and 2 columns
      Table table = new Table(5, 2);
      for (int i = 0; i < data.length; i++) {
          for (int j = 0; j < data[i].length; j++) {
              TableCell cell = table.getCell(i, j);
              cell.setText(data[i][j]);

              RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
              rt.setFontName("Arial");
              rt.setFontSize(10);

              cell.setVerticalAlignment(TextBox.AnchorMiddle);
              cell.setHorizontalAlignment(TextBox.AlignCenter);
          }
      }

      //set table borders
      Line border = table.createBorder();
      border.setLineColor(Color.black);
      border.setLineWidth(1.0);
      table.setAllBorders(border);

      //set width of the 1st column
      table.setColumnWidth(0, 300);
      //set width of the 2nd column
      table.setColumnWidth(1, 150);

      slide.addShape(table);
      table.moveTo(100, 100);

      FileOutputStream out = new FileOutputStream(file);
      ppt.write(out);
      out.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-12-03 09:02:33

它还没有实现,org.apache.poi版本3.8-beta3,什么时候实现我很不知道。

XMLSlideShow.java

public MasterSheet createMasterSheet() throws IOException {
    throw new IllegalStateException("Not implemented yet!");
}
public Slide createSlide() throws IOException {
    throw new IllegalStateException("Not implemented yet!");
}

It is not implemented yet, org.apache.poi version 3.8-beta3, when it will be implemented is very unknown to me.

XMLSlideShow.java

public MasterSheet createMasterSheet() throws IOException {
    throw new IllegalStateException("Not implemented yet!");
}
public Slide createSlide() throws IOException {
    throw new IllegalStateException("Not implemented yet!");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文