You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by milan777 <mi...@gmail.com> on 2013/06/06 08:01:37 UTC
Getting error while reading .xlsx file in using poi
Please help me i am getting error like this.......
Exception in thread "main" org.apache.poi.POIXMLException:
java.lang.reflect.InvocationTargetException
at
org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:207)
at Excel.Test.main(Test.java:28)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at
org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
... 4 more
Caused by: java.lang.ExceptionInInitializerError
at
org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown
Source)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:44)
... 9 more
Caused by: java.lang.IllegalStateException: Cannot load build: verify that
xbean.jar is on the classpath
at org.apache.xmlbeans.XmlBeans.buildMethod(XmlBeans.java:181)
at
org.apache.xmlbeans.XmlBeans.buildTypeLoaderBuilderMethod(XmlBeans.java:216)
at org.apache.xmlbeans.XmlBeans.<clinit>(XmlBeans.java:129)
... 11 more
Caused by: java.lang.NoSuchMethodException:
org.apache.xmlbeans.impl.schema.SchemaTypeLoaderImpl.build([Lorg.apache.xmlbeans.SchemaTypeLoader;,
org.apache.xmlbeans.ResourceLoader, java.lang.ClassLoader)
at java.lang.Class.getMethod(Unknown Source)
at org.apache.xmlbeans.XmlBeans.buildMethod(XmlBeans.java:174)
... 13 more
my code is.....
package Excel;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
public static void main(String[] args) throws IOException {
String fname = "D:\\test.xlsx"; // or "C:\\Test.xls" C:\\SDI-XL.xls
InputStream inp = new FileInputStream(fname);
String fileExtn = GetFileExtension(fname);
Workbook wb_xssf; //Declare XSSF WorkBook
Workbook wb_hssf; //Declare HSSF WorkBook
Sheet sheet = null; // sheet can be used as common for XSSF and HSSF
WorkBook
if (fileExtn.equalsIgnoreCase("xlsx"))
{
wb_xssf = new XSSFWorkbook(inp);
log("xlsx="+wb_xssf.getSheetName(0));
sheet = wb_xssf.getSheetAt(0);
}
if (fileExtn.equalsIgnoreCase("xls"))
{
POIFSFileSystem fs = new POIFSFileSystem(inp);
wb_hssf = new HSSFWorkbook(fs);
log("xls="+wb_hssf.getSheetName(0));
sheet = wb_hssf.getSheetAt(0);
}
Iterator rows = sheet.rowIterator(); // Now we have rows ready from the
sheet
while (rows.hasNext())
{
Row row = (Row) rows.next();
log("row#="+row.getRowNum()+"");
log("**********************");
//log(row.getPhysicalNumberOfCells()+"");
Iterator cells = row.cellIterator();
while (cells.hasNext())
{
Cell cell = (Cell) cells.next();
switch ( cell.getCellType() )
{
case Cell.CELL_TYPE_STRING:
log(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
log(cell.getDateCellValue()+"");
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
log(cell.getBooleanCellValue()+"");
break;
case Cell.CELL_TYPE_FORMULA:
log(cell.getCellFormula());
break;
default:
}
}
}
inp.close();
}
private static void log(String message)
{
System.out.println(message);
}
private static String GetFileExtension(String fname2)
{
String fileName = fname2;
String fname="";
String ext="";
int mid= fileName.lastIndexOf(".");
fname=fileName.substring(0,mid);
ext=fileName.substring(mid+1,fileName.length());
return ext;
}
}
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Getting-error-while-reading-xlsx-file-in-using-poi-tp5712947.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: Getting error while reading .xlsx file in using poi
Posted by Lo...@log-net.com.
Check your JARs:
"Caused by: java.lang.IllegalStateException: Cannot load build: verify
that xbean.jar is on the classpath"
I believe you need to include the xml beans JAR.
milan777 <mi...@gmail.com> wrote on 2013-06-06 02:01:37 AM:
> From: milan777 <mi...@gmail.com>
> To: user@poi.apache.org,
> Date: 2013-06-06 05:52 AM
> Subject: Getting error while reading .xlsx file in using poi
>
> Please help me i am getting error like this.......
>
> Exception in thread "main" org.apache.poi.POIXMLException:
> java.lang.reflect.InvocationTargetException
> at
> org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart
> (XSSFFactory.java:62)
> at
org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403)
> at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)
> at
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:207)
> at Excel.Test.main(Test.java:28)
> Caused by: java.lang.reflect.InvocationTargetException
> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
> at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown
Source)
> at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
> Source)
> at java.lang.reflect.Constructor.newInstance(Unknown Source)
> at
> org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart
> (XSSFFactory.java:60)
> ... 4 more
> Caused by: java.lang.ExceptionInInitializerError
> at
> org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument
> $Factory.parse(Unknown
> Source)
> at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:44)
> ... 9 more
> Caused by: java.lang.IllegalStateException: Cannot load build: verify
that
> xbean.jar is on the classpath
> at org.apache.xmlbeans.XmlBeans.buildMethod(XmlBeans.java:181)
> at
>
org.apache.xmlbeans.XmlBeans.buildTypeLoaderBuilderMethod(XmlBeans.java:216)
> at org.apache.xmlbeans.XmlBeans.<clinit>(XmlBeans.java:129)
> ... 11 more
> Caused by: java.lang.NoSuchMethodException:
> org.apache.xmlbeans.impl.schema.SchemaTypeLoaderImpl.build
> ([Lorg.apache.xmlbeans.SchemaTypeLoader;,
> org.apache.xmlbeans.ResourceLoader, java.lang.ClassLoader)
> at java.lang.Class.getMethod(Unknown Source)
> at org.apache.xmlbeans.XmlBeans.buildMethod(XmlBeans.java:174)
> ... 13 more
>
>
>
>
> my code is.....
>
> package Excel;
>
>
> import java.io.FileInputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.Iterator;
>
> import org.apache.poi.hssf.usermodel.HSSFWorkbook;
> import org.apache.poi.poifs.filesystem.POIFSFileSystem;
> import org.apache.poi.ss.usermodel.Cell;
> import org.apache.poi.ss.usermodel.DateUtil;
> import org.apache.poi.ss.usermodel.Row;
> import org.apache.poi.ss.usermodel.Sheet;
> import org.apache.poi.ss.usermodel.Workbook;
> import org.apache.poi.xssf.usermodel.XSSFWorkbook;
>
> public class Test {
> public static void main(String[] args) throws IOException {
> String fname = "D:\\test.xlsx"; // or "C:\\Test.xls" C:\\SDI-XL.xls
> InputStream inp = new FileInputStream(fname);
> String fileExtn = GetFileExtension(fname);
> Workbook wb_xssf; //Declare XSSF WorkBook
> Workbook wb_hssf; //Declare HSSF WorkBook
> Sheet sheet = null; // sheet can be used as common for XSSF and HSSF
> WorkBook
> if (fileExtn.equalsIgnoreCase("xlsx"))
> {
> wb_xssf = new XSSFWorkbook(inp);
> log("xlsx="+wb_xssf.getSheetName(0));
> sheet = wb_xssf.getSheetAt(0);
> }
> if (fileExtn.equalsIgnoreCase("xls"))
> {
> POIFSFileSystem fs = new POIFSFileSystem(inp);
> wb_hssf = new HSSFWorkbook(fs);
> log("xls="+wb_hssf.getSheetName(0));
> sheet = wb_hssf.getSheetAt(0);
> }
> Iterator rows = sheet.rowIterator(); // Now we have rows ready from the
> sheet
> while (rows.hasNext())
> {
> Row row = (Row) rows.next();
> log("row#="+row.getRowNum()+"");
> log("**********************");
> //log(row.getPhysicalNumberOfCells()+"");
> Iterator cells = row.cellIterator();
> while (cells.hasNext())
> {
> Cell cell = (Cell) cells.next();
>
> switch ( cell.getCellType() )
> {
> case Cell.CELL_TYPE_STRING:
> log(cell.getRichStringCellValue().getString());
> break;
> case Cell.CELL_TYPE_NUMERIC:
> if(DateUtil.isCellDateFormatted(cell)) {
> log(cell.getDateCellValue()+"");
> } else {
>
> System.out.println(cell.getNumericCellValue());
> }
> break;
> case Cell.CELL_TYPE_BOOLEAN:
> log(cell.getBooleanCellValue()+"");
> break;
> case Cell.CELL_TYPE_FORMULA:
> log(cell.getCellFormula());
> break;
> default:
> }
> }
> }
> inp.close();
> }
>
> private static void log(String message)
> {
> System.out.println(message);
> }
> private static String GetFileExtension(String fname2)
> {
> String fileName = fname2;
> String fname="";
> String ext="";
> int mid= fileName.lastIndexOf(".");
> fname=fileName.substring(0,mid);
> ext=fileName.substring(mid+1,fileName.length());
> return ext;
> }
> }
>
>
>
> --
> View this message in context: http://apache-poi.
> 1045710.n5.nabble.com/Getting-error-while-reading-xlsx-file-in-
> using-poi-tp5712947.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
Please let me help to solve the error........
Posted by milan777 <mi...@gmail.com>.
import java.io.IOException;
import jxl.CellType;
import jxl.Cell;
import jxl.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateNewWorkBook {
public static void main(String args[]) throws IOException
{
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = (Sheet) wb.getSheet("d:/test.xlsx");
try{
for (int i = 0; i < sheet.getRows(); i++) {
System.out.println("");
for (int j = 0; j < sheet.getColumns(); j++) {
Cell cell = sheet.getCell(j, i);
CellType type = cell.getType();
if (type == CellType.LABEL) {
System.out.print("\t lable "
+ cell.getContents().trim());
}
if (type == CellType.NUMBER) {
System.out.print("\t number " +
cell.getContents().trim()); }
}
}
} catch(Exception e){
e.printStackTrace();
}
}
}
getting error like nullpointer exception :( :(
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Getting-error-while-reading-xlsx-file-in-using-poi-tp5712947p5712948.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org