Reputation: 35
Sorry for the dumb question. Following is the code that I have written to read from a spreadsheet using Apache POI. The line that I am confused is
XSSFWorkbook wb = new XSSFWorkbook(fs);
XSSFSheet sheet = wb.getSheetAt(0);
I have created an XSSFSheet object, and it takes wb.getSheetAt(0), my query is why should I pass the wb.getSheetAt(0) to the sheet object. when I try to create an object as below
XSSFSheet sheet = new XSSFSheet(wb);
I get an error stating
XSSFSheet(org.apache.poi.openxml4j.opc.PackagePart) has protected access in 'org.apache.poi.xssf.usermodel.XSSFSheet'
I looked at the XSSFSheet source code and its constructor has been declared as protected. I am under the impression that an object can be created using the new keyword. What am I missing?
Source code for reference
package ReadAndWrite;
import ReadExcelLibrary.ReadExcelSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* Created by Shravya on 11/5/17.
*/
public class ReadExcel {
public static void main(String[] args) throws Exception {
File file = new File("//Development//Selenium//ApachePOI//Apache_wb.xlsx");
FileInputStream fs = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fs);
//XSSFSheet sheet = new XSSFSheet(wb);
XSSFSheet sheet = wb.getSheetAt(0);
String value = sheet.getRow(0).getCell(0).getStringCellValue();
System.out.println("The value of cell is " + value);
ReadExcelSheet excelSheet = new ReadExcelSheet("//Development//Selenium//ApachePOI//Apache_wb.xlsx");
System.out.println(excelSheet.getData(0,1,1));
}
}
Upvotes: 0
Views: 317
Reputation: 692181
XSSFSheet sheet = new XSSFSheet(wb);
Let's pretend you can do that. It would create a new XSSFSheet object, that doesn't exist yet.
XSSFSheet sheet = wb.getSheetAt(0);
That doesn't do the same thing at all. That allows getting the already existing sheet that is at index 0 in the workbook. So those are two completely different things. The same difference there is, basically, between getting a car from a garage, and building a brand new car.
So when you say
XSSFSheet sheet = wb.getSheetAt(0);
I have created an XSSFSheet object, and it takes wb.getSheetAt(0)
that's incorrect. You haven't created anything. You have obtained a reference to an existing XSSFSheet object, that the workbook had already created for you.
I am under the impression that an object can be created using the new keyword.
Yes, that's what a constructor allows doing. But if a constructor is protected, it means that only subclasses or classes in the same package are allowed to call the constructor.
To create a new sheet in a workbook, you would use this method, to ask the workbook to create the sheet for you, and give you a reference to the created sheet. Just like, to obtain a new car, you typically don't create one by yourself, but instead ask a car manufacturer to build one for you.
Upvotes: 2