我有.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包并避免或解决这些异常? **
答案 0 :(得分:0)
sheet.getRowCount()
这样可以在工作表中为您提供最大行数,例如19534667,因此您不应该将其用于此。在我的项目中,我手动添加行数和列数。
答案 1 :(得分:0)
我无法使用您的代码重现您的错误。我认为您应该更新到jOpenDocument的1.3版。我只制作了5行的电子表格来测试您的代码。尽管如此,它还是很棒的。
不过,在代码中只有一件事,您不需要将nColIndex带到“ for”循环声明之外。
您的代码非常适合仅包含1张纸的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);
}
}