英文:
Confusion on Interface Workbook, Sheet and Row vs Classess XSSFWorkbook, XSSFSheet
问题
以下是一个简单的代码,用于访问Excel表的所有单元格。我没有使用XSSFWorkbook、XSSFSheet等类。我困惑的是,如果接口只包含方法的签名,那么我怎么能够使用像getRow、getCell这样的方法?
(这些方法在我键入sheet然后键入句点时是如何定义的)
提前致谢。
FileInputStream inputStream = new FileInputStream("C:\\Eclipse\\WorkSpace\\YPractice\\TestCase.xlsx");
Workbook guru99Workbook = null;
guru99Workbook = new XSSFWorkbook(inputStream);
Sheet sheet = guru99Workbook.getSheet("KeywordFramework");
int RowNum = sheet.getLastRowNum() - sheet.getFirstRowNum();
for (int i = 0; i <= RowNum; i++)
{
Row row = sheet.getRow(i);
for(int j= 0; j<row.getLastCellNum(); j++)
{
System.out.println("Data = " +i + " " +row.getCell(j));
}
}
英文:
Below is a simple code to access all the cells of an excel sheet. I didn't use the classes
XSSFWorkbook, XSSFSheet...etc
My confusion is if an interface only holds signature of the methods then how come I'm able to
use methods like getRow, getCell using interfaces?
(where are those methods defined that are listed when I type sheet and then type period)
Thanks in advance.
FileInputStream inputStream = new FileInputStream("C:\\Eclipse\\WorkSpace\\YPractice\\TestCase.xlsx");
Workbook guru99Workbook = null;
guru99Workbook = new XSSFWorkbook(inputStream);
Sheet sheet = guru99Workbook.getSheet("KeywordFramework");
int RowNum = sheet.getLastRowNum() - sheet.getFirstRowNum();
for (int i = 0; i<=RowNum; i++)
{
Row row = sheet.getRow(i);
for(int j= 0; j<row.getLastCellNum(); j++)
{
System.out.println("Data = " +i + " " +row.getCell(j));
}
}
答案1
得分: 1
你在使用Workbook
时正在使用XSSFWorkbook
的实现,如下所示:
guru99Workbook = new XSSFWorkbook(inputStream);
之后,你在guru99Workbook
上使用了getSheet()
。鉴于guru99Workbook
是一个XSSFWorkbook
,你将得到一个XSSFSheet
作为返回值。
这类似于:
List<String> list = new ArrayList<>();
至于回答你的第二个问题,这些方法列在以下位置:
-
XSSFWorkbook
: -
XSSFSheet
: -
XSSFRow
:
英文:
You are using the XSSFWorkbook
implementation of Workbook
when you do:
guru99Workbook = new XSSFWorkbook(inputStream);
Later you use getSheet()
on guru99Workbook
. Given that guru99Workbook
is an XSSFWorkbook
, you'll get in return a XSSFSheet
.
This is similar to:
List<String> list = new ArrayList<>();
To answer your second question, the methods are listed in:
XSSFWorkbook
:
getSheet(String name)
XSSFSheet
:
getRow(int rownum)
,
getFirstRowNum()
,
getLastRowNum()
XSSFRow
:
getLastCellNum()
getCell(int cellnum)
答案2
得分: -1
getRow(int)
方法定义在 Sheet
接口 中,因此您可以从您的 Sheet
sheet
变量中调用它。同样,getCell(int)
方法定义在 Row
接口 中,因此可以通过您的 Row
类型的 row
变量访问它。
关于 Java 中接口的工作原理,您的直觉是完全正确的。您可能误读了 Sheet
和 Row
接口的文档。
英文:
The getRow(int)
method is defined in the Sheet
interface, and that's why you can call it from your Sheet
sheet
variable. Likewise, getCell(int)
is defined in the Row
interface, and thus, can be accessed through your Row
typed row
variable.
Your intuition about how interfaces work in Java is perfectly correct. You may have misread the Sheet
and Row
interfaces' documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论