1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java: Can you read an Excel Spreadsheet?

Discussion in 'Software/Coding' started by NanoNet, Jun 11, 2015.

Thread Status:
Not open for further replies.
  1. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    The tittle says it all(Well most of it xD).

    My question is: Is there a way to read an Excel Spreadsheet from Java? I know you can do this with a .txt file, but I don't know of any way to read an Excel Spreadsheet. If it is not possible, perhaps is there a way to change the Excel Spreadsheet to a .txt file, within Java's code? It would mean a lot if someone answered, Thanks!
     
  2. OrangeGE

    OrangeGE Member

    Joined:
    Jan 21, 2014
    Messages:
    4,682
    Likes Received:
    2,229
    Trophy Points:
    113
    Gender:
    Male
    Location:
    p.getLocation();
    I assume it would be possible, but using the .csv end and stating which column you wish to read. I only know how to read sheets in Python so i'm not much help here, sorry!

    @rubik_cube_man or @repository may be able to assist you. :)
     
    NanoNet likes this.
  3. alyphen

    alyphen Member

    Joined:
    Jan 4, 2014
    Messages:
    101
    Likes Received:
    96
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Technical Consultant
    Location:
    127.0.0.1
    Home Page:
    Yes, you can. There's a variety of different methods to do this depending on what you mean by "Excel Spreadsheet".
    If you're just creating a table or something that opens in Excel, the easiest way is just to do it with csv and write your own parser, since csv are essentially text files.
    If you're creating a slightly more advanced spreadsheet, I'd recommend using a library for OpenDocument spreadsheets, as these have better compatibility with LibreOffice and OpenOffice. One example is the Apache ODF toolkit.
    If you're using an existing spreadsheet, or some Microsoft-specific functionality, you will want to look at Apache POI. It's an incredibly powerful library that allows you to manipulate Microsoft Excel spreadsheets (both OOXML and the old 2003 formats).
    If you need any more help with this, I should be able to offer a little guidance.
     
    NanoNet likes this.
  4. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    Thank you very much, I will look into this as soon as possible! :p I've been programming basic stuff for a while now, but I haven't done anything huge. xD
     
  5. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    Re: With Microsoft Excel, I can save a spreadsheet as a .txt file, and choose what the separated value is (Such as Tab, single space, commas, etc.). Can I then read the file as a regular .txt file?
     
  6. alyphen

    alyphen Member

    Joined:
    Jan 4, 2014
    Messages:
    101
    Likes Received:
    96
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Technical Consultant
    Location:
    127.0.0.1
    Home Page:
    Yes - you can write your own parser for it. When separating with commas, this is essentially a csv file ("Comma Separated Values")
    Best practice would probably be to have a CSVFile class that takes a file into it's constructor (which it stores in a class variable/field). There would be a reload method that would parse the CSVFile and store the contents into a 2D array. The reload method is called in the constructor. Then finally, a getCellContents(int row, int col) method to retrieve the contents of any cell. You can then load the CSV file, retrieve any cell's contents, update them at a later time if necessary.
    If you need write access too, add a setCell method which writes to the array, and a save method which saves back to the file. To have the file auto-update on change, the save method must be called at the end of the setCell method.
    That's probably the easiest and cleanest way to do this.
     
  7. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    Thanks once again, I'm really trying to get an almost flawless understanding of writing to/reading files. You definitely helped me more than any YouTube video/google search!
     
  8. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    May someone please lock the thread? :p Thanks!
     
  9. OrangeGE

    OrangeGE Member

    Joined:
    Jan 21, 2014
    Messages:
    4,682
    Likes Received:
    2,229
    Trophy Points:
    113
    Gender:
    Male
    Location:
    p.getLocation();
    Thread locked, the original topic has been resolved. :)
     
Thread Status:
Not open for further replies.