简体   繁体   中英

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. I have a few tables with data on my presentation file and also an image (Line chart) generated using jfreechart. When I open the PPTX on my machine it seems to work fine. However when I open the file on another machine that has the powerpoint 2013, I get the following error.

"powerpoint found a problem with content powerpoint can attempt to repair the presentation".

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. 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

Hence re-creating this thread here again. A bug is also entered in bugzilla for apache POI. Bug 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:

...
  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);
    }    
   }
  }
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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