jopendocument以编程方式创建电子表格并显示它

时间:2015-03-15 23:56:00

标签: java ods

我正在尝试使用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并将其设置为docuement。如何创建有效的ODS文件以在一个ODSViewerPanel中显示它?

1 个答案:

答案 0 :(得分:0)

我非常直接地使用这个方法:

    SpreadSheet spreadSheet = SpreadSheet.create(1,20,50);

    Sheet sheet = spreadSheet.getSheet(0);

    int lineIndex = 1;
    for (String value : valueset) {
        sheet.setValueAt(value,1, lineIndex);

        lineIndex++;
    }

    spreadSheet.saveAs(new File("/home/me/example.ods"));
相关问题