首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用java和jopendocument包读取.ods文档

如何用java和jopendocument包读取.ods文档
EN

Stack Overflow用户
提问于 2017-09-28 19:42:33
回答 2查看 7.9K关注 0票数 1

我有.ods文件,我想通过java程序读取并显示它,我使用了这个程序:

代码语言:javascript
复制
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文件是这样的:

输出如下所示:

代码语言:javascript
复制
> 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包显示字符、数字和符号,并避免或解决这些异常?**

EN

回答 2

Stack Overflow用户

发布于 2018-12-08 16:18:12

我不能用你的代码重现你的错误。我认为你应该更新到jOpenDocument的1.3版本。我只做了一个5行的电子表格来测试你的代码。尽管如此,它还是工作得很好。

不过,在您的代码中只有一件事,您不需要将nColIndex带到"for“循环声明之外。

你的代码对于只有一个工作表的ods文件来说是很棒的,但是如果你有多个工作表,你可能会遇到问题。我只是稍微修改了一下你的代码,让你将来可以很容易地编辑这个版本,使程序能够处理具有多个设计相似的工作表的电子表格。

代码语言:javascript
复制
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);
  }
}
票数 1
EN

Stack Overflow用户

发布于 2018-03-30 16:08:26

代码语言:javascript
复制
sheet.getRowCount()

这给了你一个工作表中的最大行数,大约是19534667,所以你不应该使用它来this.In我的项目,我手动添加行和列数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46468272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档