Java+jopendocument: 使用getCellAt(0,0)时出现空指针异常。

huangapple go评论118阅读模式
英文:

Java+jopendocument: NullPointerException when using getCellAt(0,0)

问题

  1. LibreOffice电子表格的工作表中,我可以访问单元格,但无法访问其值。我找不到错误所在,欢迎任何反馈意见。以下是代码:
  2. ```java
  3. import java.io.File;
  4. import java.io.IOException;
  5. import org.jopendocument.dom.spreadsheet.MutableCell;
  6. import org.jopendocument.dom.spreadsheet.Sheet;
  7. import org.jopendocument.dom.spreadsheet.SpreadSheet;
  8. public class MyClass {
  9. protected Sheet dataSheet;
  10. protected File dataCalcFile;
  11. public static void main(String[] args) {
  12. MyClass myClassInstance = new MyClass();
  13. myClassInstance.loadData();
  14. }
  15. public void loadData() {
  16. int numRows=0, numColumnas=0;
  17. MutableCell cell=null;
  18. try {
  19. dataCalcFile = new File("C:\\temp\\Data.ods");
  20. dataSheet = SpreadSheet.createFromFile(dataCalcFile).getSheet(0);
  21. numRows = dataSheet.getRowCount();
  22. System.out.println("行数:" + numRows);
  23. System.out.println("位于 0,0 的单元格:" + dataSheet.getCellAt(0, 0));
  24. System.out.println("获取位于 0,0 处单元格值时出现空指针异常:" + dataSheet.getValueAt(0, 0)); // *** 臭名昭著的问题 ***
  25. } catch (Exception e) {
  26. System.out.println(e);
  27. }
  28. }
  29. }

以下是控制台输出:

  1. 行数:107
  2. 位于 0,0 处的单元格:<table:table-cell xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" table:style-name="ce1" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" office:value-type="string" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" calcext:value-type="string"><text:p xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0">Carpeta</text:p></table:table-cell>
  3. java.lang.NullPointerException
英文:

In a Libreoffice Spreadsheet's sheet, I can get access to a Cell, but not to its value. I don't find my error, any feedback is welcome. Here is the code:

  1. import java.io.File;
  2. import java.io.IOException;
  3. import org.jopendocument.dom.spreadsheet.MutableCell;
  4. import org.jopendocument.dom.spreadsheet.Sheet;
  5. import org.jopendocument.dom.spreadsheet.SpreadSheet;
  6. public class MyClass {
  7. protected Sheet dataSheet;
  8. protected File dataCalcFile;
  9. public static void main(String[] args) {
  10. MyClass myClassInstance = new MyClass();
  11. myClassInstance.loadData();
  12. }
  13. public void loadData() {
  14. int numRows=0, numColumnas=0;
  15. MutableCell cell=null;
  16. try {
  17. dataCalcFile = new File(&quot;&quot;C:\\temp\\Data.ods&quot;&quot;);
  18. dataSheet = SpreadSheet.createFromFile(dataCalcFile).getSheet(0);
  19. numRows = dataSheet.getRowCount();
  20. System.out.println(&quot;Number of rows: &quot; + numRows);
  21. System.out.println(&quot;Cell at 0,0: &quot; + dataSheet.getCellAt(0, 0));
  22. System.out.println(&quot;Nullpointer Exception when getting cell value at 0,0: &quot; + dataSheet.getValueAt(0, 0)); // *** THE INFAMOUS ONE ***
  23. } catch (Exception e) {
  24. System.out.println(e);
  25. }
  26. }
  27. }

And here is the output at console:

  1. Number of rows: 107
  2. Cell at 0,0: &lt;table:table-cell xmlns:table=&quot;urn:oasis:names:tc:opendocument:xmlns:table:1.0&quot; table:style-name=&quot;ce1&quot; xmlns:office=&quot;urn:oasis:names:tc:opendocument:xmlns:office:1.0&quot; office:value-type=&quot;string&quot; xmlns:calcext=&quot;urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0&quot; calcext:value-type=&quot;string&quot;&gt;&lt;text:p xmlns:text=&quot;urn:oasis:names:tc:opendocument:xmlns:text:1.0&quot;&gt;Carpeta&lt;/text:p&gt;&lt;/table:table-cell&gt;
  3. java.lang.NullPointerException

答案1

得分: 5

刚刚发现:
"是的,LibreOffice 7 切换到了 OpenDocument 1.3。我们正在努力支持它。与此同时,您可以将格式更改为“1.2 扩展”。进入选项,然后选择加载/保存,接着选择常规,然后选择 ODF 格式版本。"

对我来说有效,但我很期待听到我的客户意见...

英文:

Just found that:
"Yes, LO 7 switched to OpenDocument 1.3. We're working on supporting it. In the mean time, you can change the format to "1.2 extended". Go to Options, then Load/Save, then General, then ODF format version."

It works for me, but am excited to hear my customers opinion...

答案2

得分: 0

我发现** jopendocument库与较新版本的Libreoffice(7.x)之间必定存在某种不兼容性**,如上面的代码所示:

  • 如果使用Libreoffice 6.4.3.2编辑电子表格,则按预期工作。
  • 如果使用Libreoffice 7.0.1.2编辑电子表格,则会抛出空指针异常。

在某些版本中,Calc生成的XML文件无法被jopendocument解析。

英文:

I found that it must be some incompatibility between the library jopendocument and the newer versions of Libreoffice (7.x), as the code above:

  • Works as expected if the spreadsheet is edited with Libreoffice 6.4.3.2
  • Spits the null pointer exception out if the spreadsheet is edited with Libreoffice 7.0.1.2

In some version in between Calc produces an XML not understood by jopendocument.

答案3

得分: 0

代替使用

  1. getValueAt(0, 0),
  2. 你可以使用
  3. getCellAt(0, 0).getElement().getValue()

这对我有效。

英文:

Instead of using

  1. getValueAt(0, 0),
  2. you can use
  3. getCellAt(0, 0).getElement().getValue()

It works for me.

huangapple
  • 本文由 发表于 2020年10月19日 15:46:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64423111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定