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.
Related
I would like to be able to open Excel file of arbotrary type. Is it possible to select between HSSFWorkbook and XSSFWorkbook automatically?
Currently I write
Workbook workbook = new HSSFWorkbook(excelFile);
Can I write universal?
Yes! All you need to do is use WorkbookFactory
As per this part of the docs, it's better to use a File than an InputStream. So, just do something like:
File file = new File("input.xls");
Workbook wb = WorkbookFactory.create(file);
That will create whichever of HSSFWorkbook or XSSFWorkbook your file needs
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.
I'm reading a excel file by Poi(3.7). I'm learning somethins about poi at this link Poi Quick Guide. Now my concerns is about this:
When opening a workbook, either a .xls HSSFWorkbook, or a .xlsx XSSFWorkbook, the Workbook can be loaded from either a File or an InputStream. Using a File object allows for lower memory consumption, while an InputStream requires more memory as it has to buffer the whole file
In the 3.7 version to Poi the WorkbookFactory doesn't have following method
WorkbookFactory.create(new File("MyExcel.xls"))
and i try to load my file in these ways:
First way
InputStream is = (InputStream) getClass().getResourceAsStream("/MyExcel.xlsx");
Workbook wb = WorkbookFactory.create(is);
Second way
String path = getClass().getResource("/MyExcel.xlsx").getPath();
FileInputStream fis = new FileInputStream(new File(path));
Workbook wb = WorkbookFactory.create(fis);
Now i want to ask you, what is background difference of these three possibilities to load an excel file? Which of these do you suggest?
First way is good if your file is located in file system. Second way is actually wrong. If your file is a part of your application, i.e. avaliable from the classpath use the following code:
Workbook wb = WorkbookFactory.create(getClass().getResourceAsStream("/MyExcel.xlsx").getPath());
What's wrong with your code? Actually it may work only if your classes are located directly in file system. If however they are packed into jar the line new FileInputStream(new File(path)) will throw FileNotFoundException because file indeed does not exist in file system but packed into jar.
Using an InputStream has a higher memory footprint than using a File
I want to convert this code in java
fopen_s(&stream, "path", "w+");
w+ opens empty file with both reading and writing. If the given file exists, it's contents are destroyed.
any suggestions?
It seems 1.7 java is required for the nio, so my take is
RandomAccessFile f = new RandomAccessFile(name, "rw");
f.setLength(0);
I am not a Java programmer, but I had a short hunt around the web and it seems Java has a RandomAccessFile and you open it with the mode "rw".
The true equivalent is to use Files.newByteChannel.
final SeekableByteChannel channel = Files.newByteChannel(Paths.get("path"),
StandardOpenOptions.READ, StandardOpenOptions.WRITE,
StandardOpenOptions.TRUNCATE_EXISTING);
The READ and WRITE options determine if the file should be opened for reading and/or writing.
...
TRUNCATE_EXISTING - If this option is present then the existing file is truncated to a size of 0 bytes. This option is ignored when the file is opened only for reading.
Looks like you want either FileOutputStream or FileWriter, depending on what kind of data you want to write. Either of them can be instantiated with a filename.
FileOutputStream fis = new FileOutputStream("/path/to/file");
FileWriter fw = new FileWriter("/path/to/file2");
And both will clobber the file if it already exists. (Though constructors exists for appending instead of over-writing)
Quick way to achieve what you want:
import java.io.*;
// Create a new file output connected to "myfile.txt"
out = new FileOutputStream("myfile.txt");
// Create a new file input connected to "myfile.txt"
in = new FileInputStream("myfile.txt");
You might want to take a look at the java.io package at the official docs, especially the RandomAccessFile Class and also this quick guide.
Is there a way to Display a HSSFWorkbook Object in a JSP page without having an output file in the server side?
In the code below I am providing the template file and the beans required for net.sf.jxls.transformer.XLSTransformer.transformXLS(InputStream is, Map beanParams) to return me a HSSFWorkbook object. I now need a way to use this object in a JSP without having to store the output file in the server side using OutputStream.
InputStream is = new BufferedInputStream(new FileInputStream(templateFileName));
HSSFWorkbook hm = transformer.transformXLS(is, beans);
req.getSession().setAttribute("excelWorkBook",hm);
Looks simple, write the HSSFWorkbook using the write method on your instance;
HSSFWorkbook#write(OutputStream)
where the output stream is the;
response.getOutputStream()
Youll probably want to do things like set the ContentType of the reponse as well as maybe some content dispostion attributes.