无法在春季导入 org.apache.poi,因此无法编译
Posted
技术标签:
【中文标题】无法在春季导入 org.apache.poi,因此无法编译【英文标题】:cannot import org.apache.poi in spring so not able to compile 【发布时间】:2015-08-25 09:34:41 【问题描述】:我正在尝试将我桌面中的 excel 数据导入 mysql 数据库,但最初我无法解析 excel 文件,因为我无法导入 org.apache.poi 我也给了我的 pom 文件,因为我有包含 org.apache.poi 依赖项但仍然无法导入它,因为它我无法获取 POIFSFileSystem、HSSFWorkbook 等使用
我的java代码:
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>
【问题讨论】:
删除其中一个 poi 依赖项。 为什么你的 pom.xml 中有多个 poi 依赖? 为什么你的 pom 中有 3 个 poi 版本。 (使用了 2 个),你正在混合 spring 版本......基本上你的 pom 看起来像一团糟...... 【参考方案1】:只保留这个对 poi 的依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10.1</version>
</dependency>
你的 pom 中没有这个属性 spring.version,让它看起来像这样用于测试
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>2.5.6</version>
</dependency>
【讨论】:
如果您建议使用最新版本的 Apache POI(当前为 3.12 或 3.13-beta1),而不是稍旧的版本,可能会更好以上是关于无法在春季导入 org.apache.poi,因此无法编译的主要内容,如果未能解决你的问题,请参考以下文章
org.apache.poi3.1.7 Excle并发批量导入导出
JAVA EXCEL 导入jar报错The package org.apache.poi.hssf.usermodel is accessible from more than one module:
无法从Excel读取'java.lang.ClassCastException:org.apache.poi.hssf.usermodel.HSSFCell无法强制转换为java.lang.S
无法调用“org.apache.poi.ss.usermodel.Cell.setCellValue(String)”,因为现有 XLSM 文件的“cell”为空
创建错误的curropted.xlsx文件 - 无法找到匹配的构造函数:org.apache.poi.xssf.usermodel.XSSFWorkbook(java.io.FileOutputStr