NoClassDefFoundError: UnsupportedFileFormatException while working with excel sheet using java -
i'm writing java program reads .xlsx file , give output in .csv format. code:
import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstreamwriter; import java.io.writer; import java.text.simpledateformat; import java.util.date; import java.util.iterator; import org.apache.poi.xssf.usermodel.xssfsheet; import org.apache.poi.xssf.usermodel.xssfworkbook; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.dateutil; import org.apache.poi.ss.usermodel.row; public class xlsxtocsv { public static void main(string[] args) { long starttime = system.currenttimemillis(); file inputfile = new file("c:\\inputfile.xlsx"); file outputfile = new file("c:\\outputfile.csv"); xlsx(inputfile, outputfile); long stoptime = system.currenttimemillis(); long elapsedtime = stoptime - starttime; system.out.println(elapsedtime); } private static void xlsx(file inputfile, file outputfile) { //for storing data csv files stringbuffer data = new stringbuffer(); try { writer w = new outputstreamwriter(new fileoutputstream(outputfile), "utf-8"); // workbook object xls file xssfworkbook workbook = new xssfworkbook(new fileinputstream(inputfile)); // first sheet workbook xssfsheet sheet = workbook.getsheetat(0); cell cell; row row; // iterate through each rows first sheet iterator<row> rowiterator = sheet.iterator(); while (rowiterator.hasnext()) { row = rowiterator.next(); // each row, iterate through each columns iterator<cell> celliterator = row.celliterator(); while (celliterator.hasnext()) { cell = celliterator.next(); switch (cell.getcelltype()) { case cell.cell_type_boolean: data.append(cell.getbooleancellvalue() + ","); break; case cell.cell_type_numeric: if(dateutil.iscelldateformatted(cell)) { date date = cell.getdatecellvalue(); system.out.println(date.tostring()); simpledateformat sdf = new simpledateformat("dd-mm-yyyy"); string d = sdf.format(date); system.out.println(d); data.append(d + ","); } else if(cell.getnumericcellvalue() == (int)cell.getnumericcellvalue()) data.append((int)cell.getnumericcellvalue() + ","); else if(cell.getnumericcellvalue() == (long)cell.getnumericcellvalue()) data.append((long)cell.getnumericcellvalue() + ","); else data.append(cell.getnumericcellvalue() + ","); break; case cell.cell_type_string: data.append((cell.getstringcellvalue()) + ","); break; case cell.cell_type_blank: data.append("" + ","); break; default: data.append(cell + ","); } //data.append('\n'); } data.append("\r\n"); } w.write(data.tostring()); w.close(); } catch(filenotfoundexception e) { e.printstacktrace(); } catch(ioexception e) { e.printstacktrace(); } }
}
however, i'm getting following error:
exception in thread "main" java.lang.noclassdeffounderror: org/apache/poi/unsupportedfileformatexception @ java.lang.classloader.defineclass1(native method) @ java.lang.classloader.defineclass(unknown source) @ java.security.secureclassloader.defineclass(unknown source) @ java.net.urlclassloader.defineclass(unknown source) @ java.net.urlclassloader.access$100(unknown source) @ java.net.urlclassloader$1.run(unknown source) @ java.net.urlclassloader$1.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.net.urlclassloader.findclass(unknown source) @ java.lang.classloader.loadclass(unknown source) @ sun.misc.launcher$appclassloader.loadclass(unknown source) @ java.lang.classloader.loadclass(unknown source) @ xlsxtocsv.xlsxtocsv.xlsx(xlsxtocsv.java:47) @ xlsxtocsv.xlsxtocsv.main(xlsxtocsv.java:28) caused by: java.lang.classnotfoundexception: org.apache.poi.unsupportedfileformatexception @ java.net.urlclassloader$1.run(unknown source) @ java.net.urlclassloader$1.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.net.urlclassloader.findclass(unknown source) @ java.lang.classloader.loadclass(unknown source) @ sun.misc.launcher$appclassloader.loadclass(unknown source) @ java.lang.classloader.loadclass(unknown source) ... 14 more
i have included following jars:
- dom4j-1.6.jar
- poi-3.9.jar
- poi-ooxml-3.11.jar
- poi-ooxml-schemas-3.8-20120326.jar
- xmlbeans-2.3.0.jar
i've checked file format .xlsx , directory don't understand problem is.
how can remove error?
this covered in apache poi faq page:
can mix poi jars different versions?
no. not supported.
all poi jars in use must come same version. combination such poi-3.11.jar , poi-ooxml-3.9.jar not supported, , fail work in unpredictable ways.
you list using poi-3.9.jar
, poi-ooxml-3.11.jar
different versions, , won't work.
you need ensure poi jars come same version. i'd suggest grab latest poi release download page (3.12 of writing), , use consistent set of jars that
Comments
Post a Comment