Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Apache POI, ExcelSheet & Jar...

Thread view: 
julielaurek@gmail.com - 12 Jul 2007 23:30 GMT
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
Manish Pandit - 13 Jul 2007 00:24 GMT
On Jul 12, 3:30 pm, "julielau...@gmail.com" <julielau...@gmail.com>
wrote:
> Hey guys!
>
[quoted text clipped - 109 lines]
>
> 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
Jeff Higgins - 13 Jul 2007 02:02 GMT
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.
Jacky - 13 Jul 2007 02:36 GMT
On 7 13 ,   6 30 , "julielau...@gmail.com"
> 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 - 13 Jul 2007 02:49 GMT
On 7 13 ,   6 30 , "julielau...@gmail.com" <julielau...@gmail.com>
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!
julielaurek@gmail.com - 13 Jul 2007 19:04 GMT
> On 7 13 ,   6 30 , "julielau...@gmail.com" <julielau...@gmail.com>
> wrote:
[quoted text clipped - 9 lines]
>
> 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
Jeff Higgins - 13 Jul 2007 20:00 GMT
julielaurek wrote:

Here's a pretty good article.
<http://www.ibm.com/developerworks/library/j-jar/>
julielaurek@gmail.com - 16 Jul 2007 15:14 GMT
> 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
Jeff Higgins - 16 Jul 2007 15:56 GMT
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
[quoted text clipped - 3 lines]
> 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/t
asks-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.
julielaurek@gmail.com - 17 Jul 2007 17:28 GMT
> julielaurek wrote:
> > I'm reading that and I got much further. Still trying to debug
[quoted text clipped - 28 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.