Java Interface Implement - java

I am studying Java, Spring and POI. I see https://poi.apache.org/spreadsheet/quick-guide.html and follow it.
At "New WorkBook", next code exists.
Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
Running above code, it works well.
I found out that "Workbook" is NOT class BUT interface. I learned that interface HAVE TO be implemented. But I CANNOT found the implementation. Where is it implemented? What must I study to comprehend it?

Oficial Oracle tutorial about interfaces:
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

That is interface instance. Details: Using an Interface as a Type
In short, you can create instance of interface.
Case 1: for anonymous class.
Workbook wb = new Workbook() {
// Some implementation
}
Case 2: Use it as type. (Yes! your case!)
Workbook wb = new XSSFWorkbook();
see also: Strategy Design Pattern in Java. It is very useful idea.

Related

Check if Excel file supports Apache POI HSSF or XSSF

I am using Apache POI HSSF/XSSF library to support Excel file reading functionality in my java code.
Now we are struggling with a thing that, we are not sure which version of Excel we will receive. It can be very old one or new one with macros and so on.
Workbook worbook = null;
if(FileUtils.getFileExt(file).equalsIgnoreCase("xls")) {
workbook = new HSSFWorkbook(new FileInputStream(file));
} else if (FileUtils.getFileExt(file).equalsIgnoreCase("xlsx"))
workbook = new XSSFWorkbook(new FileInputStream(file));
}
According to the Excel doc there is more formats like xlsm. xlt which we can receive.
Is there any other option to recognize which implementation should I use (HSSF vs XSSF)? Is there any possibility that xls will be not supported by HSSF?

Error: Type mismatch: cannot convert from HSSFWorkbook to Workbook

I use poi-3.2-FINAL-20081019.jar . Error:
Type mismatch: cannot convert from HSSFWorkbook to Workbook
try {
if (strType.equals("xls")) {
wb = new HSSFWorkbook(inputStream);
} else {
wb = new XSSFWorkbook(inputStream);
}
Sheet sheet = wb.getSheetAt(0);
How to fix it?
As the date in your jar shows - poi-3.2-FINAL-20081019.jar - you're usigng a jar that's almost 10 years old! You need to upgrade to something more modern, and at the very least something from this decade...
Right now (November 2017), the latest version is Apache POI 3.17. You can find the latest version on the Apache POI homepage, and see all the fixes in the Changelog
In addition, you should swap to using WorkbookFactory rather than looking at file extensions to work out what class to use. That hides all the detection complexity for you, works around mis-named files etc
Your code can then become the very simple
Workbook wb = WorkbookFactory.create(new File("input.xlsx"));
Sheet s = wb.getSheetAt(0);
(Use a File if you can, rather than InputStream, for lower memory)
Use newer version:
https://mvnrepository.com/artifact/org.apache.poi/poi/3.5-FINAL
Read more at: http://poi.apache.org/spreadsheet/converting.html

Meaning of XSSFWorkbook(java.io.InputStream is)?

What exactly it means when I call the constructor XSSFWorkbook(java.io.InputStream is). I am new to java, and I am looking for an answer in layman terms.
I am assuming that you are talking about the Apache POI API XSSFWorkbook. In this case, when you are calling the constructor, you are specifying the inputstream of an excel file.
for example
InputStream input = new FileInputStream("C:\\Users\\...\\MyExcelFile.xlsx");
That would be initializing a new InputStream, where you could pass that into the XSSFWorkbook
XSSFWorkbook myWorkbook = new XSSFWorkbook(input);
Then you could make changes/get information by using the various methods of the XSSFWorkbook class.

Data driven test in selenium/eclipse. Workbook error?

I am trying to extract input from a external xls file into a selenium test in eclipse. I'm following a tutorial but for some reason I'm getting an error at the line:
Workbook w = Workbook.getWorkbook(fi);
I'm not sure why it's not working. Not that I'm sure why it should be working either though...
Please look at the screenshot for more info!
If you are using jxl your code is fine some thing like :
File f = new File("inputSheets\\DataDrivenJXL.xls");
Workbook w = Workbook.getWorkbook(f);
but if you are using Apache POI you will have to do something like
if(fileExtensionName.equals(".xlsx")){
Workbook = new XSSFWorkbook(inputStream);
}
else if(fileExtensionName.equals(".xls")){
Workbook = new HSSFWorkbook(inputStream);
}

How do I resolve code relating to "instantiate"?

So, I am writing a code in which I imported a class with class name "Workbook" and function "createWorkBook". I asked the same question earlier but I wanted to add changes so I removed that before anyone could reply.
Anyways,
I am new to Java , interfaces and importing class.
I imported a package named "jxl" and I am using it. Here is my FULL code so far.
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.write.*;
import jxl.write.Number;
public class WriteExcel {
public static void main (String args[]) throws IOException , WriteException
{
try{
Workbook wb = new Workbook();
}
catch(WriteException e)
{
System.out.println("Sorry, failed! Keep on trying harder! :)");
}
}
}
ALL I am trying for past half an hour is trying to make an object "wb" in class "workbook" .
I followed bit of instructions from https://www.youtube.com/watch?v=A9866lBdmKo (importing of class).
I am getting an error for the link Workbook wb= new Workbook();
Cannot instantiate the Workbook type. I did some research turns out it is relating to some "interface". But the video didn't even talk about interface. I am new and would like some guidance. I just want to create one object.
So I got the file, thanks for ANY sort of input!
Workbook is an Anonymous Inner Type java class, whenever you want to instantiate that class, we'll need to override several methods. You will have to implement those methods. You can find more about Anonymous Inner Type java class here
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
Having a look at the javadoc, it seems that Workbook is an abstract class, so you can't instanciate it, but it seems that it provides some staticmethods called createWorkbook() that you can use, like that :
Workbook wb = Workbook.createWorkbook(new File("/path/to/the/workbook/file"));
As workbook is an abstract class. The only way to create a workbook object is
String fileName = "file.xls";
WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
Creates a writable workbook with the given file name.
Also please refer to the below link that represent the workbook class and its methods
http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html

Categories