redds1
asked on
How to use POI with Excel 2007 in core java?
My code works fine with Excel 2003.
fs = new POIFSFileSystem(excelFile) ;
wb = new HSSFWorkbook(fs);
String cleanSheetName;
int numsheets = wb.getNumberOfSheets();
boolean proceed;
sheet = wb.getSheetAt("testr");
Iterator iter = sheet.rowIterator();
while(iter.hasNext()){
proceed = true;
HSSFRow row = (HSSFRow)iter.next();
try{
boolean cell1,cell2;
cell1 = cell2 = true;
proceed = cell1 || cell2;
}catch(Exception ex){
proceed = false;
}
if(proceed){
StringBuffer sb = new StringBuffer();
for(int c=0;c< 1;c++){
HSSFCell cell = row.getCell((short)c);
try{
String tp = str.cleanString(cell.getSt ringCellVa lue());
sb.append(tp);
sb.append(",");
}catch(NullPointerExceptio n ex){
}catch(Exception ex){
sb.append(myFormatter.form at(cell.ge tNumericCe llValue()) );
sb.append(",");
//System.out.println("Nume ric cell "+cleanSheetName+" row: "+(row.getRowNum()+1)+" col: "+c+" value: "+cell.getNumericCellValue ());
}
}
writer.write(sb.append("\n ").toStrin g());
}
}
But I want to read an xlsx file. My POI version is 3.7, it seems to have support for xssf but I can't get access to the xssf api: import org.apache.poi.xssf. Can you help please?
fs = new POIFSFileSystem(excelFile)
wb = new HSSFWorkbook(fs);
String cleanSheetName;
int numsheets = wb.getNumberOfSheets();
boolean proceed;
sheet = wb.getSheetAt("testr");
Iterator iter = sheet.rowIterator();
while(iter.hasNext()){
proceed = true;
HSSFRow row = (HSSFRow)iter.next();
try{
boolean cell1,cell2;
cell1 = cell2 = true;
proceed = cell1 || cell2;
}catch(Exception ex){
proceed = false;
}
if(proceed){
StringBuffer sb = new StringBuffer();
for(int c=0;c< 1;c++){
HSSFCell cell = row.getCell((short)c);
try{
String tp = str.cleanString(cell.getSt
sb.append(tp);
sb.append(",");
}catch(NullPointerExceptio
}catch(Exception ex){
sb.append(myFormatter.form
sb.append(",");
//System.out.println("Nume
}
}
writer.write(sb.append("\n
}
}
But I want to read an xlsx file. My POI version is 3.7, it seems to have support for xssf but I can't get access to the xssf api: import org.apache.poi.xssf. Can you help please?
ASKER
I had 3.6 and I changed it to 3.7. All these versions supposedly support xssf but I can't access it.
You're saying you still can't compile when you import that package org.apache.poi.xssf ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks, I can compile now after adding this jar to my classpath.Still, it does not work, can you take a look at the code, thanks
public TsExcelImportPtbi(InputStr eam excelFile){
this.excelFile = excelFile;
}
public void write(OutputStream out) throws IOException{
OutputStreamWriter writer = new OutputStreamWriter(out);
POIFSFileSystem fs = null;
XSSFWorkbook wb = null;
XSSFSheet sheet = null;
DecimalFormat myFormatter = new DecimalFormat("0.######### ");
fs = new POIFSFileSystem(excelFile) ;
wb = new XSSFWorkbook(excelFile);
String cleanSheetName;
int numsheets = wb.getNumberOfSheets();
boolean proceed;
sheet = wb.getSheetAt(0);
Iterator iter = sheet.rowIterator();
while(iter.hasNext()){
proceed = true;
XSSFRow row = (XSSFRow)iter.next();
try{
boolean cell1,cell2;
cell1 = cell2 = true;
proceed = cell1 || cell2;
}catch(Exception ex){
proceed = false;
}
if(proceed){
StringBuffer sb = new StringBuffer();
for(int c=0;c< 3;c++){
XSSFCell cell = row.getCell((short)c);
try{
String tp = str.cleanString(cell.getSt ringCellVa lue());
sb.append(tp);
sb.append(",");
}catch(NullPointerExceptio n ex){
}catch(Exception ex){
sb.append(myFormatter.form at(cell.ge tNumericCe llValue()) );
sb.append(",");
}
}
writer.write(sb.append("\n ").toStrin g());
}
// }
}
writer.flush();
writer.close();
}
public TsExcelImportPtbi(InputStr
this.excelFile = excelFile;
}
public void write(OutputStream out) throws IOException{
OutputStreamWriter writer = new OutputStreamWriter(out);
POIFSFileSystem fs = null;
XSSFWorkbook wb = null;
XSSFSheet sheet = null;
DecimalFormat myFormatter = new DecimalFormat("0.#########
fs = new POIFSFileSystem(excelFile)
wb = new XSSFWorkbook(excelFile);
String cleanSheetName;
int numsheets = wb.getNumberOfSheets();
boolean proceed;
sheet = wb.getSheetAt(0);
Iterator iter = sheet.rowIterator();
while(iter.hasNext()){
proceed = true;
XSSFRow row = (XSSFRow)iter.next();
try{
boolean cell1,cell2;
cell1 = cell2 = true;
proceed = cell1 || cell2;
}catch(Exception ex){
proceed = false;
}
if(proceed){
StringBuffer sb = new StringBuffer();
for(int c=0;c< 3;c++){
XSSFCell cell = row.getCell((short)c);
try{
String tp = str.cleanString(cell.getSt
sb.append(tp);
sb.append(",");
}catch(NullPointerExceptio
}catch(Exception ex){
sb.append(myFormatter.form
sb.append(",");
}
}
writer.write(sb.append("\n
}
// }
}
writer.flush();
writer.close();
}
That's another issue, but see http://technojeeves.com/joomla/index.php/free/116-doesntwork
ASKER
I am getting: java.lang.NoClassDefFoundE rror
on this line: wb = new XSSFWorkbook(excelFile);
on this line: wb = new XSSFWorkbook(excelFile);
ASKER
I have added following jars 1)poi-3.6-20091214.jar 2)poi-contrib-3.6-20091214 .jar 3)poi-examples-3.6-2009121 4.jar 4)poi-ooxml-3.6-20091214.j ar 5)poi-ooxml-schemas-3.6-20 091214.jar 6)poi-scratchpad-3.6-20091 214.jar, still it's giving java.lang.NoClassDefFoundE rror when I try to import the file
ASKER
it works now, few pre-requisite jar files were missing
You've probably got a few unnecessary ones now in the classpath - try removing them one by one
In that case you have the wrong version in your classpath. Make sure you get rid of all versions that don't support 2007