簡體   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