我有.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软件包并避免或解决这些异常? **
最佳答案
我无法使用您的代码重现您的错误。我认为您应该更新到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);
}
}