Error in using getRichStringCellValue() of Apache POI
The following is my java code for reading a excel sheet content.
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.
Kindly let me know what has to be done to proceeed.
The error is because getRichStringCellValue()
returns a HSSFRichTextString or XSSFRichTextString (depending on whether your cell is HSSFCell or XSSFCell) and you are assigning it to a String
Depending on your further processing -
Do you want to call applyFont
or clearFormatting
on the HSSFRichTextString ?
then store it in a HSSFRichTextString/XSSFRichTextString.
If you actually want only the String text, use the getString() method from the POI API
UPDATE as per your comments
Use it as
urlcnt=firstCell.getRichStringCellValue().getString();
精彩评论