use of org.odftoolkit.odfdom.doc.table.OdfTableRow 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(JsonNode 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 OffsetDateTime) {
OffsetDateTime odt = (OffsetDateTime) v;
c.setDateValue(ParsingUtilities.offsetDateTimeToCalendar(odt));
} 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();
}
use of org.odftoolkit.odfdom.doc.table.OdfTableRow in project pentaho-kettle by pentaho.
the class OdfSheet method getRow.
public KCell[] getRow(int rownr) {
if (rownr >= nrOfRows) {
throw new ArrayIndexOutOfBoundsException("Read beyond last row: " + rownr);
}
OdfTableRow row = table.getRowByIndex(rownr);
int cols = findNrColumns(row);
OdfCell[] xlsCells = new OdfCell[cols];
for (int i = 0; i < cols; i++) {
OdfTableCell cell = row.getCellByIndex(i);
if (cell != null) {
xlsCells[i] = new OdfCell(cell);
}
}
return xlsCells;
}
use of org.odftoolkit.odfdom.doc.table.OdfTableRow in project OpenRefine by OpenRefine.
the class OdsImporter method parseOneFile.
@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, InputStream inputStream, int limit, JSONObject options, List<Exception> exceptions) {
OdfDocument odfDoc;
try {
odfDoc = OdfDocument.loadDocument(inputStream);
} catch (Exception e) {
// Ugh! could they throw any wider exception?
exceptions.add(e);
return;
}
List<OdfTable> tables = odfDoc.getTableList();
int[] sheets = JSONUtilities.getIntArray(options, "sheets");
for (int sheetIndex : sheets) {
final OdfTable table = tables.get(sheetIndex);
final int lastRow = table.getRowCount();
TableDataReader dataReader = new TableDataReader() {
int nextRow = 0;
Map<String, Recon> reconMap = new HashMap<String, Recon>();
@Override
public List<Object> getNextRowOfCells() throws IOException {
if (nextRow > lastRow) {
return null;
}
List<Object> cells = new ArrayList<Object>();
OdfTableRow row = table.getRowByIndex(nextRow++);
if (row != null) {
int lastCell = row.getCellCount();
for (int cellIndex = 0; cellIndex <= lastCell; cellIndex++) {
Cell cell = null;
OdfTableCell sourceCell = row.getCellByIndex(cellIndex);
if (sourceCell != null) {
cell = extractCell(sourceCell, reconMap);
}
cells.add(cell);
}
}
return cells;
}
};
TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource + "#" + table.getTableName(), limit, options, exceptions);
}
}
use of org.odftoolkit.odfdom.doc.table.OdfTableRow in project OpenRefine by OpenRefine.
the class OdsImporter method parseOneFile.
@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, InputStream inputStream, int limit, ObjectNode options, List<Exception> exceptions) {
OdfDocument odfDoc;
try {
odfDoc = OdfDocument.loadDocument(inputStream);
} catch (Exception e) {
// Ugh! could they throw any wider exception?
exceptions.add(e);
return;
}
List<OdfTable> tables = odfDoc.getTableList();
ArrayNode sheets = JSONUtilities.getArray(options, "sheets");
for (int i = 0; i < sheets.size(); i++) {
String[] fileNameAndSheetIndex = new String[2];
ObjectNode sheetObj = JSONUtilities.getObjectElement(sheets, i);
// value is fileName#sheetIndex
fileNameAndSheetIndex = sheetObj.get("fileNameAndSheetIndex").asText().split("#");
if (!fileNameAndSheetIndex[0].equals(fileSource))
continue;
final OdfTable table = tables.get(Integer.parseInt(fileNameAndSheetIndex[1]));
final int lastRow = table.getRowCount();
TableDataReader dataReader = new TableDataReader() {
int nextRow = 0;
Map<String, Recon> reconMap = new HashMap<String, Recon>();
@Override
public List<Object> getNextRowOfCells() throws IOException {
if (nextRow > lastRow) {
return null;
}
List<Object> cells = new ArrayList<Object>();
OdfTableRow row = table.getRowByIndex(nextRow++);
int maxCol = 0;
if (row != null) {
int lastCell = row.getCellCount();
for (int cellIndex = 0; cellIndex <= lastCell; cellIndex++) {
Cell cell = null;
OdfTableCell sourceCell = row.getCellByIndex(cellIndex);
if (sourceCell != null) {
cell = extractCell(sourceCell, reconMap);
}
cells.add(cell);
if (cell != null && cellIndex > maxCol) {
maxCol = cellIndex;
}
}
}
// Right truncate null cells
return cells.subList(0, maxCol + 1);
}
};
TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource + "#" + table.getTableName(), limit, options, exceptions);
}
}
Aggregations