`
8792321
  • 浏览: 39211 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

poi 读取excel2007

    博客分类:
  • Java
阅读更多
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcel2007 {
	public static void main(String[] args) {
		FileInputStream is = null;
		String srcFile = "e:/2007.xlsx";		
		try {
			is = new FileInputStream(new File(srcFile));
			XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(is));
			for (int i = 0; i < wb.getNumberOfSheets(); i++) {// 循环sheet
				XSSFSheet childSheet = wb.getSheetAt(i);
				for (int j = 0; j <= childSheet.getLastRowNum(); j++) {// 循环该子sheet row 
					XSSFRow row = childSheet.getRow(j);

					if (row != null) {
						for (int k = 0; k <= row.getLastCellNum(); k++) {// 循环该子sheet行对应的单元格项
							XSSFCell cell = row.getCell(k);// 单元格
							if (cell != null) {
								switch (cell.getCellType()) {
								case XSSFCell.CELL_TYPE_BLANK:
									break;
								case XSSFCell.CELL_TYPE_ERROR:
									break;
								case XSSFCell.CELL_TYPE_BOOLEAN:
									System.out.print(cell.getBooleanCellValue());
									break;
								case XSSFCell.CELL_TYPE_FORMULA:// 公式
									System.out.print(cell.getNumericCellValue());
									break;
								case XSSFCell.CELL_TYPE_NUMERIC:
									if(DateUtil.isCellDateFormatted(cell)){
										System.out.print(cell.getDateCellValue());
									}else{
										System.out.print(Double.valueOf(cell.getNumericCellValue()).longValue());										
									}
									break;
								case XSSFCell.CELL_TYPE_STRING:
									System.out.print(cell.getRichStringCellValue());
									break;
								default:
									System.out.print(cell);
								}
							}
							System.out.print(',');
						}
						System.out.println();
					}
				}
			}			
			 
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (Exception e) {
			}
		}
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics