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
Related
I have this following portion of code that i don't get why it doesn't work.
I've checked the location of the file and it's fine, though, just to be sure i've changed it to a new location but the problem continues.
//resources is the paste where the jar file will get Inputs
File temp = new File("resources\\OEE_SETOR_LECTRAS.xlsx");
try {
FileInputStream fis = new FileInputStream(temp);
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(6);
.
.
.
-->then when i call the function it throws this:
java.io.FileNotFoundException: *******.XLSX
at java.io.FileInputStream.open(Native Method)
thanks in advance.
As #ankush has already pointed out, it's not good way to load file from resources.
Try using
System.out.println(new File("resources\\OEE_SETOR_LECTRAS.xlsx").getAbsolutePath());
to check full path from where your program is trying to load the file.
If the defined xlsx file is in your your class path, try loading it via classloader.
Test.class.getClassLoader().getResourceAsStream("resources\\OEE_SETOR_LECTRAS.xlsx");
I have very large xls file , which contains two sheets. I want to combine these two sheets into one and copy to new workbook . But I get out of memory exception when I try to access this large xls as below :
FileInputStream fis = new FileInputStream(new File("input.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(fis);
I tried using event api for xls : http://poi.apache.org/spreadsheet/how-to.html#event_api
But using that we can only read the cell values . But here I need to copy to new excel sheet.
Apache POI provides a low-memory footprint SXSSF API to write data to xlsx. It does not load everything in memory at once so it's the solution while working with very large excel files. You may want to consider this.
I have a program that does various tasks and then logs the results to an excel file. I'm using Apache POI.
The program is working perfectly and is logging everything as I want it. However, it can take hours to complete, and I noticed that if I open the excel file while it's still running, it fails and throws a java.io.FileNotFoundException exception with this message (The process cannot access the file because it is being used by another process) when it tries to save to the file.
My guess is that by opening the excel file, the program is getting locked out of writing to it. How can I get around this? Is there a way to lock users out of opening the file while the program runs (Forcing them to access it as read-only)?
This is the code that gets used to initially create the file:
HSSFWorkbook wb = new HSSFWorkbook();
//.. bunch of POI stuff here
FileOutputStream fileOut = new FileOutputStream("C:\\ file path here");
wb.write(fileOut);
wb.close();
fileOut.close();
And then in a separate logResults() method, the file is accessed and saved:
FileInputStream file = new FileInputStream(new File("C:\\ file path here"));
HSSFWorkbook wb = new HSSFWorkbook(file);
//.. bunch more POI stuff here
FileOutputStream fileOut = new FileOutputStream("C:\\ file path here");
wb.write(fileOut);
wb.close();
fileOut.close();
I don't think you can do anything in Java side.
You may need do something in Excel file as follows
Private Sub Workbook_Open()
If ThisWorkbook.ReadOnly
Then
MsgBox "File already in use"
ThisWorkbook.Close
savechanges:=False
End If
End Sub
'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code
I found a work around. I'm not sure if it's the most elegant solution, but it seems to work well enough.
When the file is created, I set it to read only with. Then every time I will log a result, I switch it back to writeable, and then back to read only after it's saved.
Basically something like this:
File file = new File(filePath);
//..
file.setWritable(true);
FileOutputStream fileOut = new FileOutputStream(filePath);
file.setReadOnly();
My problem is a bit different from the ones that are there in SO...I will try to explain..
My code pick up an .xls file from a location (c:\codereview\) and inserts data into that file. I was using full file path for this but realized my program is not portable.
So I copied my xls from c:/ drive into the src/ folder and I now I keep getting a FileNotFoundException.
FileInputStream file = new FileInputStream(new File("/src/New_Record.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
when i try to use:
FileInputStream file = (FileInputStream) Util.class.getResourceAsStream("New_Record.xls");
i get below exception:
java.lang.NullPointerException
at org.apache.poi.poifs.filesystem.POIFSFileSystem.closeInputStream(POIFSFileSystem.java:183)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:145)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:322)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:303)
Basically I want to use a combination of FileInputStream and InputStream so that i can use that with HSSFWorkbook. InputStream is not compatible directly with it and FileInputStream is not compatible with this.getClass.getResourceStream.
I created an .xlsx file using selenium through File "fil.createNewFile()", the file got created in the location but when i try to open the file i am getting a message "Excel cannot open the file 'Example.xlsx' because the file format or file extension is not valid....". Why is this happening. Please guide me to over come this problem.
Thanks in advance...
What eltabo mentioned is correct. Below is a code sample from Apache POI which will create a blank excel file.
HSSFWorkbook workbook = new HSSFWorkbook();
FileOutputStream out = new FileOutputStream(new File("C:\\new.xls"));
workbook.write(out);
out.close();
I guess you are trying something like:
File fil = new File("Example.xlsx");
fil.createNewFile();
Well, it this is your case you're creating a file with xlsx extension, but it's not truly a Excel file (it's empty). If you try to create a new file using Excel or File Explorer, you can see that a new fresh xlsx file weight about 10kb. If you need to create a Excel file you need to use Apache POI, or another library.
You need to add a sheet at least:
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
FileOutputStream fileOut = new FileOutputStream("Example.xlsx");
wb.write(fileOut);
fileOut.close();
PS: I use poi-ooxml, since you use a xlsx file.
Hope it helps.