Friday, July 27, 2018
Read OpenOffice SpreadSheet ods
Read OpenOffice SpreadSheet ods
Moving Ahead |
We are going to learn about an open source API called jOpenDocument. In this post, we will write a simple java class to read an openOffice Spreadsheet(ods) file.
A Brief Idea |
jOpenDocument is a free java library for reading and manipulating the openOffice documents. You can use jOpenDocument to generate dynamic documents from Java, XML, or databases.You can display and print files with built-in viewers. jOpenDocument API can be used for splitting , concatenating, and manipulating the pages and to automate filling out of templates.
A Snippet |
This is the sample class for reading the .ods file data.
package com.sarf.read;
import java.io.File;
import java.io.IOException;
import org.jopendocument.dom.spreadsheet.MutableCell;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;
public class ODSReader {
public void readODS(File file)
{
Sheet sheet;
try {
//Getting the 0th sheet for manipulation| pass sheet name as string
sheet = SpreadSheet.createFromFile(file).getSheet(0);
//Get row count and column count
int nColCount = sheet.getColumnCount();
int nRowCount = sheet.getRowCount();
System.out.println("Rows :"+nRowCount);
System.out.println("Cols :"+nColCount);
//Iterating through each row of the selected sheet
MutableCell cell = null;
for(int nRowIndex = 0; nRowIndex < nRowCount; nRowIndex++)
{
//Iterating through each column
int nColIndex = 0;
for( ;nColIndex < nColCount; nColIndex++)
{
cell = sheet.getCellAt(nColIndex, nRowIndex);
System.out.print(cell.getValue()+ " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//Creating File object of .ods file
File file = new File("D:TestDatatest.ods");
ODSReader objODSReader = new ODSReader();
objODSReader.readODS(file);
}
}
Download API : Click here
In our next post, we will learn about template and how to use it.