繁体   English   中英

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

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

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

当用户单击我网站上的某个链接时,我正在使用 apache POI - XSLF 即时生成 powerpoint 演示文稿。 我有几个表格,其中包含我的演示文件中的数据以及使用 jfreechart 生成的图像(折线图)。 当我在我的机器上打开 PPTX 时,它似乎工作正常。 但是,当我在另一台装有 powerpoint 2013 的机器上打开文件时,出现以下错误。

“powerpoint 发现内容有问题,powerpoint 可以尝试修复演示文稿”。

我想摆脱这个错误。 我在互联网上读到解决方案是“解锁”powerpoint,这可以通过文件的属性部分完成。 我想知道是否可以通过编程方式为我的用户抑制此错误。 这个错误信息至少很烦人。

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

因此再次在这里重新创建这个线程。 bugzilla 中还为 apache POI 输入了一个错误。 错误 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()

如果提供代码和错误报告,则此代码必须完整且可验证。 您的代码不完整且可验证。 如果我确实完成了它,那么它就可以毫无问题地工作。

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

因此,使用您提供的代码无法重现您的问题。

但是有一件事可能会导致您的问题。 如果表格中的单元格为空,则不要创建空运行,而是让单元格完全为空。

上面代码的例子,如果单元格 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);
    }    
   }
  }
...

这会导致错误。

而是这样做:

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