我有.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);
}
}.ods文件是这样的:

输出如下所示:
> 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包显示字符、数字和符号,并避免或解决这些异常?**
发布于 2018-12-08 16:18:12
我不能用你的代码重现你的错误。我认为你应该更新到jOpenDocument的1.3版本。我只做了一个5行的电子表格来测试你的代码。尽管如此,它还是工作得很好。
不过,在您的代码中只有一件事,您不需要将nColIndex带到"for“循环声明之外。
你的代码对于只有一个工作表的ods文件来说是很棒的,但是如果你有多个工作表,你可能会遇到问题。我只是稍微修改了一下你的代码,让你将来可以很容易地编辑这个版本,使程序能够处理具有多个设计相似的工作表的电子表格。
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)
{
SpreadSheet spreadsheet;
try {
//Getting the 0th sheet for manipulation| pass sheet name as string
spreadsheet = SpreadSheet.createFromFile(file);
//Get row count and column count
int nColCount = spreadsheet.getSheet(0).getColumnCount();
int nRowCount = spreadsheet.getSheet(0).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
for(int nColIndex = 0; nColIndex < nColCount; nColIndex++)
{
cell = spreadsheet.getSheet(0).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("test.ods");
ODSReader objODSReader = new ODSReader();
objODSReader.readODS(file);
}
}发布于 2018-03-30 16:08:26
sheet.getRowCount()这给了你一个工作表中的最大行数,大约是19534667,所以你不应该使用它来this.In我的项目,我手动添加行和列数。
https://stackoverflow.com/questions/46468272
复制相似问题