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?
How can I access spreadsheets in the open document format (.ods) with Java?
Asked Answered
Take a look at jOpenDocument: http://www.jopendocument.org/documentation.html
Especially: http://www.jopendocument.org/start_spreadsheet_3.html
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;
}
© 2022 - 2024 — McMap. All rights reserved.