Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject Error

Tags:

java

I am getting following error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject at OrderBook.WriteToExcelSheet.CreateOutPutFile(WriteToExcelSheet.java:20) at OrderBook.MainMethod.main(MainMethod.java:71)

I looked for the reasons for this error online but couldn't find why I am getting it.

I have included the following jar files

poi-3.9-20121203.jar, 
poi-excelant-3.9-20121203.jar,
poi-examples-3.9-20121203.jar,
poi-ooxml-3.9-20121203.jar,
poi-ooxml-schemas-3.9-20121203.jar,
poi-scratchpad-3.9-20121203.jar

Code:

public class WriteToExcelSheet {
    public static Map < Integer, Object[] > data = new TreeMap < Integer, Object[] > ();
    public static void CreateOutPutFile() {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Orderbook Stats");
        //This data needs to be written (Object[])
        //Iterate over data and write to sheet
        Set < Integer > keyset = data.keySet()
        int rownum = 0;
        for (Integer key: keyset) {
            Row row = sheet.createRow(rownum++);
            Object[] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj: objArr) {
                Cell cell = row.createCell(cellnum++);
                if (obj instanceof String) cell.setCellValue((String) obj);
                else if (obj instanceof Integer) cell.setCellValue((Integer) obj);
            }
        }
        try {
            //Write the workbook in file system
            System.out.println("OutPutStats.xlsx writing..............................");
            FileOutputStream out = new FileOutputStream(new File("FileLocation/o.xlxs"));
            workbook.write(out);
            out.close();
            System.out.println("OutPutStats.xlsx written successfully on disk.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}    
like image 548
user2942227 Avatar asked Nov 02 '13 04:11

user2942227


3 Answers

You have to include one more jar.

xmlbeans-2.3.0.jar

Add this and try.

Note: It is required for the files with .xlsx formats only, not for just .xls formats.

like image 194
Jhanvi Avatar answered Oct 22 '22 00:10

Jhanvi


you have to include two more jar files.

xmlbeans-2.3.0.jar and dom4j-1.6.1.jar Add try it will work.

Note: It is required for the files with .xlsx formats only, not for just .xlt formats.

like image 4
Arslan Ahmad Avatar answered Oct 22 '22 01:10

Arslan Ahmad


When trying to translate Excel file with .xlsx suffix, you need to add additional jar, xmlbeansxxx.jar. xxxx is version, such as xmlbeans-2.3.0.jar

like image 2
MouseLearnJava Avatar answered Oct 22 '22 02:10

MouseLearnJava