POI读取Excel 各种特殊数字和类型的转换

1, 对于类似电话号码或手机一类的大数值读取问题 

// 取值后会带一个E的问题 
double cellValue = row.getCell(k).getNumericCellValue(); 
fieldValues[k] = new DecimalFormat("#").format(cellValue); 

2, 对于数值型单元的纯数值和日期型的处理 

复制代码
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型   
    if (HSSFDateUtil.isCellDateFormatted(cell)) {   
        //  如果是date类型则 ,获取该cell的date值   
        value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();   
    } else { // 纯数字   
        value = String.valueOf(cell.getNumericCellValue());   
}  
复制代码

转: POI读取Excel浅谈 
先看代码,挨句解释: 
一般遍历使用两种方式,1:得到总的行数和每行的列数,然后循环。2:使用迭代 
先看第一种: 

复制代码
Java代码  
package com.golden.test;   
  
import java.io.File;   
import java.io.FileInputStream;   
  
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;   
  
/**  
*   
* @author 崔素强  
*   
*/  
public class PoiReadXls2 {   
    public static void main(String[] args) {   
        File f = new File("c:\\a.xls");   
        try {   
            FileInputStream is = new FileInputStream(f);   
            HSSFWorkbook wbs = new HSSFWorkbook(is);   
            HSSFSheet childSheet = wbs.getSheetAt(0);   
            // System.out.println(childSheet.getPhysicalNumberOfRows());   
            System.out.println("有行数" + childSheet.getLastRowNum());   
            for (int j = 0; j < childSheet.getLastRowNum(); j++) {   
                HSSFRow row = childSheet.getRow(j);   
                // System.out.println(row.getPhysicalNumberOfCells());   
                // System.out.println("有列数" + row.getLastCellNum());   
                if (null != row) {   
                    for (int k = 0; k < row.getLastCellNum(); k++) {   

                        HSSFCell cell = row.getCell(k);   
                        if (null != cell) {   
                            switch (cell.getCellType()) {   
                            case HSSFCell.CELL_TYPE_NUMERIC: // 数字   
                                System.out.print(cell.getNumericCellValue()   
                                        + "   ");   
                                break;   
                            case HSSFCell.CELL_TYPE_STRING: // 字符串   
                                System.out.print(cell.getStringCellValue()   
                                        + "   ");   
                                break;   
                            case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean   
                                System.out.println(cell.getBooleanCellValue()   
                                        + "   ");   
                                break;   
                            case HSSFCell.CELL_TYPE_FORMULA: // 公式   
                                System.out.print(cell.getCellFormula() + "   ");   
                                break;   
                            case HSSFCell.CELL_TYPE_BLANK: // 空值   
                                System.out.println(" ");   
                                break;   
                            case HSSFCell.CELL_TYPE_ERROR: // 故障   
                                System.out.println(" ");   
                                break;   
                            default:   
                                System.out.print("未知类型   ");   
                                break;   
                            }   
                        } else {   
                            System.out.print("-   ");   
                        }   
                    }   
                }   
                System.out.println();   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
}  

package com.golden.test; 

import java.io.File; 
import java.io.FileInputStream; 

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; 

/** 
* 
* @author 崔素强 
* 
*/ 
public class PoiReadXls2 { 
        public static void main(String[] args) { 
                File f = new File("c:\\a.xls"); 
                try { 
                        FileInputStream is = new FileInputStream(f); 
                        HSSFWorkbook wbs = new HSSFWorkbook(is); 
                        HSSFSheet childSheet = wbs.getSheetAt(0); 
                        // System.out.println(childSheet.getPhysicalNumberOfRows()); 
                        System.out.println("有行数" + childSheet.getLastRowNum()); 
                        for (int j = 0; j < childSheet.getLastRowNum(); j++) { 
                                HSSFRow row = childSheet.getRow(j); 
                                // System.out.println(row.getPhysicalNumberOfCells()); 
                                // System.out.println("有列数" + row.getLastCellNum()); 
                                if (null != row) { 
                                        for (int k = 0; k < row.getLastCellNum(); k++) { 
                                                HSSFCell cell = row.getCell(k); 
                                                if (null != cell) { 
                                                        switch (cell.getCellType()) { 
                                                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字 
                                                                System.out.print(cell.getNumericCellValue() 
                                                                                + "   "); 
                                                                break; 
                                                        case HSSFCell.CELL_TYPE_STRING: // 字符串 
                                                                System.out.print(cell.getStringCellValue() 
                                                                                + "   "); 
                                                                break; 
                                                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean 
                                                                System.out.println(cell.getBooleanCellValue() 
                                                                                + "   "); 
                                                                break; 
                                                        case HSSFCell.CELL_TYPE_FORMULA: // 公式 
                                                                System.out.print(cell.getCellFormula() + "   "); 
                                                                break; 
                                                        case HSSFCell.CELL_TYPE_BLANK: // 空值 
                                                                System.out.println(" "); 
                                                                break; 
                                                        case HSSFCell.CELL_TYPE_ERROR: // 故障 
                                                                System.out.println(" "); 
                                                                break; 
                                                        default: 
                                                                System.out.print("未知类型   "); 
                                                                break; 
                                                        } 
                                                } else { 
                                                        System.out.print("-   "); 
                                                } 
                                        } 
                                } 
                                System.out.println(); 
                        } 
                } catch (Exception e) { 
                        e.printStackTrace(); 
                } 
        } 
} 
复制代码

得到Excel的文件然后读取,这个很简单。关键有两个地方,也许在网上会看到有的这样使用有的那样使用。 
System.out.println("有行数" + childSheet.getLastRowNum()); 
System.out.println(childSheet.getPhysicalNumberOfRows()); 
System.out.println("有列数" + row.getLastCellNum()); 
System.out.println(row.getPhysicalNumberOfCells()); 
如果人都拷贝代码进行使用了,不知道有什么区别。太多的区别不知道,但是有一点我发现了,那就是如果中间各行或者隔列的话getPhysicalNumberOfRows和getPhysicalNumberOfCells就不能读取到所有的行和列了。 
再者,一定要对单元格的格式进行判断switch (cell.getCellType()),不同的单元格格式使用不同的方法。最后加上为止类型,以防万一。 
而且在数字类型里,又分为了纯数字和时间格式: 

复制代码
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型   
    if (HSSFDateUtil.isCellDateFormatted(cell)) {   
        //  如果是date类型则 ,获取该cell的date值   
        value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();   
    } else { // 纯数字   
        value = String.valueOf(cell.getNumericCellValue());   
}  

case HSSFCell.CELL_TYPE_NUMERIC: // 数值型 
        if (HSSFDateUtil.isCellDateFormatted(cell)) { 
                //  如果是date类型则 ,获取该cell的date值 
                value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString(); 
        } else { // 纯数字 
                value = String.valueOf(cell.getNumericCellValue()); 
} 
复制代码

还有一种迭代的方法: 
Java代码  

复制代码
package com.golden.test;   
  
import java.io.File;   
import java.io.FileInputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.util.Iterator;   
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;   
  
/**  
*   
* @author 崔素强  
*   
*/  
public class PoiReadXls {   
    @SuppressWarnings( { "unchecked", "deprecation" })   
    public static void main(String[] args) {   
        File f = new File("c:\\a.xls");   
        try {   
            InputStream input = new FileInputStream(f);   
            POIFSFileSystem fs = new POIFSFileSystem(input);   
            HSSFWorkbook wb = new HSSFWorkbook(fs);   
            HSSFSheet sheet = wb.getSheetAt(0);   
            Iterator rows = sheet.rowIterator();   
            while (rows.hasNext()) {   
                HSSFRow row = (HSSFRow) rows.next();   
                // System.out.print("行:" + row.getRowNum() + " ");   
                Iterator cells = row.cellIterator();   
                while (cells.hasNext()) {   
                    HSSFCell cell = (HSSFCell) cells.next();   
                    // System.out.println("列:" + cell.getCellNum());   
                    switch (cell.getCellType()) {   
                    case HSSFCell.CELL_TYPE_NUMERIC: // 数字   
                        System.out.print(cell.getNumericCellValue() + "   ");   
                        break;   
                    case HSSFCell.CELL_TYPE_STRING: // 字符串   
                        System.out.print(cell.getStringCellValue() + "   ");   
                        break;   
                    case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean   
                        System.out.println(cell.getBooleanCellValue() + "   ");   
                        break;   
                    case HSSFCell.CELL_TYPE_FORMULA: // 公式   
                        System.out.print(cell.getCellFormula() + "   ");   
                        break;   
                    case HSSFCell.CELL_TYPE_BLANK: // 空值   
                        System.out.println(" ");   
                        break;   
                    case HSSFCell.CELL_TYPE_ERROR: // 故障   
                        System.out.println(" ");   
                        break;   
                    default:   
                        System.out.print("未知类型   ");   
                        break;   
                    }   
                }   
                System.out.println();   
            }   
        } catch (IOException ex) {   
            ex.printStackTrace();   
        }   
    }   
}  

package com.golden.test; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Iterator; 
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; 

/** 
* 
* @author 崔素强 
* 
*/ 
public class PoiReadXls { 
        @SuppressWarnings( { "unchecked", "deprecation" }) 
        public static void main(String[] args) { 
                File f = new File("c:\\a.xls"); 
                try { 
                        InputStream input = new FileInputStream(f); 
                        POIFSFileSystem fs = new POIFSFileSystem(input); 
                        HSSFWorkbook wb = new HSSFWorkbook(fs); 
                        HSSFSheet sheet = wb.getSheetAt(0); 
                        Iterator rows = sheet.rowIterator(); 
                        while (rows.hasNext()) { 
                                HSSFRow row = (HSSFRow) rows.next(); 
                                // System.out.print("行:" + row.getRowNum() + " "); 
                                Iterator cells = row.cellIterator(); 
                                while (cells.hasNext()) { 
                                        HSSFCell cell = (HSSFCell) cells.next(); 
                                        // System.out.println("列:" + cell.getCellNum()); 
                                        switch (cell.getCellType()) { 
                                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字 
                                                System.out.print(cell.getNumericCellValue() + "   "); 
                                                break; 
                                        case HSSFCell.CELL_TYPE_STRING: // 字符串 
                                                System.out.print(cell.getStringCellValue() + "   "); 
                                                break; 
                                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean 
                                                System.out.println(cell.getBooleanCellValue() + "   "); 
                                                break; 
                                        case HSSFCell.CELL_TYPE_FORMULA: // 公式 
                                                System.out.print(cell.getCellFormula() + "   "); 
                                                break; 
                                        case HSSFCell.CELL_TYPE_BLANK: // 空值 
                                                System.out.println(" "); 
                                                break; 
                                        case HSSFCell.CELL_TYPE_ERROR: // 故障 
                                                System.out.println(" "); 
                                                break; 
                                        default: 
                                                System.out.print("未知类型   "); 
                                                break; 
                                        } 
                                } 
                                System.out.println(); 
                        } 
                } catch (IOException ex) { 
                        ex.printStackTrace(); 
                } 
        } 
} 
复制代码

这种方法,如果数据的紧凑的,使用还是方便的,但是我发现,如果是空行或者是空列,他就会隔过去。具体的自己试试就知道了。 
另外,也能看到这里得到Excel文件的方式是通过File,如果要引用到Struts2里,这是很简单的,因为Struts2上传时Action里定义的就是File或者File数组。 

转自 http://cuisuqiang.iteye.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值