2013-11-02 17 views
27

sto ottenendo seguente errorejava.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject errore

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)

ho cercato le ragioni di questo errore in linea, ma non riuscivo a trovare il motivo per cui sto ottenendo esso.

ho incluso i seguenti file jar

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 

Codice:

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(); 
     } 
    } 
}  

risposta

60

Bisogna includere un altro vaso.

xmlbeans-2.3.0.jar 

Aggiungere questo e provare.

Nota: È necessario per i file con solo formati .xlsx, non solo per i formati .xls.

+1

dove lo trovo? – user2942227

+1

@ user2942227 è possibile scaricarlo da [qui] (http://www.java2s.com/Code/Jar/x/Downloadxmlbeans230jar.htm) – Jhanvi

+0

Ora ha generato questo errore, Eccezione nel thread "main" java.lang.NoClassDefFoundError : org/dom4j/DocumentExceptionCaused di: java.lang.ClassNotFoundException: org.dom4j.DocumentException, sai perché? – user2942227

1

Quando si tenta di tradurre il file Excel con il suffisso .xlsx, è necessario aggiungere altro jar, xmlbeansxxx.jar. xxxx è la versione, come ad esempio xmlbeans-2.3.0.jar

+0

Ha corretto quell'errore. Ora, ha gettato Ora ha gettato questo errore, Eccezione nel thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentExceptionCaused di: java.lang.ClassNotFoundException: org.dom4j.DocumentException, sai perché? – user2942227

+0

È necessario aggiungere anche dom4j.jar. Poiché alcuni file XML in POI utilizzano dom4j per gestire file XML. – MouseLearnJava

+0

Grazie per l'aiuto. Ha funzionato. – user2942227

0

Per tutto ciò che si aggiunge XMLBeans-2.3.0.jar e non funziona, è necessario utilizzare HSSFWorkbook invece di XSSFWorkbook dopo aggiungere esempio jar.For;

Workbook workbook = new HSSFWorkbook(); 
    Sheet listSheet = workbook.createSheet("Kişi Listesi"); 

    int rowIndex = 0; 
    for (KayitParam kp : kayitList) { 
     Row row = listSheet.createRow(rowIndex++); 
     int cellIndex = 0; 
     row.createCell(cellIndex++).setCellValue(kp.getAd()); 
     row.createCell(cellIndex++).setCellValue(kp.getSoyad()); 
     row.createCell(cellIndex++).setCellValue(kp.getEposta()); 
     row.createCell(cellIndex++).setCellValue(kp.getCinsiyet()); 
     row.createCell(cellIndex++).setCellValue(kp.getDogumtarihi()); 
     row.createCell(cellIndex++).setCellValue(kp.getTahsil()); 
    } 

    try { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     workbook.write(baos); 
     AMedia amedia = new AMedia("Kisiler.xls", "xls", 
       "application/file", baos.toByteArray()); 
     Filedownload.save(amedia); 
     baos.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
0

è necessario includere XMLBeans-xxx.jar e se avete scaricato lo zip binario POI, si otterrà il XMLBeans-xxx.jar nella cartella lib-OOXML (ad esempio: \ poi-3.11 \ ooxml- lib)

Questo jar viene utilizzato per il binding XML che è applicabile per i file .xlsx.

2

devi includere altri due file jar.

xmlbeans-2.3.0.jar e dom4j-1.6.1.jar Aggiungi prova funzionerà.

Nota: è necessario per i file con i formati .xlsx, non solo per i formati .xlt.

Problemi correlati