I'm trying to create/save/preview an spreadsheet with JOpenDocument. I have read lots of examples but none one has created a spreadsheet on the fly. All off examples start loading one existent ODS. Finally, the create process work but I could not open the file saved with the ODSViewPanel. The file open well with LibreOffice but throw an exception in my code. Here is the code:
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();
}
}
}
If you run it you will see this exception:
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)
I could not create a valid StyleMasterPage and set it to the docuement. How can I create a valid ODS file to show it in one ODSViewerPanel?
I use this very straight forward way:
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"));