Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Apache POI, ExcelSheet & Jar...

Reply
Thread Tools

Apache POI, ExcelSheet & Jar...

 
 
julielaurek@gmail.com
Guest
Posts: n/a
 
      07-12-2007
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

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      07-12-2007
On Jul 12, 3:30 pm, "(E-Mail Removed)" <(E-Mail Removed)>
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/...jar-files.html

-cheers,
Manish

 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      07-13-2007

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.


 
Reply With Quote
 
Jacky
Guest
Posts: n/a
 
      07-13-2007
On 7 13 , 6 30 , "(E-Mail Removed)"
> 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!

 
Reply With Quote
 
Jacky
Guest
Posts: n/a
 
      07-13-2007
On 7 13 , 6 30 , "(E-Mail Removed)" <(E-Mail Removed)>
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!

 
Reply With Quote
 
julielaurek@gmail.com
Guest
Posts: n/a
 
      07-13-2007
On Jul 12, 9:49 pm, Jacky <(E-Mail Removed)> wrote:
> On 7 13 , 6 30 , "(E-Mail Removed)" <(E-Mail Removed)>
> 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

 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      07-13-2007

julielaurek wrote:
>

Here's a pretty good article.
<http://www.ibm.com/developerworks/library/j-jar/>


 
Reply With Quote
 
julielaurek@gmail.com
Guest
Posts: n/a
 
      07-16-2007
On Jul 13, 3:00 pm, "Jeff Higgins" <(E-Mail Removed)> 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

 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      07-16-2007

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.


 
Reply With Quote
 
julielaurek@gmail.com
Guest
Posts: n/a
 
      07-17-2007
On Jul 16, 10:56 am, "Jeff Higgins" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use Excelsheet in ASP.NET for calculation jelle79 ASP .Net 3 05-29-2008 09:08 AM
ContentType Excelsheet Title rguarnieri ASP General 2 06-03-2007 04:47 PM
GridView 2 ExcelSheet Bruno Alexandre ASP .Net Datagrid Control 0 05-18-2006 09:51 AM
Apache FileUpload - java.lang.NoClassDefFoundError: org/apache/commons/io/FileCleaner kebabkongen@hotmail.com Java 2 03-16-2006 09:20 AM
AXIS jars org.apache.axis.wsi.* and org.apache.axis.transport.jms.* unkwb@web.de Java 0 02-23-2005 04:02 PM



Advertisments