apache的POI项目。参考资料:http://jakarta.apache.org/poi/index.html。包括包、例子、文档说明.....

解决方案 »

  1.   

    假设你的C:\test目录下有个 first.xls 文件,
    此文件有一个工作表 pageone :
    第1列第1行的值为 first ;
    此文件另有一个工作表 pagetwo:
    第1列第1行的值为 first ,
    第1列第2行的值为 second ,
    第1列第3行的值为 third ,
    那么你就在此目录下建立一个 readxls.java 文件,内容如下:
    import java.io.*; 
    import jxl.*; 
    public class readxls { 
       public static void main(String args[]) { 
          try { 
              Workbook book0= Workbook.getWorkbook(new File("first.xls"));           Sheet sheet0=book0.getSheet(0);   //获得第一个工作表对象
              Cell cell0=sheet0.getCell(0,0);   //得到第一列第一行的单元格 
              String result0=cell0.getContents(); 
              System.out.println(result0);           Sheet sheet1=book0.getSheet(1);   //获得第2个工作表对象          Cell cell1=sheet1.getCell(0,0);   //得到第1列第1行的单元格 
              String result1=cell1.getContents(); 
              System.out.println(result1);           Cell cell2=sheet1.getCell(0,1);   //得到第1列第2行的单元格 
              String result2=cell2.getContents(); 
              System.out.println(result2);           Cell cell3=sheet1.getCell(0,2);   //得到第1列第3行的单元格 
              String result3=cell3.getContents(); 
              System.out.println(result3);           book0.close(); 
          } catch(Exception e) { 
              System.out.println(e); 
          } 
       } 
    }
    那么你编译后应该就可以看到你的内容了。
    前提是你一定要把jxl.jar设置好。
      

  2.   

    如果只想抽取出excel中的内容,我这里刚好有一份。
    功能是取得excel中所有工作表中的内容,以String返回
    import java.io.*;
    import java.math.*;import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class ReadExcel {
      public static String Excel2String(File file) throws IOException{
        String returnstr="";
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
        for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
          if (null != workbook.getSheetAt(numSheets)) {
            HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet
            for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
              if (null != aSheet.getRow(rowNumOfSheet)) {
                HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
                for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
                  if (null != aRow.getCell(cellNumOfRow)) {
                    HSSFCell aCell = aRow.getCell(cellNumOfRow);
                    int cellType = aCell.getCellType();
                    String strCell=null;
                    //strCell =aCell.getStringCellValue();
                    //returnstr+=strCell+" ";                switch (cellType) {
                      case HSSFCell.CELL_TYPE_NUMERIC:    //Numeric
                        BigDecimal bd=new BigDecimal(aCell.getNumericCellValue());
                        strCell=bd.toString();
                        returnstr+=strCell+" ";
                        break;
                      case HSSFCell.CELL_TYPE_STRING:    //String
                        strCell = aCell.getStringCellValue();
                        returnstr+=strCell+" ";
                        break;
                      case HSSFCell.CELL_TYPE_FORMULA:    //formula
                        strCell = String.valueOf(aCell.getNumericCellValue());
                        returnstr+=strCell+" ";
                        break;
                      case HSSFCell.CELL_TYPE_BLANK://blank
                        strCell = aCell.getStringCellValue();
                        returnstr+=strCell+" ";
                        break;
                      default:
                        System.out.println("格式读入不正确!");//其它格式的数据
                    }              }
                }
              }        }
          }
        }
        return returnstr;
      }  public static void main(String[] args) throws IOException{
        File file =new File("D:\\bea\\啊啊啊.xls");
        String str=Excel2String(file);
        System.out.println(str);
      }
    }有一个开源组件需要下载,去http://jakarta.apache.org/poi/下载
      

  3.   

    这个类实现了excel的遍历
    第一层循环遍历的是sheet表
    第二层遍历行
    第三层遍历列
      

  4.   

    可将Excel文件作为ODBJ Data Source看待:
    在Control Panel中Administrative Tools的Data Sources (ODBC)加入你的数据源。
    后期处理与数据库的处理一样。