Interface Workbook、Sheet 和 Row 与类 XSSFWorkbook、XSSFSheet 之间的混淆。

huangapple go评论85阅读模式
英文:

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(&quot;C:\\Eclipse\\WorkSpace\\YPractice\\TestCase.xlsx&quot;);
	
	Workbook guru99Workbook = null;		
	
	guru99Workbook = new XSSFWorkbook(inputStream);									
	Sheet sheet = guru99Workbook.getSheet(&quot;KeywordFramework&quot;);
	
	int RowNum = sheet.getLastRowNum() - sheet.getFirstRowNum();
	

	for (int i = 0; i&lt;=RowNum; i++)
	{

		Row row = sheet.getRow(i);
	
		
		for(int j= 0; j&lt;row.getLastCellNum(); j++)
		{
			System.out.println(&quot;Data = &quot; +i + &quot; &quot; +row.getCell(j));
			
		}
	}

答案1

得分: 1

你在使用Workbook时正在使用XSSFWorkbook的实现,如下所示:

guru99Workbook = new XSSFWorkbook(inputStream);

之后,你在guru99Workbook上使用了getSheet()。鉴于guru99Workbook是一个XSSFWorkbook,你将得到一个XSSFSheet作为返回值。

这类似于:

List<String> list = new ArrayList<>();

至于回答你的第二个问题,这些方法列在以下位置:

  1. XSSFWorkbook

  2. XSSFSheet

  3. 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&lt;String&gt; list = new ArrayList&lt;&gt;();

To answer your second question, the methods are listed in:

  1. XSSFWorkbook:
    getSheet(String name)
  2. XSSFSheet:
    getRow(int rownum),
    getFirstRowNum(),
    getLastRowNum()
  3. XSSFRow:
    getLastCellNum()
    getCell(int cellnum)

答案2

得分: -1

getRow(int) 方法定义在 Sheet 接口 中,因此您可以从您的 Sheet sheet 变量中调用它。同样,getCell(int) 方法定义在 Row 接口 中,因此可以通过您的 Row 类型的 row 变量访问它。

关于 Java 中接口的工作原理,您的直觉是完全正确的。您可能误读了 SheetRow 接口的文档。

英文:

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 Rowtyped row variable.

Your intuition about how interfaces work in Java is perfectly correct. You may have misread the Sheet and Row interfaces' documentation.

huangapple
  • 本文由 发表于 2020年8月11日 02:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63346151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定