-1

I am getting error:

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)

My code:

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);
        }
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • go for Sax parser .... or divide your excel in 4 parts – koutuk Oct 26 '15 at 11:50
  • Did you try doing exactly what the Error message told you to do, and switched your `HSSF` code (.xls) for `XSSF` code (.xlsx)? – Gagravarr Oct 26 '15 at 12:55
  • @Gagravarr yes i converted before , if i convert xls file it will take only 65 k rows not more done that i need to import more than 1 lakh rows – new learner Oct 26 '15 at 13:36

1 Answers1

1

If we look at this snippet here:

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

We see the problem. Well, two problems, but...

As explained in the JavaDocs, POIFSFileSystem is the low level code for reading OLE2 files such as .xls. It can't open OOXML files like .xlsx, which is exactly what the exception tells you!

Secondly, as explained in the docs, don't use a stream if you have a file!

So, your code should instead be something like:

File input = new File("D://icd10 to snomed.xlsx");
OPCPackage pkg = OPCPackage.open(input);
XSSFWorkbook wb = new XSSFWorkbook(pkg);
Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • its taking more time to run at least 45 sec and showing this error @Gagravarr – new learner Nov 02 '15 at 16:13
  • Exception in thread "main" java.lang.OutOfMemoryError: Java heap space – new learner Nov 02 '15 at 18:19
  • @bala So increase the size of your heap space! See one of the hundreds of questions here on how to do that... – Gagravarr Nov 02 '15 at 18:37
  • i have increased the size of 1024 m even though its showing like that , if u have similar code to upload above 1 lakh excel rows in mysql DB cloud you share it to me , its very helpful for me , @Gagravarr – new learner Nov 02 '15 at 20:22
  • Apache POI works just fine for me, even on large files! Did you try [following the advice in the Apache POI FAQ on memory and performance](http://poi.apache.org/faq.html#faq-N10109)? – Gagravarr Nov 02 '15 at 23:35
  • i saw that link , but if you share your source code means it will help full to me .@Gagravarr – new learner Nov 03 '15 at 16:41