首页 > 代码库 > POI读取excel单元格,获取单元格各类型值,返回字符串类型

POI读取excel单元格,获取单元格各类型值,返回字符串类型

package a;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import landprice.util.DateUtil;

public class TestReadFormula {
    private static FormulaEvaluator evaluator;
    public static void main(String[] args) throws IOException {
        InputStream is=new FileInputStream("E:/temp/tempfile/1.xls");
        HSSFWorkbook wb=new HSSFWorkbook(is);
        Sheet sheet=wb.getSheetAt(0);
        
        evaluator=wb.getCreationHelper().createFormulaEvaluator();
        
        for (int i = 0; i <1; i++) {
            Row  row=sheet.getRow(i);
            for (Cell cell : row) {
                System.out.println(getCellValueByCell(cell));
            }
        }
        is.close();
    }
    
    //获取单元格各类型值,返回字符串类型
    private static String getCellValueByCell(Cell cell) {
    	//判断是否为null或空串
        if (cell==null || cell.toString().trim().equals("")) {
            return "";
        }
        String cellValuehttp://www.mamicode.com/= "";
        int cellType=cell.getCellType();
        if(cellType==Cell.CELL_TYPE_FORMULA){ //表达式类型
        	cellType=evaluator.evaluate(cell).getCellType();
        }
        
        switch (cellType) {
        case Cell.CELL_TYPE_STRING: //字符串类型
            cellValue= http://www.mamicode.com/cell.getStringCellValue().trim();"" : cellValue;  
            break;
        case Cell.CELL_TYPE_BOOLEAN:  //布尔类型
        	cellValue = http://www.mamicode.com/String.valueOf(cell.getBooleanCellValue());  "yyyy-MM-dd"); 
             } else {  //否
            	 cellValue = http://www.mamicode.com/new DecimalFormat("#.######").format(cell.getNumericCellValue());  
             }  
            break;
        default: //其它类型,取空串吧
        	cellValuehttp://www.mamicode.com/= "";
            break;
        }
        return cellValue;
    }
    
   
}

 

 技术分享

 

POI读取excel单元格,获取单元格各类型值,返回字符串类型