Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Apache POI

  1. #1
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Apache POI

    First off I feel that I should state that I am very much a Java novice. For my research I am modifying a very extensive code in order to optimize aspects of the data analysis process. In order to do this I need to export the data generated in the java code to an excel document where I can then arrange and display the data along with a few other calculations. I've referenced the internet and decided to use Apache POI 3.7 to export the data. As of now I just have written some very simple code in an example project just to get a feel for the POI. However I'm getting syntax errors with the last two lines of my code which I assume is relatively easy to fix.

    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;

    import java.io.FileOutputStream;
    import java.io.File;
    import java.io.IOException;

    public class Example {

    Workbook wb = new HSSFWorkbook();
    //Workbook wb = new XSSFWorkbook();
    Sheet sheet1 = wb.createSheet("new sheet");
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();

    }

    Also, in the future when I am looking to export my data I assume my data will be stored as some string but I wasn't sure if the data has to be stored or labeled in the java program in order to effectively be exported to Excel.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Apache POI

    syntax errors
    What error? It often helps to post the message in their entirety. From what it looks like, I'm guessing its something like uncaught exception - as the last few lines can throw an IOException. See Lesson: Exceptions (The Java™ Tutorials > Essential Classes) to learn more about exceptions, but you can readily solve the problem by catching the exception, or declaring the method to throw an exception

    try{
       ///write workbook
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        //do cleanup, like closing Input/Output Streams
    }

  3. #3
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Apache POI

    Sorry for not posting the entire error in the first post but here is the message in its entirety: "Syntax error on token "fileOut", VariableDeclaratorld expected after this token".

  4. #4
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Apache POI

    I've now altered my code and it seems to be written with no errors however when I try to run the code I receive the error from Eclipse "Launch configuration ExcelExtractor references non-existing project TestPOI." Any help with this error would be greatly appreciated.

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Apache POI

    Have you defined an application entry point (main method)? If so the launch configuration needs to know about it (if you have a class which contains main open it should automatically do this for you)

  6. #6
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Apache POI

    This code that I've written is merely a test run through with the API and will most definitely need to be extended but so far all I've created is a new java project containing one class file. The following code is all the public class file contains:

    import org.apache.poi.hssf.usermodel.*;
    import java.io.FileOutputStream;
    import java.io.File;
    import java.io.IOException;

    public class Example {

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet firstsheet = workbook.createSheet("Data 1");

    FileOutputStream fos=null; {
    try{
    fos = new FileOutputStream(new File("Data.xls"));
    workbook.write(fos);
    }catch(IOException e){
    e.printStackTrace();
    }finally{
    if (fos!=null){
    try{
    fos.flush();
    fos.close();
    }catch(IOException e){
    e.printStackTrace();
    }
    }
    }
    }
    }
    To execute this program I have simply been trying to run the class file and the project from Eclipse and am receiving the previously stated "ExcelExtractor" error. Am I trying to execute the program incorrectly?

  7. #7
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Apache POI

    You need a main method application entry point, which you don't seem to have.
    public class MyClass{
        public static void main(String[] args){
     
        }
    }

    Back to the basics:
    Lesson: A Closer Look at the "Hello World!" Application (The Java™ Tutorials > Getting Started)

    BTW: for future reference see my signature for how to use the code tags - makes your code much more readible

  8. #8
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Apache POI

    I added a main method to the class file but I am still receiving the same error as before, "Launch configuration ExcelExtractor references non-existing project TestPOI."

  9. #9
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Apache POI

    Post what you have so far (with the main method) as an SSCCE

  10. #10
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Apache POI

    Here is what I have written so far:
    import org.apache.poi.hssf.usermodel.*;
    import java.io.FileOutputStream;
    import java.io.File;
    import java.io.IOException;
     
    class Example {
    	public static void main(String[] args) {
    HSSFWorkbook workbook = new HSSFWorkbook();
     
    FileOutputStream fos=null; {
    try{
    	fos = new FileOutputStream(new File("Data.xls"));
    	workbook.write(fos);
    }catch(IOException e){
    	e.printStackTrace();
    }finally{
    	if (fos!=null){
    		try{
    			fos.flush();
    			fos.close();
    		}catch(IOException e){
    			e.printStackTrace();
    		}
    		}
    	}
    }
    }}

  11. #11
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Apache POI

    Just out of curiosity I booted into windows 7 and ran the same code I posted above in eclipse and did not receive the error I had previously posted about. However, although the Excel file was written, upon opening the file an error is displayed as follows " File error: data may have been lost." I wasn't too concerned because my original code didn't contain any data to transfer but when putting in some basic data to transfer I still am receiving the same error and no data is being displayed in the excel document. Any suggestions on how to overcome this would be greatly appreciated.

Similar Threads

  1. need help with 'org.apache.commons.net.ftp.FTPClient'
    By rtumatt in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 22nd, 2013, 07:02 PM
  2. Problem with apache xml-rpc lib
    By tah_206207 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2011, 02:31 PM
  3. Apache server restsrting problem
    By Gaurav Gupta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2011, 04:50 AM
  4. org.apache.derby does not exist?
    By disclaimer in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 30th, 2010, 04:58 PM
  5. apache-jboss
    By supriya ramjee in forum Web Frameworks
    Replies: 0
    Last Post: August 12th, 2009, 05:37 AM