JXL Won't Allow Excel 2007 Extensions - java

I am using JXL to write an Excel report. Everything works fine if I use the .XLS extension. However, when I use the .XLSM extension, the report fails to load when I try to open it. I get the message "Excel cannot open the file 'TestExcel.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file". Below is a simplified version of what I am doing. As mentioned, it works if you change the filePath to use .XLS instead.
String filePath = "C:\\Users\\Rachel\\Desktop\\TestExcel.xlsm";
File excelFile = new File(filePath);
WritableWorkbook book = Workbook.createWorkbook(excelFile);
WritableSheet sheet = book.createSheet("Test Sheet", 0);
sheet.addCell(new Label(0, 0, "Testing..."));
book.write();
book.close();
I am using Excel 2007 to open the file. If I create a new workbook in Excel, save it as xlsm, close it, and then open it, Excel opens it correctly. Does anyone know how to make the JXL file open correctly? I cannot switch to Apache POI or anything, I have to use JXL.

Related

I would like to create a copy of word or excel file using poi

I would like to create a copy of word or excel file using poi.
I know that poi is also used when reading a word or excel file. Reading means not only values but also attribute such as font size or table color and backgroud colors for each cells. Reading values and attribute of the xlsx or docx document, I want to make a copy of the word or Excel document as it is. Is it possible that the related source is open at open source on the any site?
read Apache POI or docx4j for dealing with docx documents
you can find the techniques related to adding text into document you can found out on https://www.slideshare.net/plutext/document-generation-2012osdcsydney
use POI's HWPF support. this is often enclosed in docx4j as a dependency. however its not an excellent approach, since it does not convert the doc to docx4j's internal representation:- you are kind of stuck in HWPF land
use JODConverter to convert the doc to a docx, and if necessary, back again. this is often the simplest .
To open an excel from one file, and save it to another file I use this code.
//open source excel
InputStream template = new FileInputStream("C:\\source excel path\\input.xlsx");
Workbook wb = WorkbookFactory.create(template);
//Saving excel to a different location or filename.
FileOutputStream out = new FileOutputStream("C:\\path to copy excel to\\output.xlsx");
wb.write(out);
wb.close();
out.close();
template.close();

How to Edit/Modify an existing Excel file in Java with Jexcel API

I want to edit an existing Excel file with Java, to add some more data to an existing template excel file. So i used Jexcel for this purpose.
As suggested everywhere, I tried the following,
Workbook existingWorkbook = Workbook.getWorkbook(new File("H://"+file_name));
WritableWorkbook copy = Workbook.createWorkbook(new File("H://"+file_name+"_temp1.xls"));
But it shows an exception in the second line.
jxl.common.AssertionFailed
at jxl.common.Assert.verify(Assert.java:37)
at jxl.read.biff.SheetReader.handleObjectRecord(SheetReader.java:1811)
at jxl.read.biff.SheetReader.read(SheetReader.java:1059)
at jxl.read.biff.SheetImpl.readSheet(SheetImpl.java:716)
at jxl.read.biff.WorkbookParser.getSheet(WorkbookParser.java:257)
at jxl.write.biff.WritableWorkbookImpl.copyWorkbook(WritableWorkbookImpl.java:969)
at jxl.write.biff.WritableWorkbookImpl.<init>(WritableWorkbookImpl.java:343)
at jxl.Workbook.createWorkbook(Workbook.java:339)
at jxl.Workbook.createWorkbook(Workbook.java:320)
at run_book.process_input.<init>(process_input.java:83) <--create workbook stt.
.........<stack trace goes on>
So how could one edit an already existing jexcel file.
I did get another warning
Warning: Text Object on sheet "sheet2" not supported - omitting
Thanks in advance :)
Figured out the problem.
We have to close the input file before writing back (editing) the same file.
so to edit an existing Excel file with Jexcel
File inp = new File("H://"+file_name);
File out = new File("H://"+file_name);
Workbook existingWorkbook = Workbook.getWorkbook(inp);// This opens up a read-only copy of the workbook
WritableWorkbook copy = Workbook.createWorkbook(out,existingWorkbook); // This opens up a writable workbook so that we can edit the copy
//..........Some writes to excel workbook...........
// Now before writing & closing the copy, first close the existing one
existingWorkbook.close(); // Important: Close it before writing the copy with copy.write();
inp.close();
copy.write();
copy.close();
I ran into this same issue and to solve the problem I updated to the latest version of jxl.jar via maven. After doing this there was a very long delay when it ran destinationWorkbook = Workbook.createWorkbook(outputFile, sourceWorkbook); but it completed successfully without errors.

When trying to open the .xlsx file created by selenium, getting a message stating that the file is corrupted

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.

Apache POI HSSF XLS reading error

Using the following code while reading in a .xls file, where s is the file directory:
InputStream input = new FileInputStream(s);
Workbook wbs = new HSSFWorkbook(input);
I get the following error message:
Exception in thread "main" java.io.IOException: Invalid header signature; read 0x0010000000060809, expected 0xE11AB1A1E011CFD0
I need a program that is able to read in either XLSX or XLS, and using the exact same code just adjusted for XSSF it has no problem at all reading in the XLSX file.
The Exception you're getting is one telling you that the file you're supplying isn't a valid Excel binary file, at least not a valid Excel file produced since about 1990. The exception you're getting tells you what POI expects, and that it found something else instead which wasn't a valid .xls file, and wasn't anything else POI can detect.
One thing to be aware of is that Excel opens a wide variety of different file formats, including .csv and .html. It's also not very picky about the file extension, so will happily open a CSV file that has been renamed to a .xls one. However, since renaming a .csv to a .xls doesn't magically change the format, POI still can't open it!
.
From the exception, I can tell what's happening, and I can also tell you're using an ancient version of Apache POI! A header signature of 0x0010000000060809 corresponds to the Excel 4 file format, from about 25 years ago! If you use a more recent version of Apache POI, it'll give you a helpful error message telling you that the file supplied is an old and largely unsupported Excel file. New versions of POI do include the OldExcelExtractor tool which can pull out some information from those ancient formats.
Otherwise, as with all exceptions of this type, try opening the file in Excel and doing a save-as. That will give you an idea of what the file currently is (eg .html saved as .xls, .csv saved as .xls etc), and will also let you re-save it as a proper .xls file for POI to load and work with.
If the file is in xlsx format instead of xls you might get this error. I would try using the generic Workbook object (Also called the SS Usermodel)
Check out the Workbook interface and the WorkbookFactory object. The factory should be able to create a generic Workbook for you out of either xlsx or xls.
I thought I had a good tutorial on this, but I can't seem to find it. I'll keep looking though.
Edit
I found this little tiny snippet from Apache's site about reading and rewriting using the SS Usermodel.
I hope this helps!
Invalid header signature; read 0x342E312D46445025, expected 0xE11AB1A1E011CFD0
Well I got this error when I uploaded corrupted xls/xlsx file(to upload corrupt file I renamed sample.pdf to sample.xls). Add validation like :
Workbook wbs = null;
try {
InputStream input = new FileInputStream(s);
wbs = new HSSFWorkbook(input);
} catch(IOException e) {
// log "file is corrupted", show error message to user
}

how to solve JXL error : jxl.read.biff.BiffException: Unable to recognize OLE stream

i am trying to get cell data from my .csv file but it gets error :
jxl.read.biff.BiffException: Unable to recognize OLE stream
I don't understand how to solve this,please give me some solution
this code is for jxl api & is that api support to .csv?
Code for reference:
public void read() throws IOException, BiffException {
File inputWorkbook = new File(inputFile);
try
{
w = Workbook.getWorkbook(inputWorkbook.getAbsoluteFile());
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
for (row = 1; row < sheet.getRows(); row++)
{
ReadExcelLotSizeEntity readExcelLotSizeEntity =new ReadExcelLotSizeEntity();
cell = sheet.getCell(1,row);
type= cell.getType();
if (cell.getType() == CellType.LABEL)
{
symbol=cell.getContents();
System.out.println(":::::::::::::::::"+symbol);
readExcelLotSizeEntity.setSymbol(symbol);
}
int col=2;
cell = sheet.getCell(col,row);
while(!cell.getContents().equals("")||cell.getContents()!=null)
{
System.out.println("||||||||||||||||"+cell.getContents());
cell=sheet.getCell(col,row);
col++;
}
lotSize= new Double(cell.getContents());
readExcelLotSizeEntity.setLotSize(lotSize);
readExcelLotSizeEntity.setCreateUserId(1L);
readExcelLotSizeEntity.setCreateDtTm(new Date());
readExcelLotSizeHome.persist(readExcelLotSizeEntity);
}
} catch (BiffException e) {
e.printStackTrace();
}
}
I was also facing this problem earlier. I googled and read this post and many other posts that were asking for solution to this BiffException. I don't have the exact solution but as I fixed my problem you can do it too, perhaps.
I was trying to read data from the Excel file saved in MS Office 2010 and I was getting this error. I saved the file as an Excel 2003-7 and then read it without any problem. It may the case that this problem occurs in Office 10 but not in Office 2003-7.
I hope this will work in your case.
Saving File as "Excel 97-2003 Workbook" type solved my issue.
JXL library doesnot support .csv and .xslx formats, which is the format used by Excel-2010. hence, use Excel 97-2003 which is .xls foramatted and is supported by JXL library.
or else if you want to use excel-2010, use APACHE POI(XSSFWorkbooks) instead of JXL.
For using .csv format, google for CSVReader libraries.
JXL is a simple (and hence limited) API. If it says
Unable to recognize OLE stream
it is what it is. It doesn't quite understand your Excel XLS file. Have confidence that the error is legitimate. This API only supports *.xls files; it doesn't support, for example, *.csv or *.xlsx files. Obviously, having the file renamed to *.xls alone is not sufficient. It must be in Excel 97-2003 format too.
Copy all the cells from your *.csv or *.xlsx file.
Open MS Excel and paste the copied cells.
Save the file as MS Excel 97-2003 (*.xls) file.
This error will surely not appear again.
On the other hand, if you want to process other formats (xlsx, csv) directly, look for other tools like Apache POI.
Save the Excel file type as Excel 97-2003 Worksheet and extension type as xls
Actually you are using different version of csv file .Please save it in the exact version.
For ex: we should save the excel sheet in word as 9
save the file as Excel 97-2003 and also change the file format from xlsx to xlx , in the code(in the file name)
I was trying to read data from the Excel file saved in MS Office 2010 and I was getting this error. I saved the file as an Excel 2003-7 and then read it without any problem. It may the case that this problem occurs in Office 10 but not in Office 2003-7

Categories