我用poi读excel,在window下没有问题,但把程序放到linux上时,读取客户端的excel文件报错,不知道如何解决?
错误:
C:\a.xls 
You chose the file C:\a.xls. It's contents are: Error occurred: C:\a.xls (No such file or directory) 但在window下完全正常
我试着把c:\a.xls中的"\" 改为"/"还是不行,那位知道如何改呢,多谢读excel的poi如下:
-------------------------------------------------------
<%@page contentType="text/html; charset=gb2312" import="java.io.*,org.apache.poi.poifs.filesystem.POIFSFileSystem,org.apache.poi.hssf.record.*,org.apache.poi.hssf.model.*,org.apache.poi.hssf.usermodel.*,org.apache.poi.hssf.util.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>资金文件</title>
</head>
<body>
An example of using Jakarta POI's HSSF package to read an excel spreadsheet: 
<form name="form1" method="post" action="">
  <p>Select an Excel file to read.</p>
  <p> 
    <input name="xls_filename" type="file" onChange="form1.submit()" size="90">
    <input type="submit" name="Submit" value="提交">
  </p>
</form>
<%
String filename = request.getParameter("xls_filename"); 
out.println(filename);
if (filename != null && !filename.equals("")) {
%>
<br>You chose the file <%= filename %>. 
<br><br>It's contents are: 
<%
            try
            {                // create a poi workbook from the excel spreadsheet file
                POIFSFileSystem fs =
                    new POIFSFileSystem(new FileInputStream(filename));
out.println("fs="+fs);
                HSSFWorkbook wb = new HSSFWorkbook(fs);                for (int k = 0; k < wb.getNumberOfSheets(); k++)
                {
%>
                    <br><br>Sheet  <%= k %> <br>
<%

                    HSSFSheet sheet = wb.getSheetAt(k);
                    int       rows  = sheet.getPhysicalNumberOfRows();                    for (int r = 0; r < rows; r++)
                    {
                        HSSFRow row   = sheet.getRow(r);
                        if (row != null) { 
                            int     cells = row.getPhysicalNumberOfCells();
%>
<br><b>ROW  <%= 
row.getRowNum() %> </b>
<%
                            for (short c = 0; c < cells; c++) 
                            { 
                                HSSFCell cell  = row.getCell(c);
                                if (cell != null) { 
                                    String   value = null;                                    switch (cell.getCellType())
                                    {                                        case HSSFCell.CELL_TYPE_FORMULA :
                                            value = "FORMULA ";
                                            break;                                        case HSSFCell.CELL_TYPE_NUMERIC :
                                            value = "NUMERIC value="
                                                    + cell.getNumericCellValue
();
                                            break;                                        case HSSFCell.CELL_TYPE_STRING :
                                            value = "STRING value="
                                                    + cell.getStringCellValue();
                                            break;                                        default :
                                    }
%>
                                    <%= "CELL col=" 

+ cell.getCellNum()
                                        + " VALUE=" + value %>
<%
                                } 
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
%>
Error occurred:  <%= e.getMessage() %>
<%
                e.printStackTrace();
            }

%> 
</body>
</html>

解决方案 »

  1.   

    我用poi读excel,在window下没有问题,但把程序放到linux上时,读取客户端的excel文件报错,不知道如何解决?linux下使用绝对路径吧。linux没有分区的。只有目录。
    比如/app/soft/stat/asdfa.xls
      

  2.   

    to:楼上,我把excel文件放到服务器上,指定usr/aaa/a.xls这样调用是没有错的,但我不要求这样的结果,我是候让用户在客户端上浏览excel文件,然后读excel的?但这样不行不知道为什么
      

  3.   

    to楼上..那poi包我下的poi-src-3.0.1-FINAL-20070705.zip怎么没有org.apache.poi.poifs.filesystem.POIFSFileSystem等文件
      

  4.   

    我认为还是个路径的问题,
    java代码是服务器端解析运行的,也就是在你这个linux下的,当然不能识别这种路径的你上面说到在windows下可以,但我怀疑你是不是这样:客户端和服务器同是一台机器,这个文件表面是是客户端,但同时也是在服务器上,因为你用的同一台机器(只是这样猜测)