2015-05-12 32 views
1

我正在寫一個Java程序,它從.xlsx文件讀取並以.csv格式輸出。這是我的代碼:NoClassDefFoundError:UnsupportedFileFormatException使用java的Excel工作表使用

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 into CSV files 
     StringBuffer data = new StringBuffer(); 

     try { 

      Writer w = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"); 

      // Get the workbook object for XLS file 
      XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(inputFile)); 

      // Get first sheet from the workbook 
      XSSFSheet sheet = workbook.getSheetAt(0); 
      Cell cell; 
      Row row; 

      // Iterate through each rows from first sheet 
      Iterator<Row> rowIterator = sheet.iterator(); 
      while (rowIterator.hasNext()) 
      { 
       row = rowIterator.next(); 
       // For 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(); 
    } 
} 

}

不過,我發現了以下錯誤:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException 
at java.lang.ClassLoader.defineClass1(Native Method) 
at java.lang.ClassLoader.defineClass(Unknown Source) 
at java.security.SecureClassLoader.defineClass(Unknown Source) 
at java.net.URLClassLoader.defineClass(Unknown Source) 
at java.net.URLClassLoader.access$100(Unknown Source) 
at java.net.URLClassLoader$1.run(Unknown Source) 
at java.net.URLClassLoader$1.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at xlsxToCsv.xlsxToCsv.xlsx(xlsxToCsv.java:47) 
at xlsxToCsv.xlsxToCsv.main(xlsxToCsv.java:28) 
Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException 
at java.net.URLClassLoader$1.run(Unknown Source) 
at java.net.URLClassLoader$1.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
... 14 more 

我已經包括以下罐子:

  • 的dom4j-1.6 .jar
  • poi-3.9.jar
  • POI-OOXML-3.11.jar
  • POI-OOXML-架構 - 3.8-20120326.jar
  • 的xmlbeans-2.3.0.jar

我檢查文件格式的.xlsx,也該目錄,但我不明白是什麼問題。

我該如何刪除此錯誤?

+0

你得到的錯誤? –

+0

XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(inputFile)); –

+2

你所有的poi版本都不一樣。添加所有來自相同版本的poi罐,即3.9。所有的jar文件都與apache poi一起提供給你的項目的類路徑。 – LittlePanda

回答

9

這是包括在Apache POI FAQ page

Can I mix POI jars from different versions?

No. This is not supported.

All POI jars in use must come from the same version. A combination such as poi-3.11.jar and poi-ooxml-3.9.jar is not supported, and will fail to work in unpredictable ways.

你列出自己是使用poi-3.9.jarpoi-ooxml-3.11.jar它們是從不同的版本,並且將無法正常工作。

您需要確保所有POI罐子來自同一版本。我建議你抓住latest POI release from the download page(截止到撰寫時的3.12),並使用一組一致的罐子,從這

+0

感謝您的幫助。 –

-1

當JVM在運行時無法找到庫時,會出現NoClassDefFoundError。可能是你在文件路徑中添加了庫,但它在運行時不可用。

0

我試着相同,並得到相同的錯誤。這是因爲poi罐子被複制到pom.xml文件中。

我得到了這些我首先忽略的警告。後來我嘗試刪除pom.xml中的重複poi罐子,這解決了問題。

這是我之前得到了警告:

其中線

[WARNING] Some problems were encountered while building the effective model for MavenProj:Perfumania:jar:0.0.1-SNAPSHOT [WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.apache.poi:poi:jar -> duplicate declaration of version 3.9 @ line 36,

相關問題