Java源码示例:org.odftoolkit.simple.table.Table
示例1
@Override
public long test() throws IOException {
try {
// Open the file.
this.logger.info("testSimpleOdf: filling a " + this.getRowCount() + " rows, " +
this.getColCount() + " columns spreadsheet");
final long t1 = System.currentTimeMillis();
final SpreadsheetDocument document = SpreadsheetDocument.newSpreadsheetDocument();
final Table table = document.appendSheet("test");
for (int y = 0; y < this.getRowCount(); y++) {
final Row row = table.appendRow();
for (int x = 0; x < this.getColCount(); x++) {
row.getCellByIndex(x)
.setStringValue(String.valueOf(this.getRandom().nextInt(1000)));
}
}
document.save(new File("generated_files", "simpleodf_benchmark.ods").getPath());
final long t2 = System.currentTimeMillis();
this.logger.info("Filled in " + (t2 - t1) + " ms");
return t2 - t1;
} catch (final Exception e) {
throw new IOException(e);
}
}
示例2
public static String getText(SpreadsheetDocument spreadsheetDocument) {
try {
StringBuilder stringBuilder = new StringBuilder("");
int sheetCount = spreadsheetDocument.getSheetCount();
for(int i=0; i<sheetCount; i++) {
Table sheet = spreadsheetDocument.getSheetByIndex(i);
int rowCount = sheet.getRowCount();
for(int y=0; y<rowCount; y++) {
Row row = sheet.getRowByIndex(y);
int cellCount = row.getCellCount();
for(int x=0; x<cellCount; x++) {
Cell cell = row.getCellByIndex(x);
String value = cell.getStringValue();
stringBuilder.append(value);
stringBuilder.append("\t");
}
stringBuilder.append("\n");
}
stringBuilder.append("\n\n");
}
return stringBuilder.toString();
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
示例3
@Override
protected void importDatasource(Datasource datasource, List<String> geographyScope, List<String> temporalScope, List<String> datasourceLocation) throws Exception {
// Check the datasourceid to reference the corresponding url
String whichDatasource = datasource.getDatasourceSpec().getId();
String fileLocation = getDatasourceSpec(whichDatasource).getUrl();
URL url = new URL(fileLocation);
InputStream isr = downloadUtils.fetchInputStream(url, getProvider().getLabel(), ".ods");
SpreadsheetDocument workbook = SpreadsheetDocument.loadDocument(isr);
// Getting only the first sheet
Table sheet = workbook.getSheetByIndex(0);
Iterator<Row> rowIterator = sheet.getRowIterator();
// This dataset contains both subject types
SubjectType localauthority = SubjectTypeUtils.getSubjectTypeByProviderAndLabel(AbstractONSImporter.PROVIDER.getLabel(), getOaDatasourceIds().get(0));
SubjectType englandboundaries = SubjectTypeUtils.getSubjectTypeByProviderAndLabel(AbstractONSImporter.PROVIDER.getLabel(), getOaDatasourceIds().get(1));
int ignore = 0;
while (ignore++ < 7) {
rowIterator.next();
}
// Getting the indices of the columns containing the data.
int columnIndex = 0;
List<Integer> columnLoop = new ArrayList<>();
while ( columnIndex++ <= 24) {
if (!(columnIndex % 5 == 0 && columnIndex != 0)) {
// +1 one to match the fact that the values do not start from column 0
columnLoop.add(columnIndex + 1);
}
}
Row rowTime = sheet.getRowByIndex(3);
String year = rowTime.getCellByIndex(0).getStringValue();
LocalDateTime timestamp = TimedValueUtils.parseTimestampString("20" + year.substring(year.length() - 2));
log.info("Time is presented in the dataset as {} and we persist it as {}", year, timestamp);
List<TimedValue> timedValues = new ArrayList<>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
String geograghy = row.getCellByIndex(0).getDisplayText().trim();
Subject subject = SubjectUtils.getSubjectByTypeAndLabel(localauthority, geograghy);
subject = subject != null ? subject : SubjectUtils.getSubjectByTypeAndLabel(englandboundaries, geograghy);
if (subject != null) {
for (int i = 0; i < columnLoop.size(); i++) {
Double record = row.getCellByIndex(columnLoop.get(i)).getDoubleValue();
Attribute attribute = datasource.getTimedValueAttributes().get(i);
timedValues.add(new TimedValue(subject, attribute, timestamp, record/100.));
}
} else {
log.warn("Could not find subject for {}", geograghy);
}
}
saveAndClearTimedValueBuffer(timedValues);
}
示例4
private static void addTitlePage(TextDocument outputDocument) throws Exception {
addLines(outputDocument, 2);
Paragraph para = outputDocument.addParagraph(null);
// add company logo
addImage(para, extension.getParams().getLogoFileName(), 0);
addLines(outputDocument, 4);
// Lets write a big header
para = outputDocument.addParagraph(null);
para.setTextContent(extension.getParams().getTitleReport());
para.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
para.setFont(fontTitleTextReport);
addLines(outputDocument, 4);
para = outputDocument.addParagraph(null);
para.setTextContent(extension.getParams().getCustomerName());
para.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
para.setFont(fontTitleTextReport);
addLines(outputDocument, 15);
Table table = outputDocument.addTable(2, 1);
Cell cell = table.getCellByPosition(0, 0);
cell.setStringValue(
extension
.getMessages()
.getString("alertreport.export.message.export.pdf.confidential"));
cell.setFont(fontSmallBold);
String color = Color.toSixDigitHexRGB("#87cefa");
cell.setCellBackgroundColor(Color.valueOf(color));
Cell cell1 = table.getCellByPosition(0, 1);
cell1.setStringValue(extension.getParams().getConfidentialText());
cell1.setHorizontalAlignment(HorizontalAlignmentType.JUSTIFY);
cell1.setFont(fontSmall);
// outputDocument.addPageBreak();
}