我有 .ods 文件,我想通过 java 程序读取和显示它我使用了这个程序:
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:\\TestData\\test.ods");
ODSReader objODSReader = new ODSReader();
objODSReader.readODS(file);
}
}
输出如下所示:
> Date
Volume
Open
Low
High
Close
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at org.jopendocument.dom.spreadsheet.Row.getCellAt(Unknown Source)
at org.jopendocument.dom.spreadsheet.Row.getValidCellAt(UnknownSource)
at org.jopendocument.dom.spreadsheet.Row.getMutableCellAt(Unknown
Source)
at org.jopendocument.dom.spreadsheet.Table.getCellAt(Unknown Source)
at com.spreadSheets.java.ODSReader.readODS(ODSReader.java:38)
at com.spreadSheets.java.Main.main(Main.java:20)
**问题是我如何使用这个 jopendocument 包显示字符、数字和符号并避免或解决这些异常??**