Parse XLSX files in Java without external libraries? - java

quick question I've been asked to create a couple of parsers for XLSX file formats. Pretty much everywhere I've read says to grab the POI libraries, however the system I am working on are very touchy about bringing on external APIs so I'd far rather have to do some extra leg work myself then go down that route.
So is it possible (without spending days of coding) via a SAXParser to Parse an XLSX file or am I a mug if I dont use the POI libraries?
Cheers
* UPDATE *
Since extracting the XLSX fileand having a better look at the archive, I believe I can now parse these files without spending days coding, I could probably extract the information within a few hours. I am however only looking to extract the physical cell data and not any reference data on those values i.e. cell reference. I am also looking to extract the XLSX metadata. I'll provide a quick answer on how I did this when I am done for future reference.

Without spending few days of coding...it's not possible...you have to write code for at least two three days....it's just a zip file but bunch of XML files and manifest xml

a standard xlsx file is not xml so nope its not possible.
correction: Walter Laan is correct, xlsx format is indeed a zip file full of xml's and shoud be relativly easily parseable

Effectively I did this, but obviously tailored my java to read the specific xlsx XML structure.
To open the xlsx in java use the ZipEntry API's & enumerate that entry to ensure you drill down through all the various folder structures. Then follow the guide below to read the XML:
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
Cheers

Related

Are there any languages that can be integrated with java that read large XLSX files efficiently?

So i'm working on an app that needs to frequently read large XLSX files. I'm using Java, and Apache POI keeps running out of memory on certain XLSX files. I know theres a way to XML parse with POI, but it looks pretty messy.
Resaving these files as another format (XLS, CSV) is not an option because the entire process needs to be automated, and some of these files have multiple sheets or exceed the row count allotted for XLS files.
I've also thought about writing a script to "recreate" the excel files with only the underlying data, but this is not ideal because there are formats that need to be preserved on some files.
Are there any languages that I can call from Java that can read large XLSX files without memory issues?
#Gus, I had the same problem. I had to read a 13MB XLSX and ran out of heap with conventional POI. I had to implement XSSF+SAX API to read the file. Although very difficult to understand at first, I'm able to read my XLSX file easily with it (and very quickly, too).
http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api
The Apache guys give an example of it's usage in the link. In my case I copied the code and adapted to my needs.

How to create docx file using java?

I am trying to create a .docx file using java but for some reason I can't open the file. The error comes "Problem with the content of file". Does anyone knows how to fix this problem?
Tried multiple libraries
This one is free.
http://www.docx4java.org/trac/docx4j
Also check aspose (is not free)
http://www.aspose.com/categories/java-components/aspose.total-for-java/default.aspx
you may use http://poi.apache.org/ to create such files.
Microsoft Word's docx files are zip files with specific content inside them. Simply creating a file in Java and writing some text to them isn't going to create a valid docx file that Word will recognize, even if you give it a .docx extension.
To create them from Java you can use the Apache POI XWPF library. That will give you some Java classes that'll create and write contents to docx files that will work with Word.
It sounds like you are producing a corrupt document. If it really is DOCX format, then open it with a ZIP tool and examine the contexts of the XML files - there's a reasonable chance you are simply producing invalid XML and so looking at it with a browser or XML editor will help you there.
You probably need to say HOW you are producing the DOCX file so people can make better suggestions.
If you are looking for more options, I would look at docx4j and Docmosis. Please note I work for the company that created Docmosis.

How to load old Microsoft Office XML file (Excel) using Java

I'm not able to load an Excel file in the older Office XML format (think Office 2002 or 2003 version) into Java. I tried JXL and Apache's POI (version 3.7). POI doesn't work since it appears to want the newer Office .xlsx format.
Here's an example of the older Office XML format.
One can generate a similar XML file from MS Excel 2010 by saving the workbook as the format "XML Spreadsheet 2003"?
Are there any open-source Java libraries that will load the XMLSS format? Otherwise I have no choice but to write a custom parser: read the XML file then interpret the cell tags to build out the cell matrix. In this XML format, any rows with empty cell values are skipped, the next cell with data positioned with an index attribute that acts like an offset in the columns, I assume to save space in the XML file.
The format is called SpreadsheetML (do not confuse with .xlsx which is also xml-based), a library called Xelem can handle it:
import nl.fountain.xelem.excel.Workbook;
import nl.fountain.xelem.lex.ExcelReader;
//...
ExcelReader reader = new ExcelReader();
Workbook xlWorkbook = reader.getWorkbook("c:\\my\\spreadsheet.xml");
System.out.println(xlWorkbook.getSheetNames());
Copying Mark Beardsley's answer from POI team http://apache-poi.1045710.n5.nabble.com/How-to-convert-xml-to-xls-td2306602.html :
You have got an Office 2003 xml file there, not an OpenXML file; it is an early attempt by Microsoft to create an xml based file format for Excel and it is in that sense a 'valid' Office file format.
Sadly, POI cannot interpret this file at all and that is why you saw the exception when you tried to wrap it up in the InputStream and pass it to WorkbookFactory(s) constructor. You do however have a number of options;
You could use Excel itself and manually open and save each file you wish to convert, as you already have done.
If you have access to Visual Studio and can write Visual Basic or C# code then you could use a control that will allow you to control Excel programmatically. This way you could automate a file conversion process using Excel itself. Then once the file has been converted wither to the binary or OpenXML formats, POI can be used to process it.
If you are running on a stand alone PC on which a copy of Excel is installed and using the Windows operating system, then you could use OLE to do something very similar from Java code. As above, POI can be used to process the file following the conversion.
If you have access to OpenOffice, it has a rather good API that is accessible from Java code. You could use it to convert between the file types for you - it is simply a matter of discovering the correct filter to use in this case. OpenOffice is good for all except the most complex files and you should be able to use POI to process the file following conversion. However, if you choose this route, it may be best to do all of the work using OpenOffice's UNO api.
Depending upon what you want to do with the file's contents, you could create your own parser using core java code and either the SAX or Xerces parsers (consider using xmlBeans (http://xmlbeans.apache.org/) ). If you simply open the original xml file using a simple text editor, you can see that the structure is not complex and, if all you wish to get at is the raw data it contains, this could be your best option.
After a lot of pain I've found a solution to this. JODConverter uses the OpenOffice.org/LibreOffice API and can convert SpreadsheetML to whatever formats OpenOffice.org suppports.
You might get some result using the OpenOffice API. If not directly you could probably convert to a 'supported' format.
Otherwise the schema for the Office 2003 'SpreadsheetML' isn't very complicated. I have succesfully created an xslt scenario to convert a resultset (database query) to a (simple yet effective) Excel 2003 document (XML format). The other way around should not be very hard to achieve.
Cheers,
Wim
The answer today was to ask the vendor to change their Excel file format to an Excel binary rather than the old Office XML. Doing so allowed me to use Apache POI 3.7 to read the file with no issues. I appreciate the answers, as I had no idea there was no direct support in the Java-based open source libraries for this old Office XML format. Now I know next time to check earlier to see what format the Excel files are in before committing to a timeline.
I had the same problem some time ago, ended up writing a SAX parser to read the XML file. I wrote a blog post about it here.
You can find the sample project to parse the file in Github.

Convert from Excel xlsx to xls in Java

I have an Excel 2007 xlsx file that I would like to programmatically convert to an .xls file. The xlsx file is an export from a reporting tool, and I would like to convert it to xls for better compatibility with the software stack of my application users. The xlsx is as plain as it gets. Just rows with data and basic type information (int/date/string). No formulas.
My platform is Java, and I do not have Microsoft Office installed. I'm looking for a solution that will allow me to convert between the formats with the least amount of effort. I.e. I'd like to avoid having to write a custom "copy application" that would read the xlsx file and copy the rows and formatting to another file. Preferably, the solution is open source and/or free.
I have looked at POI, and as far as I could tell, it can read and write both xls and xlsx files. But I was not able to tell by browsing the documentation and examples if it could read one format and write out in the other. Before I dig in any deeper, I would like to check if any of you out there have done anything like this before in Java, and if you have any tips.
Converting with POI would be a tedious task. I would like to point you to JODConverter. JODConverter is used by OpenOffice to convert its stuff, so it should work fine for that task.
However, that being said, I have not used JODConverter myself.

Spreadsheet Parser in Java/Groovy

Hi I'm looking to parse spreadsheets (xls/ods) in Groovy. I have been using the Roo library for Ruby and was looking to try the same tasks in Groovy, as Java is already installed on a development server I use, and I would like to keep the number of technologies on the server to a simple core few.
I am aware that the ods format is zipped XML, and so can be parsed as such, but I would like to process the file using spreadsheet concepts, not XML concepts.
The ability to process xls files is not of major importance, but would save me having to save multiple xls files to ods (as this is for parsing data from clients).
Thanks
I would suggest Apache POI for access to .xls files.
I've never had to work with the .ods format, so no information on that one.
There's also JExcelAPI, which has a nice, clean, simple interface (for the most part).
Can't help you with ODS Files though.
How about looking at 'odftoolkit' ? http://odftoolkit.openoffice.org/
Groovy in Action has a chapter named "Groovy on Windows" that discusses using Scriptom, a Groovy/COM bridge (using JACOB under the covers), to access several Windows apps including Excel.
For OpenOffice, you can use ODF Toolkit, as Amit pointed out.
I second jdmichal's vote for Apache POI. I have selected it as our library of choose to handle Excel file input (.XLS). The project is also working on the .XLSX file format if you ever decide you want to support that. Based on your specifications, I don't think you want to get into converting things into CSV and it seems like you have established input and output paths. For anyone who hasn't had the joy of dealing with CSV to Excel conversion, it can get a bit dicey. I have spent hours dealing with issues created by Excel converting string data to numeric data. You can see other testimonies to this effect on the POI Case Studies page. Beyond these issues, I simply don't want to personally have to handle these inputs. I'd rather invest the programming effort and streamline the workflow for the future.
I too have not dealt with ODF and have no plans to support it in my current project. You might want to check out the OpenOffice.org ODF Toolkit Project.
Good luck and have fun,
- D.
I suggest you to take a look at SimpleXlsBuilder and SimpleXlsSlurper, both are based on apache POI and can fit your basic needs for reading from and writing to Excel 97 spreadsheets in a concise way.
If your spreadsheets are simple enught - without charts and other embedded contents - you should simply convert the spreadsheet to CSV.
Pros:
Both xls and ods will produce the same CSV - You'll have to handle just one input type.
You won't have to mess with new versions of (Open) Office.
Handling plaintext is always more fun than other obscure formats.
Cons:
One that I can think of - finding a reliable converter from xls and odf to csv. Shouldn't be too hard - OpenOffice has a built in one.
A couple things:
1) I agree that using a CSV format can simplify some of the development work. OpenCSV can help with processing CSV files. There are other good CSV parsers for Java out there. Just remember that anything that's available for Java can be used by Groovy due to Groovy's unparalleled integration with Java.
2) I know you said you wanted to avoid handling XML, but Groovy makes XML processing exceedingly simple.

Categories