如何使用 apache poi 读取 xlsx 类型的 excel 文件的单元格内容? [英] How do I read the cell contents using apache poi for xlsx type of excel file?

查看:39
本文介绍了如何使用 apache poi 读取 xlsx 类型的 excel 文件的单元格内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我有点不高兴,我阅读了有关使用 hssf 读取 xls 文件的各种主题,但我似乎找不到好的 xssf 教程,这真的很难,因为它们有不同的陈述.我的代码应该读取第 2 行第 2 列,但我收到getContents() 未定义 XSSFComment 类型"错误

我的代码类似于:

import java.io.FileOutputStream;导入 java.io.IOException;导入 java.io.InputStream;导入 org.apache.poi.xssf.usermodel.XSSFCell;导入 org.apache.poi.xssf.usermodel.XSSFRow;导入 org.apache.poi.xssf.usermodel.XSSFSheet;导入 org.apache.poi.xssf.usermodel.XSSFWorkbook;公共类解析{public static void main(String[] args) 抛出 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());}}

错误:

线程main"中的异常java.lang.Error:未解决的编译问题:对于 XSSFComment 类型,方法 getContents() 未定义在 parsing.main(parsing.java:18)

解决方案

根据最新的 Apache POI 文档,XSSFComment 类没有 getContents() 方法完全没有.

改为尝试使用 getString() 方法来获取 Comment 内容.查看 Apache POI XSSFComment 的文档>

另请查看 Apache PoI 快速指南,了解提供的 Cell Comment 示例.

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)

解决方案

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.

这篇关于如何使用 apache poi 读取 xlsx 类型的 excel 文件的单元格内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆