How do I read the cell contents using apache poi for xlsx type of excel file?

我怕爱的太早我们不能终老 提交于 2019-12-12 01:29:08

问题


It seems I'm in a bit of a pickle, I read various topics about read xls files with hssf but I can't seem to find good xssf tutorials and it's really hard since they have different statements. My code is supposed to read the row 2, column 2 but I get a "getContents() is undefined for the type XSSFComment" error

My code goes something like:

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class parsing{ 

public static void main(String[] args) throws IOException { 
InputStream ExcelFileToRead = new FileInputStream("C:/test.xlsx"); 
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead); 
XSSFSheet sh = wb.getSheetAt(0); 
System.out.println(sh.getCellComment(1,1).getContents()); 
} 
}

Error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method getContents() is undefined for the type XSSFComment
    at parsing.main(parsing.java:18)

回答1:


As per the documentation of latest Apache POI the class XSSFComment doesn't have getContents() method at all.

Instead try to use getString() method to get the Comment content. Check the documentation of Apache POI XSSFComment

Also check Apache PoI Quick Guide for the Cell Comment example provided.




回答2:


Instead of using getContents() method, use getString()



来源:https://stackoverflow.com/questions/27467555/how-do-i-read-the-cell-contents-using-apache-poi-for-xlsx-type-of-excel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!