Apache POI, ExcelSheet & Jar...

Discussion in 'Java' started by julielaurek@gmail.com, Jul 12, 2007.

  1. Guest

    Hey guys!

    So I have his modified code to create an excel sheet and when I run
    it, it does create one, although I have to manually go open the
    sheet;


    //import java.awt.List;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.lang.Double;
    import java.lang.String;
    import java.util.*;


    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    public class SimpleSpreadsheetTest{
    //SerialDemo serialDemo ;
    //public static String[] serialOutput;
    public static String[] serialOutput = {"1","M","43423",
    "2","B","50608", "3","M","53478", "4","B","55853"};
    public static Double myDoubleObject = null;
    public static String myStringObject;

    public static Double getTheString(Double someDouble, String
    someString){
    return someDouble.valueOf(someString);
    }

    public static double getTheDouble(int i){
    if (serialOutput.length != 0){
    ArrayList tempList = new ArrayList(Arrays.asList(serialOutput));
    String myStringObject = (String) tempList.get(i);
    myDoubleObject = getTheString(myDoubleObject, myStringObject);
    //return myDoubleObject.doubleValue();
    }
    return myDoubleObject.doubleValue();
    }
    public static void main(String[] args) throws IOException{
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("sheetAttemptOne...Work!");
    HSSFRow row ;


    if (serialOutput.length % 3 != 0){
    System.out.println("Insufficient Data!");
    }
    else
    {
    row = sheet.createRow(0);
    HSSFCell hssfCell = row.createCell((short)0);
    hssfCell.setCellValue("Event Number");
    hssfCell = row.createCell((short)1);
    hssfCell.setCellValue("Event Type");
    hssfCell = row.createCell((short)2);
    hssfCell.setCellValue("Time");

    for (int i=0; i<(serialOutput.length) ; i = i + 3){
    row = sheet.createRow(1 + (i/3));

    hssfCell = row.createCell((short)0);
    hssfCell.setCellValue(getTheDouble(i));
    hssfCell = row.createCell((short)1);
    hssfCell.setCellValue(serialOutput[i+1]);
    hssfCell = row.createCell((short)2);
    hssfCell.setCellValue(getTheDouble(i+2));
    }
    FileOutputStream fileOut = new
    FileOutputStream("workbookAttempt8.xls");
    wb.write(fileOut);
    fileOut.close();
    }
    }
    }

    /* HSSFCell hssfCell = row.createCell((short)0);
    hssfCell.setCellValue("Product");
    hssfCell = row.createCell((short)1);
    hssfCell.setCellValue("Sales");
    hssfCell = row.createCell((short)2);
    hssfCell.setCellValue("Price");
    hssfCell = row.createCell((short)3);
    hssfCell.setCellValue("Total");

    row = sheet.createRow(1);

    hssfCell = row.createCell((short)0);
    hssfCell.setCellValue("Pens");
    hssfCell = row.createCell((short)1);
    hssfCell.setCellValue(120.00);
    hssfCell = row.createCell((short)2);
    hssfCell.setCellValue(0.25);
    hssfCell = row.createCell((short)3);
    hssfCell.setCellFormula("B2*C2");




    FileOutputStream fileOut = new
    FileOutputStream("workbookAttempt1.xls");
    wb.write(fileOut);
    fileOut.close();
    }

    }*/


    So I wanted to make this executable and I have successfully exported
    it as a jar file. I am still learning so bear with my question if it's
    stupid O:) but why doesn't the excel sheet get created anymore when I
    launch the jar file?

    Thanks

    JL
    , Jul 12, 2007
    #1
    1. Advertising

  2. On Jul 12, 3:30 pm, "" <>
    wrote:
    > Hey guys!
    >
    > So I have his modified code to create an excel sheet and when I run
    > it, it does create one, although I have to manually go open the
    > sheet;
    >
    > //import java.awt.List;
    > import java.io.FileOutputStream;
    > import java.io.IOException;
    > import java.lang.Double;
    > import java.lang.String;
    > import java.util.*;
    >
    > import org.apache.poi.hssf.usermodel.HSSFCell;
    > import org.apache.poi.hssf.usermodel.HSSFRow;
    > import org.apache.poi.hssf.usermodel.HSSFSheet;
    > import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    >
    > public class SimpleSpreadsheetTest{
    > //SerialDemo serialDemo ;
    > //public static String[] serialOutput;
    > public static String[] serialOutput = {"1","M","43423",
    > "2","B","50608", "3","M","53478", "4","B","55853"};
    > public static Double myDoubleObject = null;
    > public static String myStringObject;
    >
    > public static Double getTheString(Double someDouble, String
    > someString){
    > return someDouble.valueOf(someString);
    > }
    >
    > public static double getTheDouble(int i){
    > if (serialOutput.length != 0){
    > ArrayList tempList = new ArrayList(Arrays.asList(serialOutput));
    > String myStringObject = (String) tempList.get(i);
    > myDoubleObject = getTheString(myDoubleObject, myStringObject);
    > //return myDoubleObject.doubleValue();
    > }
    > return myDoubleObject.doubleValue();
    > }
    > public static void main(String[] args) throws IOException{
    > HSSFWorkbook wb = new HSSFWorkbook();
    > HSSFSheet sheet = wb.createSheet("sheetAttemptOne...Work!");
    > HSSFRow row ;
    >
    > if (serialOutput.length % 3 != 0){
    > System.out.println("Insufficient Data!");
    > }
    > else
    > {
    > row = sheet.createRow(0);
    > HSSFCell hssfCell = row.createCell((short)0);
    > hssfCell.setCellValue("Event Number");
    > hssfCell = row.createCell((short)1);
    > hssfCell.setCellValue("Event Type");
    > hssfCell = row.createCell((short)2);
    > hssfCell.setCellValue("Time");
    >
    > for (int i=0; i<(serialOutput.length) ; i = i + 3){
    > row = sheet.createRow(1 + (i/3));
    >
    > hssfCell = row.createCell((short)0);
    > hssfCell.setCellValue(getTheDouble(i));
    > hssfCell = row.createCell((short)1);
    > hssfCell.setCellValue(serialOutput[i+1]);
    > hssfCell = row.createCell((short)2);
    > hssfCell.setCellValue(getTheDouble(i+2));
    > }
    > FileOutputStream fileOut = new
    > FileOutputStream("workbookAttempt8.xls");
    > wb.write(fileOut);
    > fileOut.close();
    > }
    > }
    >
    > }
    >
    > /* HSSFCell hssfCell = row.createCell((short)0);
    > hssfCell.setCellValue("Product");
    > hssfCell = row.createCell((short)1);
    > hssfCell.setCellValue("Sales");
    > hssfCell = row.createCell((short)2);
    > hssfCell.setCellValue("Price");
    > hssfCell = row.createCell((short)3);
    > hssfCell.setCellValue("Total");
    >
    > row = sheet.createRow(1);
    >
    > hssfCell = row.createCell((short)0);
    > hssfCell.setCellValue("Pens");
    > hssfCell = row.createCell((short)1);
    > hssfCell.setCellValue(120.00);
    > hssfCell = row.createCell((short)2);
    > hssfCell.setCellValue(0.25);
    > hssfCell = row.createCell((short)3);
    > hssfCell.setCellFormula("B2*C2");
    >
    > FileOutputStream fileOut = new
    > FileOutputStream("workbookAttempt1.xls");
    > wb.write(fileOut);
    > fileOut.close();
    > }
    >
    > }*/
    >
    > So I wanted to make this executable and I have successfully exported
    > it as a jar file. I am still learning so bear with my question if it's
    > stupid O:) but why doesn't the excel sheet get created anymore when I
    > launch the jar file?
    >
    > Thanks
    >
    > JL


    How are you launching the jar file? You should be using java -jar <jar
    file name>

    Also, when you create the jar file, the manifest needs to call out the
    main class (whose main() will be executed).

    Here is a link for more details : http://csdl.ics.hawaii.edu/~johnson/613f99/modules/04/jar-files.html

    -cheers,
    Manish
    Manish Pandit, Jul 13, 2007
    #2
    1. Advertising

  3. Jeff Higgins Guest

    julielaurek wrote:
    > So I wanted to make this executable and I have successfully exported
    > it as a jar file. I am still learning so bear with my question if it's
    > stupid O:) but why doesn't the excel sheet get created anymore when I
    > launch the jar file?
    >


    In addition to what Manish said:
    add a classpath entry in the
    manifest file for your POI library jar.

    Class-Path: poi-3.0.1.jar //or whatever your jar is called.
    Jeff Higgins, Jul 13, 2007
    #3
  4. Jacky Guest

    On 7 13 , 6 30 , ""
    > to make this executable

    1. You can use jbuild guide(New->Archive) to make an executebale file.
    2. since you've exported it as a jar file, make sure your jar file
    have a correct manifest file, and the classpath is reachable in your
    system envionment sets, otherwise, you can include all
    dependencies(third-party lib) in your jar file.

    Good lucky!
    Jacky, Jul 13, 2007
    #4
  5. Jacky Guest

    On 7 13 , 6 30 , "" <>
    wrote:
    > to make this executable



    1. You can use jbuild guide(New->Archive) to make an executebale
    file.
    2. since you've exported it as a jar file, make sure your jar file
    have a correct manifest file, and the classpath is reachable in your
    system envionment sets, otherwise, you can include all
    dependencies(third-party lib) in your jar file.

    Good luck!
    Jacky, Jul 13, 2007
    #5
  6. Guest

    On Jul 12, 9:49 pm, Jacky <> wrote:
    > On 7 13 , 6 30 , "" <>
    > wrote:
    >
    > > to make this executable

    >
    > 1. You can use jbuild guide(New->Archive) to make an executebale
    > file.
    > 2. since you've exported it as a jar file, make sure your jar file
    > have a correct manifest file, and the classpath is reachable in your
    > system envionment sets, otherwise, you can include all
    > dependencies(third-party lib) in your jar file.
    >
    > Good luck!


    Hey guys!

    Thanks so much for replying. So I've been trying to follow everyone's
    advice, in order:

    Manish, I'm using Eclipse; that's where I created the jar file. Other
    jar files I created had worked. I think I usually see two options when
    trying to create the jar, one of which is to create one's own manifest
    and the other, to let eclipse do it. I always did the latter =)

    I'm following the link Manish gave me, and Jeff's advice, but I'm
    slightly stuck: is there a way to view your class' current classpath?
    I somehow learnt java without encountering that word till this
    summer :-? Also, I keep on getting this error:
    "jar is not recognized as an internal or external command operable
    program or batch file".
    and I'm googling solutions to that :) But if anyone has any, any
    solutions, please do tell; my jar is located on my desktop, and I
    think my java.exe is located here: C:\jdk1.5.0_12\jre\bin (among other
    places)

    I'll be trying jbuild next. I think the problems I have now are:
    fixing the jar problem from the command prompt and
    figuring out the proper way of finding and writing a classpath.

    s.o.s

    thanks for helping!

    jl
    , Jul 13, 2007
    #6
  7. Jeff Higgins Guest

    julielaurek wrote:
    >

    Here's a pretty good article.
    <http://www.ibm.com/developerworks/library/j-jar/>
    Jeff Higgins, Jul 13, 2007
    #7
  8. Guest

    On Jul 13, 3:00 pm, "Jeff Higgins" <> wrote:
    > julielaurek wrote:
    >
    > Here's a pretty good article.
    > <http://www.ibm.com/developerworks/library/j-jar/>



    Thanks!!!!
    I'm reading that and I got much further. Still trying to debug
    something that has to do with my manifest file, but I think I'll get
    there. The site says I can put my manifest somewhere not in the
    application's directory. I had assumed that would mean it could track
    down my manifest file wherever which is probably wrong since it sends
    me a java.io.Filenotfoundexception file not found: MyManifest (not in
    those exact words, but...)
    I am also curious as to something in the line of the Excel sheet I
    will be creating in th elong run. I know it's possible to make a
    component visible via "setVisible", I think. Is there a way to do this
    to an excel sheet? maybe casting it in some way to make it become a
    component? I tried that but it didn't turn out too well...

    Thanks

    jl
    , Jul 16, 2007
    #8
  9. Jeff Higgins Guest

    julielaurek wrote:
    > I'm reading that and I got much further. Still trying to debug
    > something that has to do with my manifest file, but I think I'll get
    > there. The site says I can put my manifest somewhere not in the
    > application's directory. I had assumed that would mean it could track
    > down my manifest file wherever which is probably wrong since it sends
    > me a java.io.Filenotfoundexception file not found: MyManifest (not in
    > those exact words, but...)


    I think I remember reading somewhere upthread that you are using Eclipse.
    In that case here might be a better reference.
    <http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.jdt.doc.user/tasks/tasks-33.htm>
    also:
    <http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html>

    > I am also curious as to something in the line of the Excel sheet I
    > will be creating in th elong run. I know it's possible to make a
    > component visible via "setVisible", I think. Is there a way to do this
    > to an excel sheet? maybe casting it in some way to make it become a
    > component? I tried that but it didn't turn out too well...
    >


    I think, also from a earlier comment you made, that what you are asking is:
    How can I open, in Excel, the worksheet that I just created, from my Java
    program?
    If that's the case, see:
    <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html>
    If not, then I'm not sure.

    PS - I was very easily able to run your SimpleSpreadsheetTest class from an
    executable jar exported from Eclipse. Just be sure that you put a copy of
    your POI.jar in the same directory as your executable SST.jar \or specify
    where to find it in your manifest files' Class-Path: property.
    Jeff Higgins, Jul 16, 2007
    #9
  10. Guest

    On Jul 16, 10:56 am, "Jeff Higgins" <> wrote:
    > julielaurek wrote:
    > > I'm reading that and I got much further. Still trying to debug
    > > something that has to do with my manifest file, but I think I'll get
    > > there. The site says I can put my manifest somewhere not in the
    > > application's directory. I had assumed that would mean it could track
    > > down my manifest file wherever which is probably wrong since it sends
    > > me a java.io.Filenotfoundexception file not found: MyManifest (not in
    > > those exact words, but...)

    >
    > I think I remember reading somewhere upthread that you are using Eclipse.
    > In that case here might be a better reference.
    > <http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.jdt.doc.u...>
    > also:
    > <http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html>
    >
    > > I am also curious as to something in the line of the Excel sheet I
    > > will be creating in th elong run. I know it's possible to make a
    > > component visible via "setVisible", I think. Is there a way to do this
    > > to an excel sheet? maybe casting it in some way to make it become a
    > > component? I tried that but it didn't turn out too well...

    >
    > I think, also from a earlier comment you made, that what you are asking is:
    > How can I open, in Excel, the worksheet that I just created, from my Java
    > program?
    > If that's the case, see:
    > <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html>
    > If not, then I'm not sure.
    >
    > PS - I was very easily able to run your SimpleSpreadsheetTest class from an
    > executable jar exported from Eclipse. Just be sure that you put a copy of
    > your POI.jar in the same directory as your executable SST.jar \or specify
    > where to find it in your manifest files' Class-Path: property.



    it worked! i fixed my classpaths according to the websites (thanks for
    those links, guys!) and i also think java couldn't find my poi jars
    since they were in a folder in the jre library instead of just being
    in the library.

    Thanks a million!!!!!!!!!!!!!!!!

    JL
    , Jul 17, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Si

    Apache POI Question

    Si, Dec 16, 2004, in forum: Java
    Replies:
    1
    Views:
    549
    TechBookReport
    Dec 17, 2004
  2. Replies:
    0
    Views:
    14,078
  3. jelle79
    Replies:
    3
    Views:
    530
    jelle79
    May 29, 2008
  4. Bruno Alexandre

    GridView 2 ExcelSheet

    Bruno Alexandre, May 18, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    119
    Bruno Alexandre
    May 18, 2006
  5. rguarnieri

    ContentType Excelsheet Title

    rguarnieri, Jun 1, 2007, in forum: ASP General
    Replies:
    2
    Views:
    128
    Anthony Jones
    Jun 3, 2007
Loading...

Share This Page