dalatian
asked on
Using POI in Java
Hi experts,
Pls help to guide me to use POI in Java. I am new to Java and have read many posts about POI but still unable to start using POI.
I have JDK 1.6 and JCreator intalled in my PC. I also downloaded the poi-bin-3.6-20091214 and extracted to C:\jdk6.
Pls help me to modify the classpath...and so on...
Thanks a lot
Pls help to guide me to use POI in Java. I am new to Java and have read many posts about POI but still unable to start using POI.
I have JDK 1.6 and JCreator intalled in my PC. I also downloaded the poi-bin-3.6-20091214 and extracted to C:\jdk6.
Pls help me to modify the classpath...and so on...
Thanks a lot
ASKER
Thank you for your respond. I've tried but here is the message:
Exception in thread "main" java.lang.NullPointerExcep tion
at org.apache.poi.poifs.files ystem.POIF SFileSyste m.closeInp utStream(P OIFSFileSy stem.java: 196)
at org.apache.poi.poifs.files ystem.POIF SFileSyste m.<init>(P OIFSFileSy stem.java: 158)
at OpenExcel.main(OpenExcel.j ava:28)
Process completed.
My questions are:
1- the lib folder means c:\jdk6\lib or c:\jdk6\jre\lib? (the jdk is installed in c:\jdk6)
2- where should I put the lines:
javac -cp .;lib/poi-3.6-20091214.jar ;lib/log4j -1.2.13.ja r POITest.java
and
java -cp .;lib/poi-3.6-20091214.jar ;lib/log4j -1.2.13.ja r POITest
Pls help because I new to java, thank you very much,
Exception in thread "main" java.lang.NullPointerExcep
at org.apache.poi.poifs.files
at org.apache.poi.poifs.files
at OpenExcel.main(OpenExcel.j
Process completed.
My questions are:
1- the lib folder means c:\jdk6\lib or c:\jdk6\jre\lib? (the jdk is installed in c:\jdk6)
2- where should I put the lines:
javac -cp .;lib/poi-3.6-20091214.jar
and
java -cp .;lib/poi-3.6-20091214.jar
Pls help because I new to java, thank you very much,
Looks like the POI jar is found when you run the program, but there is another problem there. Is OpenExcel your test program? Can you share the sourcecode?
ASKER
I just try to test using POI with this code fragment:
import java.lang.Object;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.poifs.files ystem.POIF SFileSyste m;
import org.apache.poi.hssf.usermo del.HSSFCe ll;
import org.apache.poi.hssf.usermo del.HSSFSh eet;
import org.apache.poi.hssf.usermo del.HSSFWo rkbook;
import org.apache.poi.hssf.usermo del.HSSFRo w;
/**
* A simple POI example of opening an Excel spreadsheet
* and writing its contents to the command line.
* @author Tony Sintes
*/
public class OpenExcel
{
public static void main( String [] args )
{
try
{
InputStream input = OpenExcel.class.getResourc eAsStream( "c:/List.xls" );
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
// Iterate over each row in the sheet
Iterator rows = sheet.rowIterator();
while( rows.hasNext() ) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println( "Row #" + row.getRowNum() );
// Iterate over each cell in the row and print out the cell's content
Iterator cells = row.cellIterator();
while( cells.hasNext() ) {
HSSFCell cell = (HSSFCell) cells.next();
System.out.println( "Cell #" + cell.getCellNum() );
switch ( cell.getCellType() ) {
case HSSFCell.CELL_TYPE_NUMERIC :
System.out.println( cell.getNumericCellValue() );
break;
case HSSFCell.CELL_TYPE_STRING:
System.out.println( cell.getStringCellValue() );
break;
default:
System.out.println( "unsuported cell type" );
break;
}
}
}
} catch ( IOException ex ) {
ex.printStackTrace();
}
}
}
Thanks,
import java.lang.Object;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.poifs.files
import org.apache.poi.hssf.usermo
import org.apache.poi.hssf.usermo
import org.apache.poi.hssf.usermo
import org.apache.poi.hssf.usermo
/**
* A simple POI example of opening an Excel spreadsheet
* and writing its contents to the command line.
* @author Tony Sintes
*/
public class OpenExcel
{
public static void main( String [] args )
{
try
{
InputStream input = OpenExcel.class.getResourc
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
// Iterate over each row in the sheet
Iterator rows = sheet.rowIterator();
while( rows.hasNext() ) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println( "Row #" + row.getRowNum() );
// Iterate over each cell in the row and print out the cell's content
Iterator cells = row.cellIterator();
while( cells.hasNext() ) {
HSSFCell cell = (HSSFCell) cells.next();
System.out.println( "Cell #" + cell.getCellNum() );
switch ( cell.getCellType() ) {
case HSSFCell.CELL_TYPE_NUMERIC
System.out.println( cell.getNumericCellValue()
break;
case HSSFCell.CELL_TYPE_STRING:
System.out.println( cell.getStringCellValue() );
break;
default:
System.out.println( "unsuported cell type" );
break;
}
}
}
} catch ( IOException ex ) {
ex.printStackTrace();
}
}
}
Thanks,
One thing that I do not see in this code is a closing of the input stream -- please never leave streams open like this...
Might not be the issue, but let's get this cleared out first.
And because this sounded familiar:
http://forums.sun.com/thread.jspa?threadID=5321509
In short: make sure that your resource is loading properly - that's the error that usually shows up when the file cannot be found.
Might not be the issue, but let's get this cleared out first.
And because this sounded familiar:
http://forums.sun.com/thread.jspa?threadID=5321509
In short: make sure that your resource is loading properly - that's the error that usually shows up when the file cannot be found.
ASKER
I did add the line
InputStream input = new FileInputStream(c:\List.xl s );
but there is a compile error saying that "can not find symbol class FileInputStream"
I need help to check/guide me through whether I setup the POI correct or not, since I am even able to test any single line of codes using POI so far,
Thank you,
InputStream input = new FileInputStream(c:\List.xl
but there is a compile error saying that "can not find symbol class FileInputStream"
I need help to check/guide me through whether I setup the POI correct or not, since I am even able to test any single line of codes using POI so far,
Thank you,
ASKER
Please help:
1- How to use POI in JDK and JCreator?
2- There are multiple \lib in jdk. Pls instruct which is the one we shoule place POI files in?
Thank you,
1- How to use POI in JDK and JCreator?
2- There are multiple \lib in jdk. Pls instruct which is the one we shoule place POI files in?
Thank you,
>but there is a compile error saying that "can not find symbol class FileInputStream"
Just import it then.
The code looks ok, what is wrong is the way you get the excel file. Did you see the link I posted above?
Just import it then.
The code looks ok, what is wrong is the way you get the excel file. Did you see the link I posted above?
ASKER
I followed the link you posted and imported java.io.InputStream and used FileInputStream, but there is still a compile error indicating that "can not find symbol constructor FileInputStream (java.io.InpuStream).
Just want to know if my POI setting-up is right? and what else can I do to be able to start using POI for my program.
Pls help!
Just want to know if my POI setting-up is right? and what else can I do to be able to start using POI for my program.
Pls help!
The compilation error is a core Java one, not a POI one...
Again - are you sure that your file is read correctly? The link above is to a solution to the same problem you have now so either use the code from there or change your code accordingly. In any way - your POI looks fine, something else is happening -- and I suspect you simply cannot read the resource.
Again - are you sure that your file is read correctly? The link above is to a solution to the same problem you have now so either use the code from there or change your code accordingly. In any way - your POI looks fine, something else is happening -- and I suspect you simply cannot read the resource.
ASKER
The problem is that I can compile other programs without any issue, but when I import the org.apache.poi.poifs.files ystem.POIF SFileSyste m or org.apache.poi.hssf.usermo del.HSSFCe ll, the compile error occurs as following
Note: C:\JAVA\Project_Work\src\O penExcel.j ava uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Process completed.
honestly, I do not know how to recompile with -Xlint, do you have any idea? I greatly appreciate it.
Note: C:\JAVA\Project_Work\src\O
Note: Recompile with -Xlint:deprecation for details.
Process completed.
honestly, I do not know how to recompile with -Xlint, do you have any idea? I greatly appreciate it.
That's just a warning, the code should compile and work even with that note. The problem is elsewhere.
ASKER
The program does nothing, just out the following message when I run the program:
Exception in thread "main" java.lang.NullPointerExcep tion
at org.apache.poi.poifs.files ystem.POIF SFileSyste m.closeInp utStream(P OIFSFileSy stem.java: 196)
at org.apache.poi.poifs.files ystem.POIF SFileSyste m.<init>(P OIFSFileSy stem.java: 158)
at OpenExcel.main(OpenExcel.j ava:30)
Process completed.
Do you have any idea of where this err comes from? Is it probably from my JCreator or from my core Java setting? But I do not know how to fix it.
Exception in thread "main" java.lang.NullPointerExcep
at org.apache.poi.poifs.files
at org.apache.poi.poifs.files
at OpenExcel.main(OpenExcel.j
Process completed.
Do you have any idea of where this err comes from? Is it probably from my JCreator or from my core Java setting? But I do not know how to fix it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, thanks a lot.
It runs now when I place poi-3.6-20091214.jar and OpenExcel.java in the same directory.
Thank you very much for a long follow-up.
It is highly appreciated,
It runs now when I place poi-3.6-20091214.jar and OpenExcel.java in the same directory.
Thank you very much for a long follow-up.
It is highly appreciated,
So, if you have a class called POITest.java that contains code using POI, and the POI jars are in the lib folder, you compile it with
javac -cp .;lib/poi-3.6-20091214.jar
and you run it with
java -cp .;lib/poi-3.6-20091214.jar
For JCreator, see http://www.skylit.com/javamethods/faqs/jcreator4.html#jars