简体   繁体   English

打开 Apache POI 生成的 PPT 文件时出错

[英]Error while opening PPT File generated by Apache POI

点击“修复”后出错 打开ppt文件时出错

I am generating a powerpoint presentation using apache POI - XSLF, on the fly when the user clicks a certain link on my website.当用户单击我网站上的某个链接时,我正在使用 apache POI - XSLF 即时生成 powerpoint 演示文稿。 I have a few tables with data on my presentation file and also an image (Line chart) generated using jfreechart.我有几个表格,其中包含我的演示文件中的数据以及使用 jfreechart 生成的图像(折线图)。 When I open the PPTX on my machine it seems to work fine.当我在我的机器上打开 PPTX 时,它似乎工作正常。 However when I open the file on another machine that has the powerpoint 2013, I get the following error.但是,当我在另一台装有 powerpoint 2013 的机器上打开文件时,出现以下错误。

"powerpoint found a problem with content powerpoint can attempt to repair the presentation". “powerpoint 发现内容有问题,powerpoint 可以尝试修复演示文稿”。

I want to get rid of this error.我想摆脱这个错误。 I read on the internet that the solution is to "UNBLOCK" the powerpoint, which can be done through the properties section of the file.我在互联网上读到解决方案是“解锁”powerpoint,这可以通过文件的属性部分完成。 I am wondering if there's something I can do programmatically to suppress this errors for my users.我想知道是否可以通过编程方式为我的用户抑制此错误。 This error message is annoying at the least.这个错误信息至少很烦人。

My last thread on this was deleted - https://stackoverflow.com/questions/41163148/how-to-unblock-pptx-using-apache-poi我关于这个的最后一个帖子被删除了 - https://stackoverflow.com/questions/41163148/how-to-unblock-pptx-using-apache-poi

Hence re-creating this thread here again.因此再次在这里重新创建这个线程。 A bug is also entered in bugzilla for apache POI. bugzilla 中还为 apache POI 输入了一个错误。 Bug Id - 60633 ( https://bz.apache.org/bugzilla/show_bug.cgi?id=60633 ).错误 ID - 60633 ( https://bz.apache.org/bugzilla/show_bug.cgi?id=60633 )。

    XSLFTableCell cell
    XSLFTextParagraph p
    XSLFTextRun line

    XSLFTable tbl = slide.createTable();
    tbl.setAnchor(new Rectangle(X, Y, WIDTH, HEIGHT));

    XSLFTableRow headerRow = tbl.addRow();
    headerRow.setHeight(45);
    //Loop through the data collection and populate rows and columns. 
    for(int i = 0; i < numberOfCols; i++) {
    XSLFTableCell th = headerRow.addCell();
    p = th.addNewTextParagraph();
    p.setTextAlign(TextAlign.CENTER);
    line = p.addNewTextRun();.....}
    for (int item=0; item < 8; item++)
    {
    XSLFTableRow itemRow = tbl.addRow();.....}

   //finally write the file
   File pptFile = File.createTempFile("fileName", ".ppt")
   FileOutputStream out = new FileOutputStream(pptFile)
   ppt.write(out)
   out.close()

If one provides code along with a bug report, then this code must be complete and verifiable.如果提供代码和错误报告,则此代码必须完整且可验证。 Your code is not complete and verifiable.您的代码不完整且可验证。 And if i do completing it, then it works without problems.如果我确实完成了它,那么它就可以毫无问题地工作。

import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
import org.apache.poi.sl.usermodel.TableCell.BorderEdge;
import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;

import java.awt.Rectangle;
import java.awt.Point;
import java.awt.Color;

public class CreatePPTX {

 public static void main(String[] args) throws Exception {

  XMLSlideShow ppt = new XMLSlideShow();

  XSLFSlide slide = ppt.createSlide();

  XSLFTableCell cell;
  XSLFTextParagraph p;
  XSLFTextRun line;

  XSLFTable tbl = slide.createTable();
  tbl.setAnchor(new Rectangle(new Point(100, 100)));

  XSLFTableRow headerRow = tbl.addRow();
  headerRow.setHeight(45);

  for(int i = 0; i < 5; i++) {
   XSLFTableCell th = headerRow.addCell();
   p = th.addNewTextParagraph();
   p.setTextAlign(TextAlign.CENTER);
   line = p.addNewTextRun();
   line.setText("Header " + i);
   th.setBorderWidth(BorderEdge.bottom, 2.0);
   th.setBorderColor(BorderEdge.bottom, Color.black);
  }

  for (int item=0; item < 8; item++) {
   XSLFTableRow itemRow = tbl.addRow();
   for (int i = 0; i < 5; i++) {
    XSLFTableCell td = itemRow.addCell();
    p = td.addNewTextParagraph();
    p.setTextAlign(TextAlign.CENTER);
    line = p.addNewTextRun();
    line.setText("Cell " + item + ":" +i);    
   }
  }

  FileOutputStream out = new FileOutputStream("fileName.pptx");
  ppt.write(out);
  out.close();
 }
}

So your problem is not reproducible using the code you have provided.因此,使用您提供的代码无法重现您的问题。

But one thing can lead to your issue.但是有一件事可能会导致您的问题。 If cells shall be empty in the table, then do not create empty runs but let the cell totally empty.如果表格中的单元格为空,则不要创建空运行,而是让单元格完全为空。

Example with the above code, if cell 1:1 shall be empty, then do not:上面代码的例子,如果单元格 1:1 应该是空的,那么不要:

...
  for (int item=0; item < 8; item++) {
   XSLFTableRow itemRow = tbl.addRow();
   for (int i = 0; i < 5; i++) {
    XSLFTableCell td = itemRow.addCell();
    p = td.addNewTextParagraph();
    p.setTextAlign(TextAlign.CENTER);
    line = p.addNewTextRun();
    if (!(item==1 && i==1)) {
     line.setText("Cell " + item + ":" +i);
    }    
   }
  }
...

This leads to the error.这会导致错误。

Instead do:而是这样做:

...
  for (int item=0; item < 8; item++) {
   XSLFTableRow itemRow = tbl.addRow();
   for (int i = 0; i < 5; i++) {
    XSLFTableCell td = itemRow.addCell();
    p = td.addNewTextParagraph();
    p.setTextAlign(TextAlign.CENTER);
    if (!(item==1 && i==1)) {
     line = p.addNewTextRun();
     line.setText("Cell " + item + ":" +i);
    }    
   }
  }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM