写了一个java读取excel表格的小程序
如果没有cell中都是文本类型的话,就可以很容易实现读取(打印出来)
但是如果在excel中cell的数据为数字的话,就不能读取了,必须要在这个数据前面手动加上一个('),这样使其变成一个文本类型的,通过程序自动实现,求解
[code]
package cn.bidlink.test;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class readxls {
private static final int CELL_TYPE_NUMERIC = 0; static String filename = "test.xls"; // 要读取的excel表的文件名称 static String c0 = null; // 单元格的内容 static String c1 = null; static String c2 = null; // static String c3=null; public static void main(String[] args) throws FileNotFoundException, IOException, NullPointerException{
  POIFSFileSystem file =new POIFSFileSystem(new FileInputStream(filename)); // 打开输入流
  HSSFWorkbook book = new HSSFWorkbook(file); //
  HSSFSheet sheet1 = book.getSheetAt(0); // 读取sheet1(book.getSheetAt(0)这里的0表示1!)
  int total = sheet1.getLastRowNum();  // 读取该表的总行数
  System.out.println("loading...");
  System.out.println("共读到数据" + total + "条");
   try{
    for(int i = 0 ;i<=total;i++){
     HSSFRow rows = sheet1.getRow(i); // 读取sheet1中的第i行
     HSSFCell cell0 = rows.getCell((short)0); // 读取出第i行的第一列
     HSSFCell cell1 = rows.getCell((short)1);
     HSSFCell cell2 = rows.getCell((short)2);
// HSSFCell cell3 = rows.getCell((short)3);
     
//     if(cell0.getCellType()==CELL_TYPE_NUMERIC || cell1.getCellType()==CELL_TYPE_NUMERIC || cell2.getCellType()==CELL_TYPE_NUMERIC){
//      // 判断cell是否为数字
//   
//     }
     
     
     c0 = cell0.getStringCellValue(); // 获取单元格的字符(内容)
     c1 = cell1.getStringCellValue().trim();
     c2 = cell2.getStringCellValue().trim();
     // c3 = cell3.getStringCellValue();
 
      System.out.println(c0+"  |  "+c1+"  |  "+c2);
    
      
    } 
   }catch(Exception e){
    e.printStackTrace();
   }
  }
}[/code]

解决方案 »

  1.   

    package cn.bidlink.test;import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Date;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;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class readxls {
    private static final int CELL_TYPE_NUMERIC = 0; static String filename = "test.xls"; // 要读取的excel表的文件名称 static String c0 = null; // 单元格的内容 static String c1 = null; static String c2 = null; // static String c3=null; public static void main(String[] args) throws FileNotFoundException, IOException, NullPointerException{
      POIFSFileSystem file =new POIFSFileSystem(new FileInputStream(filename)); // 打开输入流
      HSSFWorkbook book = new HSSFWorkbook(file); //
      HSSFSheet sheet1 = book.getSheetAt(0); // 读取sheet1(book.getSheetAt(0)这里的0表示1!)
      int total = sheet1.getLastRowNum();  // 读取该表的总行数
      System.out.println("loading...");
      System.out.println("共读到数据" + total + "条");
       try{
        for(int i = 0 ;i<=total;i++){
         HSSFRow rows = sheet1.getRow(i); // 读取sheet1中的第i行
         HSSFCell cell0 = rows.getCell((short)0); // 读取出第i行的第一列
         HSSFCell cell1 = rows.getCell((short)1);
         HSSFCell cell2 = rows.getCell((short)2);
    // HSSFCell cell3 = rows.getCell((short)3);
         
    //     if(cell0.getCellType()==CELL_TYPE_NUMERIC || cell1.getCellType()==CELL_TYPE_NUMERIC || cell2.getCellType()==CELL_TYPE_NUMERIC){
    //      // 判断cell是否为数字
    //   
    //     }
         
         
         c0 = cell0.getStringCellValue(); // 获取单元格的字符(内容)
         c1 = cell1.getStringCellValue().trim();
         c2 = cell2.getStringCellValue().trim();
         // c3 = cell3.getStringCellValue();
     
          System.out.println(c0+"  |  "+c1+"  |  "+c2);
        
          
        } 
       }catch(Exception e){
        e.printStackTrace();
       }
      }
    }
      

  2.   


    cell1.getCellType()==HSSFCell.CELL_TYPE_NUMERIC进行一下判断。
    里面总共这些类型
    HSSFCell.CELL_TYPE_BLANK;
    HSSFCell.CELL_TYPE_BOOLEAN;
    HSSFCell.CELL_TYPE_ERROR;
    HSSFCell.CELL_TYPE_FORMULA;
    HSSFCell.CELL_TYPE_NUMERIC;
    HSSFCell.CELL_TYPE_STRING;
    具体资料可以查
    http://poi.apache.org/apidocs/index.html
      

  3.   

    if (cell != null) {
        int cellType = cell.getCellType();

        switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
                 //类型处理
                  break;
    .........下边懒的写了,不好意思
      

  4.   

    直接使用toString()的方法

    HSSFCell   cell0   =   rows.getCell((short)0);
    String a = cell0.toString();
      

  5.   

    操作excel可以使用Jxcell,比poi更高效、好用,并且支持chart和实时公式计算
      

  6.   

    to TerryLhw1983我没有明白您的意思,我加上了
    String   a   =   cell0.toString();
    打印a,会报错的!!
    loading...
    共读到数据21条
    org.apache.poi.hssf.usermodel.HSSFCell@1c78e57java.lang.NumberFormatException: You cannot get a string value from a numeric cell
    at org.apache.poi.hssf.usermodel.HSSFCell.getStringCellValue(HSSFCell.java:775)
    at cn.bidlink.test.readxls.main(readxls.java:60)
      

  7.   

    是我表述的不明确还是由其他的问题?为什么没有人解答呢?我google,baidu很久,都没有找到问题的所在,所以希望各路高手能伸出援助之手
      

  8.   

    public String getCellValueToString(HSSFCell cell) { String rtnVal = null; if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: { if (HSSFDateUtil.isCellDateFormatted(cell)) { // 判断值为日期
    rtnVal = HSSFDateUtil.getJavaDate(
    cell.getNumericCellValue()).toLocaleString();
    } else { // 纯数字
    rtnVal = String.valueOf(cell.getNumericCellValue());
    } break;
    } case HSSFCell.CELL_TYPE_STRING: // 值为字符串
    rtnVal = cell.getStringCellValue();
    break; case HSSFCell.CELL_TYPE_BOOLEAN: // 值为boolean
    rtnVal = String.valueOf(cell.getBooleanCellValue());
    break;

    default :
    rtnVal = ""; }
    } return rtnVal;
    }
    这是我写的一个取值的方法.只有几种最基本的数据类型,差不多够用,其他类型都置空了 
      

  9.   

    谢谢大家回帖
    在一番折腾后,我终于知道,我的想法是不现实的
    查阅了资料,了解到,java的poi读取excel,只能把单元格当作字符串来读取
    所以,我再尝试另一种方法解决
    利用jxl包
    但是这个总会报数组越界的异常,请大家帮我看看
    package cn.bidlink;import java.io.*;
    import jxl.*;public class Pro1 {
    public static String filename = "test.xls";
    public static int total = 0;
    public static void main(String[] args) throws IndexOutOfBoundsException {
    try {
    Workbook wb = Workbook.getWorkbook(new File(filename));
    Sheet sh = wb.getSheet(0);
    for (int k = 0; k <= sh.getRows(); k++) {
    for (int i = 0; i <= sh.getColumns(); i++) {
    Cell cell = sh.getCell(i, k);
    String celStr = cell.getContents();
    System.out.println(celStr);
    }
    }
    wb.close(); 
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  10.   

    for   (int   k   =   0;   k   <=   sh.getRows();   k++)    
    for   (int   i   =   0;   i   <=   sh.getColumns();   i++)    for   (int   k   =   0;   k   <  sh.getRows();   k++)   
    for   (int   i   =   0;   i   <  sh.getColumns();   i++)