Linux 使用 Apache POI 的 getRichStringCellValue() 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:15:25  来源:igfitidea点击:

Error in using getRichStringCellValue() of Apache POI

javaexcelapache-poi

提问by LGAP

The following is my java code for reading a excel sheet content.

以下是我读取excel表格内容的java代码。

String urlcnt="";
for (Row row : sheet) {
 {
Cell firstCell = row.getCell(0);
urlcnt=firstCell.getRichStringCellValue();}

While compiling the above code am getting the following error.

编译上述代码时出现以下错误。

ReadExcel.java:18: incompatible types
found   : org.apache.poi.ss.usermodel.RichTextString required: java.lang.String
                    urlcnt=firstCell.getRichStringCellValue();//This line is the cause for the error

Instead of storing the getRichStringCellValue() in a string, if I just print the value of the cell, it works fine. But I go ahead and store it in a string for further processing the problem occurs.

如果我只是打印单元格的值,而不是将 getRichStringCellValue() 存储在字符串中,它可以正常工作。但是我继续将它存储在一个字符串中,以便进一步处理发生的问题。

Kindly let me know what has to be done to proceeed.

请让我知道必须做什么才能继续。

采纳答案by JoseK

The error is because getRichStringCellValue()returns a HSSFRichTextStringor XSSFRichTextString(depending on whether your cell is HSSFCell or XSSFCell) and you are assigning it to a String

错误是因为getRichStringCellValue()返回HSSFRichTextStringXSSFRichTextString(取决于您的单元格是 HSSFCell 还是 XSSFCell)并且您将其分配给String

Depending on your further processing -

根据您的进一步处理 -

Do you want to call applyFontor clearFormattingon the HSSFRichTextString ? then store it in a HSSFRichTextString/XSSFRichTextString.

你想调用applyFont还是clearFormatting在 HSSFRichTextString 上?然后将其存储在 HSSFRichTextString/XSSFRichTextString 中。

If you actually want only the String text, use the getString()method from the POI API

如果您实际上只想要字符串文本,请使用POI API 中的getString()方法

UPDATE as per your comments

根据您的评论更新

Use it as

将其用作

urlcnt=firstCell.getRichStringCellValue().getString();