Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: How to create a.odt document formatted in Java

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to create a.odt document formatted in Java

    Hi,

    With Java 17 I can create a .ods document through the class: org.jopendocument.dom.spreadsheet.SpreadSheet
    but I can't format the contents of columns, rows and cells because I have to populate it with a DefaultTableModel.

    In practice, I would like to be able to set for example:
    for all file, font style and size,
    for each column, width, and alignment,
    for each row the height, bold, alignment, etc.
    for each cell the bold, italic, text color, etc.

    Is this something that can be done? And with which library?
    Can any pious soul give me a hand?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    Do you have a small, complete program that compiles and executes to demonstrate the problem?

    Do you have a link for the documentation for the package you are using?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to create a.odt document formatted in Java

    Well a complete program is not small.
    I report the actions I do. It's very simple.

    I load a JTable with a: DefaultTableModel modTable; loaded from a db table.
    then I declare a file with:
    File fiFileName = new File("/Directory/FileName.ods");
    and then I create the file ods with:
    SpreadSheet.createEmpty(modTable).saveAs(fiFileNam e);

    The documentation is here: https://www.jopendocument.org/docs/o...readSheet.html
    Last edited by ZioCrick; December 17th, 2024 at 04:11 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    Sorry, without code to work with, i won't be able to help. I have no experience with the package you are using.
    My suggestion is to read the API documentation for the package to see what methods will help.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to create a.odt document formatted in Java

    I have done a simple example.
    Unfortunately I can't attach it

    As you can see there it is possible to use only setValue and setBackgroundColor
    In the documentation there is also the setStyleName but it is not enough.

    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
    }
    Last edited by Norm; December 17th, 2024 at 12:51 PM. Reason: Added code tags

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    There is no main method in the posted code. How do I execute the code for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Re: How to create a.odt document formatted in Java

    AH yes! because I have a test program with a Button that does:

    private void BtnSaveSpreadActionPerformed(java.awt.event.Action Event evt) {
    new ProvaJOpenDocument();

    because ProvaJOpenDocument is just a class and not a JFrame.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    I added a main method that creates an instance of the class. Nothing happens when I execute it. What do you expect the above posted code to do when it is executed? How does it show the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to create a.odt document formatted in Java

    The program only creates an ods file with lines with a different background color depending on the gender of the person.
    What I would like to get instead is the ability to set the characteristics of columns, rows and cells, as you can do by hand in an ods file.
    All there.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    Can you create an class that extends the Cell class and have it control how it is shown in the table?
    Do the jopendocument classes support a custom Cell class?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to create a.odt document formatted in Java

    Unfortunately JOPENDOCUMENT is very limited and that's exactly why I asked for help, to check if there is another solution.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    The only other source for spreadsheet products I know of is Apache.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to create a.odt document formatted in Java

    Can you please give me a link or give an example?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to create a.odt document formatted in Java

    Dear Norm, the links you indicated to me talk about OpenOffice.
    Since I use LibreOffice somebody suggested me to use the LibreOffice UNO API.

    I also found and downloaded from the web the following example:
    https://api.libreoffice.org/examples...eetSample.java
    that seems to solve all my needs.

    So from https://www.libreoffice.org/download...d-libreoffice/
    I downloaded and installed the LibreOffice_24.8.3_Linux_x86-64_deb.tar.gz package.

    Then I downloaded the package LibreOffice_24.8.4_Linux_x86-64_rpm_sdk.tar.gz
    I unpacked it and now I don't know how to install it so that it is recognized by NetBeans.

    Could you help me on this?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    Sorry, I don't know Linux or NetBeans
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to create a.odt document formatted in Java

    Ok, thank you anyway for your availability.

    Can no one else give me any indication?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    Here is another forum that may have some one that knows more: http://www.coderanch.com/forums
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Oct 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Talking Re: How to create a.odt document formatted in Java

    Thank you. I just posted a question there too.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to create a.odt document formatted in Java

    Posted here: https://www.coderanch.com/t/786775/j...-document-Java
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java org.w3c.dom.Document.normalize() isn't working as expected
    By artaxerxe in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 10th, 2020, 01:21 AM
  2. Set Footer and Header in Document using Java
    By raghu3.ankam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2014, 07:01 AM
  3. Replies: 1
    Last Post: January 27th, 2014, 12:46 AM
  4. Fetching the document mode of IE in java
    By soumyam1990 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 18th, 2013, 04:43 AM
  5. Making the pieces fit together in Java (Document vs File)
    By TenLeftFingers in forum Object Oriented Programming
    Replies: 6
    Last Post: November 19th, 2012, 12:06 PM