读取xlsx文件行时的Apache POI ClassCastException

时间:2014-12-11 21:06:08

标签: java jsp apache-poi classnotfoundexception

我正在编写一个使用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();
    }
}

1 个答案:

答案 0 :(得分:3)

您还需要xssf的这种依赖关系:

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

如果您现在没有收到编译错误,则表示您从其他地方获得此依赖关系,可能是您的RiverBoat项目暴露了较旧/不兼容的版本。所以你要么在这个pom中都需要poi依赖,要么都没有(因为两者都可能被RiverBoat暴露)。