Mnementh
Mnementh

Reputation: 51331

How can I access spreadsheets in the open document format (.ods) with Java?

I want to read, write and create Spreadsheets in the Open Document Format with Java. And I want the resulting Java-program running on a computer without OpenOffice.org or other ODS-capable programs installed. Exists a library to access this format?

Upvotes: 11

Views: 13156

Answers (3)

Adrian Jimenez
Adrian Jimenez

Reputation: 1167

First import library

  <dependency>
        <groupId>com.github.miachm.sods</groupId>
        <artifactId>SODS</artifactId>
        <version>1.6.2</version>
    </dependency>

then this is an example on how to read the first column of the first sheet

public static List<String> readColumnA(String filePath) {
    List<String> movieList = new ArrayList<>();
    try {
        SpreadSheet spread = new SpreadSheet(new File(filePath));
        //     System.out.println("Number of sheets: " + spread.getNumSheets());
        Sheet sheet = spread.getSheets().get(0);
        //     System.out.println("In sheet " + sheet.getName());
        Range movies = sheet.getDataRange();
        Object[][] movieColumn = movies.getValues();
        for (int i = 0; i < sheet.getMaxRows(); i++) {
            movieList.add(movieColumn[i][0].toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return movieList;
}

Upvotes: 0

user270491
user270491

Reputation: 19

jOpenDocument is all you need.

Upvotes: 2

xyz
xyz

Reputation: 1192

Take a look at jOpenDocument: http://www.jopendocument.org/documentation.html

Especially: http://www.jopendocument.org/start_spreadsheet_3.html

Upvotes: 14

Related Questions