package ClassiTest;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jopendocument.dom.spreadsheet.MutableCell;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SheetTableModel.MutableTableModel;
import org.jopendocument.dom.spreadsheet.SpreadSheet;
//================================================================================
public class ProvaJOpenDocument {
public ProvaJOpenDocument() {
Person[] vPers = { new Person("Mario", "Rossi", 1984, Sex.MASCHIO),
new Person("Lucia", "Verdi", 1954, Sex.FEMMINA),
new Person("Roberto", "Bianchi", 1966, Sex.MASCHIO),
new Person("Giacomo", "Neri", 1954, Sex.MASCHIO),
new Person("Anna", "Marrone", 1975, Sex.FEMMINA),
new Person("Marco", "Viola", 1961, Sex.MASCHIO), };
SpreadSheet sprSheet = SpreadSheet.create(1, 1, 1);
Sheet sht = sprSheet.getFirstSheet();
sht.setName("Persone");
sht.ensureRowCount(vPers.length);
sht.ensureColumnCount(4);
MutableTableModel<SpreadSheet> tm = sht.getMutableTableModel(0, 0);
//--------------------------------------------------------------------------------
for (int ii = 0; ii < vPers.length; ii++) {
Person people = vPers[ii];
Color background = people.sesso == Sex.MASCHIO ? new Color(135, 206, 250) : new Color(255, 192, 203);
setCella(tm.getCellAt(ii, 0), people.nome, background);
setCella(tm.getCellAt(ii, 1), people.cognome, background);
setCella(tm.getCellAt(ii, 2), people.annoNascita, background);
setCella(tm.getCellAt(ii, 3), people.sesso, background);
}
try {
sprSheet.saveAs(new File("/Export/Persone.ods"));
} catch (IOException ex) {
Logger.getLogger(ProvaJOpenDocument.class.getName()).log(Level.SEVERE, null, ex);
}
}
//================================================================================
private static void setCella(MutableCell<SpreadSheet> cell, Object valore, Color background) {
cell.setValue(valore);
cell.setBackgroundColor(background);
}
}
//================================================================================
class Person {
public String nome;
public String cognome;
public int annoNascita;
public Sex sesso;
//================================================================================
public Person(String nome, String cognome, int annoNascita, Sex sesso) {
this.nome = nome;
this.cognome = cognome;
this.annoNascita = annoNascita;
this.sesso = sesso;
}
}
//================================================================================
enum Sex {
MASCHIO, FEMMINA
}