我正在尝试使用 JOpenDocument 创建/保存/预览电子表格。我已经阅读了很多示例,但没有人即时创建电子表格。所有关闭示例都开始加载一个现有的 ODS。最后,创建过程工作,但我无法打开使用 ODSViewPanel 保存的文件。该文件可以使用 LibreOffice 很好地打开,但在我的代码中抛出异常。这是代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.swing.JFrame;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;
import org.jopendocument.model.OpenDocument;
import org.jopendocument.model.office.OfficeMasterStyles;
import org.jopendocument.panel.ODSViewerPanel;
public class Test {
private final static Logger LOGGER = Logger.getLogger(Test.class .getName());
public static void main(String[] args) {
new Test();
}
public Test() {
try {
// preparar el preview
final SpreadSheet ooSSheet = SpreadSheet.create(1, 1, 1);
final Sheet oosheet = ooSSheet.getSheet(0);
oosheet.setRowCount(5);
oosheet.setColumnCount(5);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
oosheet.setValueAt("DEMO", i, j);
}
}
// grabar la planilla para poder previsualizarla.
String tmpDir = System.getProperty("java.io.tmpdir");
String tmpOOSht = tmpDir+"/tmpoo.ods";
File tmpooFile = new File(tmpOOSht);
if (tmpooFile.exists())
tmpooFile.delete();
ooSSheet.saveAs(tmpooFile);
// Preview de los datos
final JFrame mainFrame = new JFrame("Viewer");
final OpenDocument ooDoc = new OpenDocument();
ooDoc.loadFrom(tmpOOSht);
ODSViewerPanel viewerPanel = new ODSViewerPanel(ooDoc);
mainFrame.setContentPane(viewerPanel);
mainFrame.pack();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocation(10, 10);
mainFrame.setVisible(true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
如果你运行它,你会看到这个异常:
SaxContentUnmarshaller.log() Sun Mar 15 20:38:09 ART 2015
content.xml : ignoring :office:document-content current:null
SaxContentUnmarshaller.log() Sun Mar 15 20:38:09 ART 2015
Dump attributes:
SaxContentUnmarshaller.log() Sun Mar 15 20:38:09 ART 2015
'table:style-name' -> 'ta0'
SaxContentUnmarshaller.log() Sun Mar 15 20:38:09 ART 2015
style.xml : ignoring :office:document-styles current:null
Exception in thread "main" java.lang.IllegalArgumentException: null is not a valid StyleMasterPage name
at org.jopendocument.model.office.OfficeMasterStyles.getMasterPageFromStyleName(Unknown Source)
at org.jopendocument.model.table.TableTable.getPageLayoutProperties(Unknown Source)
at org.jopendocument.model.OpenDocument.computePages(Unknown Source)
at org.jopendocument.model.OpenDocument.getPrintedPage(Unknown Source)
at org.jopendocument.renderer.ODTRenderer.<init>(Unknown Source)
at org.jopendocument.panel.ODSViewerPanel.<init>(Unknown Source)
at org.jopendocument.panel.ODSViewerPanel.<init>(Unknown Source)
at org.jopendocument.panel.ODSViewerPanel.<init>(Unknown Source)
at ar.gov.santafe.mpa.crosscall.Test.<init>(Test.java:68)
at ar.gov.santafe.mpa.crosscall.Test.main(Test.java:33)
我无法创建有效的 StyleMasterPage 并将其设置为文档。如何创建一个有效的 ODS 文件以在一个 ODSViewerPanel 中显示它?