后来测试改成下面这个简单的语句直接运行没问题,到tomcat下就运行到输出1,就不动了
System.out.println(1);
HSSFWorkbook wb = new HSSFWorkbook();
System.out.println(2);

解决方案 »

  1.   

    有没有人在tomcat环境下用过这个类包呢?是否能用呢
      

  2.   

    import org.apache.poi.hssf.usermodel.*;
    import java.util.Iterator;
    import java.util.ArrayList;
    import java.util.List;
    import java.io.*;
    public class ExcelProcess { public static List getSheetInfo(String filename , String sheetname )
    {
    ArrayList list = new ArrayList();
    // filename.replaceAll("","\\");
    System.out.println(filename);
    System.out.println(sheetname);

    try{
    File file = new File(filename);
    System.out.println(file.length());

    //System.out.println("123");
    org.apache.poi.poifs.filesystem.POIFSFileSystem ps = new org.apache.poi.poifs.filesystem.POIFSFileSystem(new FileInputStream(filename));
    //System.out.println("1234");
    HSSFWorkbook workbook = new HSSFWorkbook(ps);
    //System.out.println("12345");
    HSSFSheet sheet = workbook.getSheet(sheetname);
    Iterator iter = sheet.rowIterator();
    //System.out.println("12345");
    while(iter.hasNext())
    {
    HSSFRow row = (HSSFRow)iter.next();
    Iterator celliter = row.cellIterator();
    ArrayList rowlist = new ArrayList();
    //System.out.println("12345");
    while(celliter.hasNext())
    {
    HSSFCell cell = (HSSFCell)celliter.next();
    //cell.setCellType(1);

    String value = "";
    System.out.println("type:"+cell.getCellType());
    switch(cell.getCellType())
    {
      case 0:
       value ="" + (new Double(cell.getNumericCellValue())).intValue();
       break;
      case 1:
       value =  cell.getStringCellValue();
       break;
      case 2:
       value =  cell.getCellFormula();
       break;
      default:
       break;
    }
    //System.out.println(value);
    rowlist.add(value);
    }
    list.add(rowlist);
    }
    }catch(Exception e)
    {
    e.printStackTrace();
    }
    return list;
    }
    public static void main(String[] args) {

    String filename = com.qq.hlj.SystemConf.getDownDir()+"excel/phone.xls";
    List list = ExcelProcess.getSheetInfo(filename,"Sheet1");
    System.out.println("size:"+list.size());
    for(int i=0;i<list.size();i++)
    {
    List templist = (List)list.get(i);
    System.out.println("col size:"+templist.size());
    for(int j=0;j<templist.size();j++)
    {
    System.out.println(templist.get(j));
    }
    }


    }
    }
      

  3.   

    就是这段代码直接运行可以,没有任何问题。在tomcat环境下就不行了
    运行到这System.out.println("1234");之后就不再运行了。
    后来我又换了一台机器,那台机器上是好的。这是什么原因?郁闷死我了!
      

  4.   


    excel的文件位置
    假设filePath是"excel/phone.xls",那么excel文件应该放在tomcat/webapps/你的jsp工程名/excel/phone.xls。
    protected String setFilePath( String filePath )
    {
    return context.getRealPath( filePath );
    }可以直接用
    try
        {
    book = new HSSFWorkbook( new FileInputStream( setFilePath(filePath) ) );
    createCellStrle();
        }
        catch(FileNotFoundException e)
        {
         e.printStackTrace();
        }     来生成HSSFWorkBook。另外这一句应该捕获FileNotFoundException,来确保读入了文件。