1

I trying to find a way to set borders to each cell and the optimal width to each column before creating the ods file [or either after the file has been created - i don't mind]. I'm writing the program in Java and I'm using the jopendocument library to create the ods file which is rich of method and tools but unfortunately I can't find any method in the documentation to set border or the optimal width for a column so i would like to know if anyone knows a library or way to do that.

public static void QuadernoFatture() throws FileNotFoundException, IOException {

    fatture.sort(Comparator.comparing(StrutturaFattura::getNumero));

    final Object[][] data = new Object[fatture.size()][7];
    for(int i=0;i<fatture.size();i++) {
        data[i]=new Object[] {fatture.get(i).cliente,fatture.get(i).numero,fatture.get(i).imponibile,fatture.get(i).iva,
                fatture.get(i).TotFatt,fatture.get(i).r_a,fatture.get(i).TotPagare};
    }

    fatture.clear();

    String[] columns = new String[] {"CLIENTE" , "N° FATTURA" , "IMPONIBILE" , "IVA" , "TOTALE FATTURA" , "R_ACCONTO" , "TOTALE DA PAGARE"};

    TableModel model = new DefaultTableModel(data,columns);

    Date now = new Date();
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM_yyyy");
    final File file = new File("/home/max/Documents/quadernoFatture/QuadernoFatture_" +dateFormatter.format(now).toString()+".ods");
    SpreadSheet.createEmpty(model).saveAs(file);
    OOUtils.open(file);


}
Krystian G
  • 2,842
  • 3
  • 11
  • 25
Maxuel
  • 127
  • 10

1 Answers1

2

I don't know if it's possible to set width matching content, but I can show you how to set specific width of a column. Replace line:

    SpreadSheet.createEmpty(model).saveAs(file);

with lines:

    SpreadSheet newSpreadSheet = SpreadSheet.createEmpty(model);
    newSpreadSheet.getFirstSheet().getColumn(0).setWidth(100);
    newSpreadSheet.getFirstSheet().getColumn(1).setWidth(200);
    newSpreadSheet.saveAs(file);

EDIT:

That was for version 1.3. For version 1.4rc2 use:

    newSpreadSheet.getFirstSheet().getColumn(0).setWidth(Length.MM(100));
    newSpreadSheet.getFirstSheet().getColumn(1).setWidth(Length.INCH(5));
Krystian G
  • 2,842
  • 3
  • 11
  • 25
  • Have you tried already? 'cause I've just tried and I get an error. It says that the method is not applicable for the arguments (int) ?any idea? – Maxuel Sep 05 '18 at 18:51
  • 1
    Yes, it works for me with recommended jopendocument 1.3. I'll take a look what changed in newer version. – Krystian G Sep 05 '18 at 20:02