我正在编写一个类,它使用 apache-poi 库来迭代 xlsx 工作表并获取一些 UPC 编号。应用程序本身在 TOMCAT 服务器上运行 - 代码编译时没有任何错误,但我收到一个奇怪的运行时错误有人知道为什么会发生这种情况吗?我使用 maven 来获取 poi 依赖项,所以这有点意外:
type Exception report
message org.apache.poi.xssf.usermodel.XSSFRow cannot be cast
to org.apache.poi.ss.usermodel.Row
description The server encountered an internal error that prevented it
from fulfilling this request.
异常
java.lang.ClassCastException: org.apache.poi.xssf.usermodel.XSSFRow cannot be cast to org.apache.poi.ss.usermodel.Row
com.riverboat.util.RiverBoatExcelParser.parseXLSManifest(RiverBoatExcelParser.java:51)
com.riverboat.servlets.FileManagerServlet.doPost(FileManagerServlet.java:113)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
注意 Apache Tomcat (TomEE)/7.0.53 (1.6.0.2) 日志中提供了根本原因的完整堆栈跟踪。
编辑:这是 Pom 文件
<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">
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10.1</version>
</dependency>
</dependencies>
<modelVersion>4.0.0</modelVersion>
<groupId>RiverBoat</groupId>
<artifactId>RiverBoat</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是java代码:
package com.riverboat.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class RiverBoatExcelParser {
FileInputStream spreadsheet;
public RiverBoatExcelParser(InputStream file)
{
spreadsheet = (FileInputStream)file;
}
/*
* Parse an XLS spread sheet
*/
public void parseXLSManifest(int book, String UPCColumnHeader)
{
String currText;
boolean foundUPC = false;
List<ExcelManifestRecord> result = new ArrayList<>();
int current_row = 0;
int current_col = 0;
int upc_row;
int upc_col=0;
try {
//Create workbook instance to hold file reference to .xlsxfile
XSSFWorkbook workbook = new XSSFWorkbook(spreadsheet);
//Get first/desire sheed from the workbook
XSSFSheet sheet = workbook.getSheetAt(book);
//Iterate through each row one by one
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the colums
Iterator<Cell> cellIterator = row.cellIterator();
ExcelManifestRecord rec = new ExcelManifestRecord();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
current_col++;
switch(cell.getCellType())
{
case Cell.CELL_TYPE_STRING:
currText = cell.getStringCellValue();
//check for the upc column header
if(currText.equalsIgnoreCase(UPCColumnHeader))
{
upc_col = current_col;
foundUPC = !foundUPC;
}
//TODO: init row/column
break;
case Cell.CELL_TYPE_NUMERIC:
if(current_col == upc_col && foundUPC)
{
rec.UPC = (int)cell.getNumericCellValue();
System.out.println("Found UPC "+rec.UPC);
}
break;
}
}
current_col = 0;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
最佳答案
您还需要 xssf 的此依赖项:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10.1</version>
</dependency>
如果您现在没有收到编译错误,则意味着您有来自其他地方的依赖项,可能您的 RiverBoat 项目公开了旧的/不兼容的版本。因此,您要么需要此 pom 中的两个 poi 依赖项,要么不需要(因为两者都可能由 RiverBoat 公开)。
关于java - 读取 xlsx 文件行时 Apache POI ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27432344/