Search in sources :

Example 1 with OdfSpreadsheetDocument

use of org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument in project OpenRefine by OpenRefine.

the class OdsExporter method export.

@Override
public void export(final Project project, Properties params, Engine engine, OutputStream outputStream) throws IOException {
    final OdfSpreadsheetDocument odfDoc;
    try {
        odfDoc = OdfSpreadsheetDocument.newSpreadsheetDocument();
    } catch (Exception e) {
        throw new IOException("Failed to create spreadsheet", e);
    }
    TabularSerializer serializer = new TabularSerializer() {

        OdfTable table;

        //int rowCount = 0;
        @Override
        public void startFile(JSONObject options) {
            table = OdfTable.newTable(odfDoc);
            table.setTableName(ProjectManager.singleton.getProjectMetadata(project.id).getName());
        }

        @Override
        public void endFile() {
        }

        @Override
        public void addRow(List<CellData> cells, boolean isHeader) {
            OdfTableRow r = table.appendRow();
            for (int i = 0; i < cells.size(); i++) {
                // implicitly creates cell
                OdfTableCell c = r.getCellByIndex(i);
                CellData cellData = cells.get(i);
                if (cellData != null && cellData.text != null && cellData.value != null) {
                    Object v = cellData.value;
                    if (v instanceof Number) {
                        c.setDoubleValue(((Number) v).doubleValue());
                    } else if (v instanceof Boolean) {
                        c.setBooleanValue(((Boolean) v).booleanValue());
                    } else if (v instanceof Date) {
                        Calendar cal = Calendar.getInstance();
                        cal.setTime((Date) v);
                        c.setDateValue(cal);
                    } else if (v instanceof Calendar) {
                        c.setDateValue((Calendar) v);
                    } else {
                        c.setStringValue(cellData.text);
                    }
                    if (cellData.link != null) {
                    // TODO: How do we do output hyperlinks?
                    }
                }
            }
        }
    };
    CustomizableTabularExporterUtilities.exportRows(project, engine, params, serializer);
    try {
        odfDoc.save(outputStream);
    } catch (Exception e) {
        throw new IOException("Error saving spreadsheet", e);
    }
    outputStream.flush();
}
Also used : Calendar(java.util.Calendar) OdfSpreadsheetDocument(org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument) IOException(java.io.IOException) IOException(java.io.IOException) Date(java.util.Date) OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) JSONObject(org.json.JSONObject) OdfTable(org.odftoolkit.odfdom.doc.table.OdfTable) List(java.util.List) JSONObject(org.json.JSONObject) OdfTableRow(org.odftoolkit.odfdom.doc.table.OdfTableRow)

Aggregations

IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 List (java.util.List)1 JSONObject (org.json.JSONObject)1 OdfSpreadsheetDocument (org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument)1 OdfTable (org.odftoolkit.odfdom.doc.table.OdfTable)1 OdfTableCell (org.odftoolkit.odfdom.doc.table.OdfTableCell)1 OdfTableRow (org.odftoolkit.odfdom.doc.table.OdfTableRow)1