2015-10-26 4 views
-1

Я получаю сообщение об ошибке:Как импортировать первенствует файл с более 100k строк в базу данных 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 .... или разделить первенствует в 4-х частях – koutuk

+0

Пробовали ли вы делать именно то, что сообщение об ошибке сказал вам делать, и переключил ваш код «HSSF» (.xls) для кода «XSSF» (.xlsx)? – Gagravarr

+0

@Gagravarr да, я переработал раньше, если я конвертирую файл xls, потребуется не более 65 тыс. Строк, что мне нужно импортировать более 1 строки lakh –

ответ

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 heap space –

+0

@bala Так что увеличьте размер своего кучного пространства! См. Один из сотен вопросов здесь о том, как это сделать ... – Gagravarr