www.webdeveloper.com
Results 1 to 11 of 11

Thread: How to read xlsx file from local java file

  1. #1
    Join Date
    Oct 2011
    Location
    Noida, INDIA
    Posts
    11

    Angry How to read xlsx file from local java file

    Hi Experts,

    I want to create a local java file to read a xlsx-file . I have gone through some code such as using POI-3.5 (which is for jdk 1.5) but I want to do it using jdk 1.4.
    Can anyone help me into this ?
    Thanks in advance

  2. #2
    Join Date
    Oct 2005
    Posts
    843
    Unfortunately I don't think this is possible, I took a look through some of the changes for all the different versions of Apache POI and it looks like support for XLSX wasn't added until version 3.5 - beta 1.

    As their website lists, you require JDK 1.5 to even compile the 3.5 source code, as I'm assuming they are using classes and methods not available in the 1.4 JDK.

    Your one alternative would be to have people save the XLSX files as plain old XLS files, then utilize POI version 3.2 Final which can be used with the 1.4 JDK.

    Edit: I did a little looking around, and there are some other tools apart form the Apache POI that can read in XLSX files, however I'm unsure of how they work or what version of the JDK they require. I'd suggest taking a look at some of the suggestions in this Stack Overflow post: http://stackoverflow.com/questions/2...x-file-in-java
    Last edited by welsh; 01-29-2012 at 03:18 AM.

  3. #3
    Join Date
    Oct 2011
    Location
    Noida, INDIA
    Posts
    11
    Thanks for your reply,

    You are right about POI-3.5 . It doesn't support jdk 1.4. I even tried POI-3.2-FINAL but it only supports xls-file, not xlsx file(which is required).

    Does anyone having idea about Aspose-API ? It is fulfilling my requirements but I am not familiar with its code.....

  4. #4
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    xlsx is actually just an archive (.zip) of xml files that describe the workbook. You could unzip and concatenate [archive] _xl/* to get a somewhat legible xml document that describes the data contained in the xlsx document. You will need to put a root element around the result of what you concatenate.

  5. #5
    Join Date
    Oct 2005
    Posts
    843
    Quote Originally Posted by eval(BadCode) View Post
    xlsx is actually just an archive (.zip) of xml files that describe the workbook. You could unzip and concatenate [archive] _xl/* to get a somewhat legible xml document that describes the data contained in the xlsx document. You will need to put a root element around the result of what you concatenate.
    That's very interesting, I never knew that is what the xlsx format actually was at its core. Learn something new everyday.

    Quote Originally Posted by SAPjava View Post
    Thanks for your reply,

    You are right about POI-3.5 . It doesn't support jdk 1.4. I even tried POI-3.2-FINAL but it only supports xls-file, not xlsx file(which is required).

    Does anyone having idea about Aspose-API ? It is fulfilling my requirements but I am not familiar with its code.....
    I'm unfamiliar with this API due to the cost, have you considered eval(BadCode)'s suggestion of how to access the xlsx without utilizing a third party API.

    My main question is after learning about what an xlsx file is, is what do you need to accomplish by opening the xlsx file? Just read in the contents or read in and change values and save the updated? Compile multiple xlsx files?

  6. #6
    Join Date
    Oct 2011
    Location
    Noida, INDIA
    Posts
    11
    Hi Welsh,

    I just need to read an xlsx-file's content using jdk 1.4.

  7. #7
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    something like this in bash would work:

    Code:
    echo '<workbook>'"$( unzip -pq wb.xlsx xl/worksheets/*.xml | sed -e 's/<?xml\(.*\)//g' )"'</workbook>' > wb.xml
    that is...

    write a file, wb.xml, containing

    <workbook>
    ,
    the concatenated contents of the unzipped files /xl/worksheets/*.xml in the zip archive wb.xlsx after removing the xml version header on each worksheet
    ,
    </workbook>

    in that order

    I'm sure there's a java package to decompress, or "unzip", .zip files as well as a better alternative to sed for editing xml.



    A word of warning about date/datetime: If xlsx is anything like xls, then date/datetime will be stored as integers/float (if I recall correctly). I believe date will be an intenger representing days with an offset of 1980-01-01 (the year). datetime is similar but with the right hand of the decimal somehow representing time.

    If you're on windows you can rename .xlsx to .zip and see for yourself.

  8. #8
    Join Date
    Oct 2005
    Posts
    843
    Quote Originally Posted by eval(BadCode) View Post
    I'm sure there's a java package to decompress, or "unzip", .zip files as well as a better alternative to sed for editing xml.
    I actually came across the java.util.zip package, that is available in the 1.4 JDK.

    You can see some examples of Compressing and Decompressing files here: http://java.sun.com/developer/techni...g/compression/

    You can see the java package for the 1.4 JDK here: http://docs.oracle.com/javase/1.4.2/...e-summary.html

    So if you utilize the knowledge that eval(BadCode) has provided about xlsx files, and you utilize the package above to unzip the xlsx file to access the xml work sheet files you can then create something that can read in your xlsx files.

  9. #9
    Join Date
    Oct 2011
    Location
    Noida, INDIA
    Posts
    11
    I think, i am missing something.
    Can i save my xlsx file as ".zip" ? and then use decompression code to read it ?

  10. #10
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    Renaming it is not necessary. That is just in case you are using an operating system that uses the extension to determine which program is best suited to open the file with.

    A files name is what you make of it. The data inside the file is what we are concerned with in this thread.

    Quote Originally Posted by SAPjava View Post
    I think, i am missing something.
    Can i save my xlsx file as ".zip" ? and then use decompression code to read it ?
    Yes you can. But you do not HAVE to rename it, that is optional.

  11. #11
    Join Date
    Oct 2005
    Posts
    843
    As eval(BadCode) said, you don't have to rename the file. Extensions just help the Operating System determine which program to utilize to open the file by default. This is why when you click .txt files you by default get notepad in windows or by default .html files in your default browser.

    In the examples of Compressing and Decompressing a zip file that I posted the link you, you would simply go:
    Code:
    ZipFile zipfile = new ZipFile("myfile.xlsx");
    Instead of
    Code:
    ZipFile zipfile = new ZipFile("figs.zip");
    Or
    Code:
    FileInputStream fis = new FileInputStream("myfile.xlsx");
    instead of
    Code:
    FileInputStream fis = new FileInputStream("figs.zip");

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles