2015-10-26 27 views
-1

我收到錯誤出類拔萃有超過10萬行的文件到MySQL數據庫:如何導入用java

Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF) at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:128) at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:112) at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.(NPOIFSFileSystem.java:300) at org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:86) at importdata.Snomed10.main(Snomed10.java:28)

我的代碼:

package importdata; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.Locale; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.poifs.filesystem.POIFSFileSystem; 
import org.apache.poi.ss.usermodel.DataFormatter; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import com.mysql.jdbc.Connection; 
import com.mysql.jdbc.PreparedStatement; 
public class Snomed10 { 
    public static void main(String[] args) { 
     try{ 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/snomedinfo","root","root"); 
      con.setAutoCommit(false); 
      PreparedStatement pstm = null ; 
      FileInputStream input = new FileInputStream("D://icd10 to snomed.xlsx"); 
      POIFSFileSystem fs = new POIFSFileSystem(input); 
      //HSSFWorkbook wb = new HSSFWorkbook(fs); 
      //HSSFSheet sheet = wb.getSheetAt(0); 
      XSSFWorkbook wb = new XSSFWorkbook(input); 
      XSSFSheet sheet = wb.getSheetAt(0); 

      Row row; 
      DataFormatter formatter = new DataFormatter(Locale.US); 
      for(int i=1; i<=sheet.getLastRowNum(); i++){ 
       row = sheet.getRow(i); 



       String id = formatter.formatCellValue(row.getCell(0)); 
       int effectiveTime = (int) row.getCell(1).getNumericCellValue(); 
       int active = (int) row.getCell(2).getNumericCellValue(); 
       String moduleId = formatter.formatCellValue(row.getCell(3)); 
       String refSetId = formatter.formatCellValue(row.getCell(4)); 
       String referencedComponentId = formatter.formatCellValue(row.getCell(5)); 
       String sctName = formatter.formatCellValue(row.getCell(6)); 
       int mapGroup = (int) row.getCell(7).getNumericCellValue(); 
       int mapPriority = (int) row.getCell(8).getNumericCellValue(); 
       String mapRule = formatter.formatCellValue(row.getCell(9)); 
       String mapAdvice = formatter.formatCellValue(row.getCell(10)); 
       String mapTarget =formatter.formatCellValue(row.getCell(11)); 
       String icdName = formatter.formatCellValue(row.getCell(12)); 
       int mmapCategoryId = (int) row.getCell(13).getNumericCellValue(); 
       String mapCategoryValue = formatter.formatCellValue(row.getCell(14)); 

       //String sql = "INSERT INTO sno10 VALUES('"+id+"','"+effectiveTime+"','"+active+"'," 
         //+ "'"+moduleId+"','"+refSetId+"','"+referencedComponentId+"'," 
           //+ "'"+sctName +"','"+mapGroup+"','"+mapPriority+"'," 
             //+ "'"+mapRule+"','"+mapAdvice+"','"+mapTarget+"'," 
              // + "'"+icdName+"','"+mmapCategoryId+"','"+mapCategoryValue+"')"; 
       //pstm = (PreparedStatement) con.prepareStatement(sql); 
       // pstm.execute(); 

       String sql = "INSERT INTO sno10 VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
       pstm = (PreparedStatement) con.prepareStatement(sql); 

       pstm.setString(1, id); 
       pstm.setInt(2, effectiveTime); 
       pstm.setInt(3, active); 
       pstm.setString(4, moduleId); 
       pstm.setString(5, refSetId); 
       pstm.setString(6, referencedComponentId); 
       pstm.setString(7, sctName); 
       pstm.setInt(8, mapGroup); 
       pstm.setInt(9, mapPriority); 
       pstm.setString(10, mapRule); 
       pstm.setString(11, mapAdvice); 
       pstm.setString(12, mapTarget); 
       pstm.setString(13, icdName); 
       pstm.setInt(14, mmapCategoryId); 
       pstm.setString(15,mapCategoryValue); 
       pstm.execute(); 



       System.out.println("Import rows "+i); 
      } 
      con.commit(); 
      pstm.close(); 
      con.close(); 
      input.close(); 
      System.out.println("Success import excel to mysql table"); 
     }catch(ClassNotFoundException e){ 
      System.out.println(e); 
     }catch(SQLException ex){ 
      System.out.println(ex); 
     }catch(IOException ioe){ 
      System.out.println(ioe); 
     } 
    } 
} 
+0

隨時隨地SAX解析器....或者將您的Excel在4個部分 – koutuk

+0

你嘗試做什麼錯誤消息告訴你這樣做,併爲'XSSF'代碼(.xlsx)切換'HSSF'代碼(.xls)? – Gagravarr

+0

@Gagravarr是的,我轉換之前,如果我轉換xls文件,它將只需要65 k行沒有更多,我需要導入超過1萬行 –

回答

1

如果我們看一下這個片段在這裏:

FileInputStream input = new FileInputStream("D://icd10 to snomed.xlsx"); 
POIFSFileSystem fs = new POIFSFileSystem(input); 
//HSSFWorkbook wb = new HSSFWorkbook(fs); 
XSSFWorkbook wb = new XSSFWorkbook(input); 

我們看到了問題。那麼,兩個問題,但...

因爲explained in the JavaDocs, POIFSFileSystem是讀取OLE2文件,如.xls的低級代碼。它不能打開OOXML文件,如.xlsx,這正是異常告訴你的!

其次,如explained in the docs,如果您有文件,請勿使用流!

所以,你的代碼應該改爲類似:

File input = new File("D://icd10 to snomed.xlsx"); 
OPCPackage pkg = OPCPackage.open(input); 
XSSFWorkbook wb = new XSSFWorkbook(pkg); 
+0

它需要更多的時間運行至少45秒,並顯示此錯誤@Gagravarr –

+0

線程「main」中的異常java.lang.OutOfMemoryError:Java堆空間 –

+0

@bala因此增加堆空間的大小!請參閱數百個問題之一,以瞭解如何做到這一點... – Gagravarr