`

java读取excel

    博客分类:
  • java
阅读更多

1. 下载poi库

http://poi.apache.org/download.html

2.将poi核心文件取出放入myeclipse的lib中

3.测试代码

package test5;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.ParseException;

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;

/**
 * 读取Excel测试
 * @author chitianxiang
 * @version v1.0 $17th April, 2012 - Tuesday - 4:34 p.m
 */
public class Test1 {
	
	public static void main(String[] args) {
		File file = new File("E:/chtx/项目/home.xls");
		InputStream fileIn = null;
		InputStream in = null;
		try {
			fileIn = new FileInputStream(file);
			in = new BufferedInputStream(fileIn);
			HSSFWorkbook wb = new HSSFWorkbook(in);
			for (int i = 0, sheetNum = wb.getNumberOfSheets(); i < sheetNum; i++) {
				HSSFSheet sheet = wb.getSheetAt(i);		//读取页面
				if (sheet == null) {
					continue;
				}
				for (int j = 0, rowNum = sheet.getLastRowNum(); j < rowNum; j++) {
					HSSFRow row = sheet.getRow(j);		//读取行
					if (row == null) {
						continue;
					}
					for (int z = 0, cellNum = row.getLastCellNum(); z < cellNum; z++) {
						HSSFCell cell = row.getCell(z);		//读取列
						if (cell == null) {
							continue;
						}
						switch (z) {
							case 0: 
								System.out.println(readCell2String(cell));
								break;
							case 1:
								System.out.println(readCell2String(cell));
								break;
							default:
								//do nothing
								break;
						}
					}
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("文件没找着");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("读取流异常");
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileIn != null) {
				try {
					fileIn.close();
				} catch (IOException e) {
					System.out.println("关闭流异常");
					e.printStackTrace();
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					System.out.println("关闭流异常");
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 将单元格内容转换成字符串,空白和错误默认为空
	 * @param cell	excel 单元格
	 * @author chitianxiang $17th April, 2012 - Tuesday - 5:54 p.m
	 */
	private static String readCell2String(HSSFCell cell) {
		String cellContent = "";
		switch(cell.getCellType()) {
			case HSSFCell.CELL_TYPE_NUMERIC: // 数字  
				cellContent = cell.getNumericCellValue() + "";  
				break;  
			case HSSFCell.CELL_TYPE_STRING: // 字符串  
				cellContent = cell.getStringCellValue() + "";  
				break;  
			case HSSFCell.CELL_TYPE_BOOLEAN: // boolean  
				cellContent = cell.getBooleanCellValue() + "";  
				break;  
			case HSSFCell.CELL_TYPE_FORMULA: // 公式  
				cellContent = cell.getCellFormula() + "";  
				break;  
			case HSSFCell.CELL_TYPE_BLANK: // 空白  
			case HSSFCell.CELL_TYPE_ERROR: // 错误  
			default:  
				cellContent = "";  
				break;  
		}
		
		return normalNumber(cellContent);
	}
	
	/**
	 * 将特殊数字格式转换成普通格式
	 * @param cellContent	数字
	 * @author chitianxiang $17th April, 2012 - Tuesday - 6:09 p.m
	 */
	private static String normalNumber (String cellContent) {
		String tempCellContent = cellContent;
		//如果读取的是科学计数法的格式,则转换为普通格式
        if (null != tempCellContent 			
        		&& tempCellContent.indexOf(".") != -1 
        		&& tempCellContent.indexOf("E") != -1) {
        	
            DecimalFormat df = new DecimalFormat();
            try {
            	tempCellContent = df.parse(tempCellContent).toString();
			} catch (ParseException e) {
				//do nothing
				e.printStackTrace();
			}
		//如果读取的是数字格式,并且以".0"结尾格式,则转换为普通格式
        } else if (null != tempCellContent 		
        		&& tempCellContent.endsWith(".0")) {
        	
            int size = tempCellContent.length();
            tempCellContent = tempCellContent.substring(0, size - 2);
        }
        
        return tempCellContent;
	}
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics