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));
}
}
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)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.