请用过POI的朋友指教。POI能用在WEB中吗?怎么用?最好有例子。
EMAIL:[email protected]

解决方案 »

  1.   

    Poi是java中用于excel处理的第三方包,当然可以用于web.如下:
    jsp 读EXCEL文件:
    package cn.founder.tokubai.business.pdu;
    import java.io.*;
    import java.util.*;
    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.poifs.filesystem.*;
    public class test {
    private   HSSFWorkbook   poiWorkbook = null;
    private   HSSFSheet      poiSheet = null;
    private   int            curLine = 0;
    private Vector vContent = null;
    void importExcel(){
     vContent= new Vector();
    String strFileName = "";
    POIFSFileSystem fs = null;
    try{
       fs = new POIFSFileSystem(new FileInputStream(strFileName));
       poiWorkbook = new HSSFWorkbook(fs);
       //默认读取第一个 sheet
       poiSheet = poiWorkbook.getSheetAt(0);
       if (poiSheet == null){
    return ;
       }
    } catch (IOException e){
       poiWorkbook = null;
    }
    while (readNextRecord()){
     //.....
    }

    }
    boolean readNextRecord(){
       int lineIndex = poiSheet.getFirstRowNum() + curLine;
       curLine ++;
       if (lineIndex > poiSheet.getLastRowNum()){
    return false;
       }
       HSSFRow row = poiSheet.getRow(lineIndex);
      //此行为空行
       if (row == null){
     return true;
       }
       HSSFCell cell = null;
       for (short iCell = row.getFirstCellNum(); iCell <= row.getLastCellNum(); iCell ++){
    cell = row.getCell(iCell);
    if (cell == null){
       vContent.add("");
       continue;
    }
    vContent.add(formatCellToString(cell));
       }
       return true;
    }
    String formatCellToString(HSSFCell cell){
    String strCell = "";
    switch (cell.getCellType()) {
     case HSSFCell.CELL_TYPE_NUMERIC:strCell = String.valueOf(cell.getNumericCellValue());
       break;
     case HSSFCell.CELL_TYPE_STRING:strCell = cell.getStringCellValue();
    break;
     case HSSFCell.CELL_TYPE_BOOLEAN:strCell = String.valueOf(cell.getBooleanCellValue());
    break;
     default:strCell = "";           break;
       }
       return strCell.trim();
    }
    }