-1

我在上传 excel 文件时遇到问题,下面是我的 Java 代码

File newFile = new File("C:/sample.xls");
int firstColNo = 0;
int row = 0;
Cell cell;
Workbook wb = Workbook.getWorkbook(newFile);
Sheet sheet = wb.getSheet(0);
    for (row = 1; row < 1005; row++) {
        LabelCell labelCell1 = sheet.findLabelCell("COMPOUND_ID");
            if (labelCell1 != null) {
                firstColNo = labelCell1.getColumn();
                cell = sheet.getCell(firstColNo, row);
                    if (cell.getContents() != null && cell.getContents().length() > 0) {                                            System.out.println(cell.getContents());
                } else {
                                        System.out.println("-");
                                    }
                                }
                            }

当我调试它时,我得到了这个异常

jxl.read.biff.BiffException: Unable to recognize OLE stream
    at jxl.read.biff.CompoundFile.<init>(CompoundFile.java:116)
    at jxl.read.biff.File.<init>(File.java:127)
    at jxl.Workbook.getWorkbook(Workbook.java:221)
    at jxl.Workbook.getWorkbook(Workbook.java:198)
    at hello.FileUploadExample.doPost(FileUploadExample.java:63)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

我正在使用 jxl.jar 文件进行文件上传。

谁能帮我解决它?

4

1 回答 1

0

在堆栈跟踪上它是

at jxl.Workbook.getWorkbook(Workbook.java:198)

在您的代码中调用

Workbook wb = Workbook.getWorkbook(newFile);

我使用过 POI 和其他 excel 库,而不是 this ,但似乎在这一点上可以将 FileNotFoundException 转换为 jxl.read.biff.BiffException 或者库无法识别输入文件格式。-因为你给了一个普通的.txt 重命名为 .xls

请至少进行 Java 级别的验证

if(!newFile.isFile()){
    return;
}

你也可以检查长度不为0等等。

也许可以成为您的解决方案。

我希望它可以帮助您找到方向。

于 2013-05-21T11:30:59.153 回答