Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Loading Data in separate class

Reply
Thread Tools

Loading Data in separate class

 
 
sav
Guest
Posts: n/a
 
      07-12-2007
Hi everyone, i am having a little trouble, i am relatively new to java
and I am having difficulty with a little methodology.

I am using the java excel api to import some data from excel into a
routine for some data analysis. Below is a section of the code which
at the moment imports the data into java.


//import java.io.File;
//import java.io.FileInputStream;

//import jxl.Cell;
//import jxl.Sheet;
//import jxl.Workbook;


//javac -classpath c:\jexcelapi\jxl.jar;. JET2.java
//java -classpath c:\jexcelapi\jxl.jar;. JET2
//java -Xms10m -Xmx100m -classpath jxl.jar spreadsheet.xls

//java -Xms100m -Xmx200m -classpath c:\jexcelapi\jxl.jar;. JET2

import java.io.File;
import java.util.Date;
import jxl.*;

public class JET2 {

public static void main (String[] args){
Workbook workbook;
try {

workbook = Workbook.getWorkbook(new File("c:\\Docs_2\\Dad.xls"));

double stringa1 = 0 ;
double stringa2 = 0 ;

Sheet sheet1 = workbook.getSheet(0);
Sheet sheet2 = workbook.getSheet(0);

Cell a1 = sheet1.getCell(1,1);
Cell a2 = sheet2.getCell(0,5);

if (a1.getType() == CellType.NUMBER)
{
NumberCell fc1 = (NumberCell)a1;
stringa1 = fc1.getValue();
}
if (a2.getType() == CellType.NUMBER)
{
NumberCell fc2 = (NumberCell)a2;
stringa2 = fc2.getValue();
}
//String stringa1 = a1.getContents();
//String stringa2 = a2.getContents();

workbook.close();

}// End try

catch (Exception e){

System.out.println("Exception thrown: "+ e);

}// End catch

}// End Main
}// End class Jet


Now this portion works fine, however, I want to place the import code
section into a class or a method on its own, for a couple of reasons,
its cleaner and the routine is quite lengthy and i dont want to have a
cluttered piece of code. How do i place the code into a section on its
own that can be called simply by using something like

public class JET2 {

public static void main (String[] args){

Import data ......(utilising the above code to import data from excel)

Do stuff with data.........(data manipulation)

}// End Main
}// End class Jet

I seem to misunderstanding something very basic or can this not be
done?

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

Here's one way

public class JET2
{
public static void main(String[] args)
{
processWorkbook(fetchWorkbook(args[0]));
}

public static void processWorkbook(Workbook workbook)
{
double stringa1 = 0;
double stringa2 = 0;

Sheet sheet1 = workbook.getSheet(0);
Sheet sheet2 = workbook.getSheet(0);

Cell a1 = sheet1.getCell(1, 1);
Cell a2 = sheet2.getCell(0, 5);

if (a1.getType() == CellType.NUMBER)
{
NumberCell fc1 = (NumberCell) a1;
stringa1 = fc1.getValue();
}
if (a2.getType() == CellType.NUMBER)
{
NumberCell fc2 = (NumberCell) a2;
stringa2 = fc2.getValue();
}
workbook.close();
}

public static Workbook fetchWorkbook(String fileName)
{
Workbook workbook = null;
try
{
workbook = Workbook.getWorkbook(new File(fileName));
}
catch (Exception e)
{
e.printStackTrace();
}
return workbook;
}
}




 
Reply With Quote
 
 
 
 
sav
Guest
Posts: n/a
 
      07-12-2007
On 12 Jul, 21:17, "Jeff Higgins" <oohigg...@yahoo.com> wrote:
> Here's one way
>
> public class JET2
> {
> public static void main(String[] args)
> {
> processWorkbook(fetchWorkbook(args[0]));
> }
>
> public static void processWorkbook(Workbook workbook)
> {
> double stringa1 = 0;
> double stringa2 = 0;
>
> Sheet sheet1 = workbook.getSheet(0);
> Sheet sheet2 = workbook.getSheet(0);
>
> Cell a1 = sheet1.getCell(1, 1);
> Cell a2 = sheet2.getCell(0, 5);
>
> if (a1.getType() == CellType.NUMBER)
> {
> NumberCell fc1 = (NumberCell) a1;
> stringa1 = fc1.getValue();
> }
> if (a2.getType() == CellType.NUMBER)
> {
> NumberCell fc2 = (NumberCell) a2;
> stringa2 = fc2.getValue();
> }
> workbook.close();
> }
>
> public static Workbook fetchWorkbook(String fileName)
> {
> Workbook workbook = null;
> try
> {
> workbook = Workbook.getWorkbook(new File(fileName));
> }
> catch (Exception e)
> {
> e.printStackTrace();
> }
> return workbook;
> }
>
> }


Jeff thanks a bunch that is just the thing i was trying to achieve,
many thanks

 
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
Separate Template Definition I wrote class Data in header. The C++Compiler compiled without errors. I decided to move all member functionsinto source code because they are for implementation. I do not like thatthey are placed in class body. Immortal Nephi C++ 12 07-30-2010 11:54 AM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:27 AM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:26 AM
Using separate classpaths for separate classes? Frank Fredstone Java 1 06-27-2006 06:46 AM
How to use several separate classes (separate files) to be executed in one class (another file) EvgueniB Java 1 12-15-2003 01:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57