Search code examples
javaspringmavenapache-poiimport-from-excel

cannot import org.apache.poi in spring so not able to compile


i am trying to import excel data in my desktop to mysql database but initially i am not able to parse excel file as i am not able to import org.apache.poi i have given my pom file also, in that i have included org.apache.poi dependency but still not able to import it because of it i am not able to get POIFSFileSystem,HSSFWorkbook,etc to use

my java code:

    public static void main( String[] args )
        {

            FileInputStream input = new FileInputStream("/Users/Desktop/file.xlsx");  
            POIFSFileSystem fs = new POIFSFileSystem( input );  
            HSSFWorkbook wb = new HSSFWorkbook(fs);  
            HSSFSheet sheet = wb.getSheetAt(0);  

            HSSFRow row;  



            ApplicationContext appContext = 
                      new ClassPathXmlApplicationContext("resources/spring/config/BeanLocations.xml");

                    StockBo stockBo = (StockBo)appContext.getBean("stockBo");

                    int row1 = 1;
                    /** insert **/
                    Stock stock = new Stock();
 for(int i=1; i<=sheet.getLastRowNum(); i++)
                {  
                    Employee employee=new Employee();
                    row = sheet.getRow(i);                      
stock.setRow(row1);
                    stock.setSource(String.valueOf(row.getCell(0).getRichStringCellValue()));
                    stock.setDestination(String.valueOf(row.getCell(1).getRichStringCellValue()));
                    stock.setProtocol(String.valueOf(row.getCell(2).getRichStringCellValue()));
                    stock.setPort("");
                    stockBo.save(stock);
}
            System.out.println( "Hello World!" );
        }

pom.xml:

< project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>

      <groupId>xl</groupId>
      <artifactId>xl</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>

      <name>xl</name>
      <url>http://maven.apache.org</url>

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>

      <dependencies>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
          </dependency>

          <!-- Spring framework -->
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring</artifactId>
              <version>2.5.6</version>
          </dependency>

          <!-- Spring AOP dependency -->
          <dependency>
              <groupId>cglib</groupId>
              <artifactId>cglib</artifactId>
              <version>2.2</version>
          </dependency>

          <!-- MySQL database driver -->
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>5.1.9</version>
          </dependency>

          <!-- Hibernate framework -->
          <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate</artifactId>
              <version>3.2.3.ga</version>
          </dependency>     

          <!-- Hibernate library dependecy start -->
          <dependency>
              <groupId>dom4j</groupId>
              <artifactId>dom4j</artifactId>
              <version>1.6.1</version>
          </dependency>

          <dependency>
              <groupId>commons-logging</groupId>
              <artifactId>commons-logging</artifactId>
              <version>1.1.1</version>
          </dependency>

          <dependency>
              <groupId>commons-collections</groupId>
              <artifactId>commons-collections</artifactId>
              <version>3.2.1</version>
          </dependency>

          <dependency>
              <groupId>antlr</groupId>
              <artifactId>antlr</artifactId>
              <version>2.7.7</version>
          </dependency>

          <!-- Hibernate library dependecy end -->
          <dependency>
              <groupId>javax.transaction</groupId>
              <artifactId>jta</artifactId>
              <version>1.1</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-orm</artifactId>
              <version>${spring.version}</version>
              <type>jar</type>
              <scope>compile</scope>
          </dependency>

          <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-core</artifactId>
              <version>3.6.0.Final</version>
          </dependency>
          <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-log4j12</artifactId>
              <version>1.6.5</version>
          </dependency>

          <dependency>
              <groupId>javassist</groupId>
              <artifactId>javassist</artifactId>
              <version>3.12.1.GA</version>
          </dependency>

         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi</artifactId>
             <version>3.10.1</version>
         </dependency>


    </dependencies>
</project>

Solution

  • Keep only this dependency for poi

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10.1</version>
        </dependency>
    

    you don't have this properies spring.version in your pom, make it look like this for test

       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>2.5.6</version>
        </dependency>