Java通过poi对Excel的读取操作

package text;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class readDai {

		public static void read(InputStream inputStream) throws IOException{ 
			
			//初始整个Excel 
			HSSFWorkbook workbook = new HSSFWorkbook(inputStream); 
			//循环workbook中所有sheet 
			for(int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++){ 
				HSSFSheet sheet = workbook.getSheetAt(sheetIndex); 
				System.out.println("sheet序号:"+sheetIndex+",sheet名称:"+workbook.getSheetName(sheetIndex)); 
				//循环该sheet中的有数据的每一行 
				for(int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++){ 
					HSSFRow row = sheet.getRow(rowIndex); 
					if(row == null){ 
						continue; 
					} 
					//循环该行的每一个单元格 
					for(int cellnum = 0; cellnum < row.getLastCellNum(); cellnum++){ 
						HSSFCell cell = row.getCell((short) cellnum); 
						getCellValue(cell,rowIndex,cellnum);
					} 
					System.out.println();
				} 
				System.out.println("---------------------------------------------------------"); 
			}
		}
		
		private static void getCellValue(HSSFCell cell, int rowIndex, int cellnum) {
			if(cell == null){ 
				return;
			}else if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK){ 
				System.out.print(" "+"\t"); 
			}else if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){ 
				System.out.print(cell.getRichStringCellValue().getString()+"\t"); 
			}else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ 
				if(HSSFDateUtil.isCellDateFormatted(cell)){ 
					Date date = cell.getDateCellValue(); 
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
					System.out.print(dateFormat.format(date)+"\t");
				}else{ 
					System.out.print(cell.getNumericCellValue()+"\t"); 
				} 
			}
			else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ 
				System.out.print(cell.getNumericCellValue()+"\t");
			}else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){ 
				System.out.print(cell.getBooleanCellValue()+"\t");
			}else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){ 
				System.out.print(cell.getNumericCellValue()+"\t"); 
			} 
		
			return;
		}

		public static void main(String[] args) { 
			InputStream inputStream = null; 
			try { 
				//读取文件流 
				inputStream = new FileInputStream(new File("F:\\as.xls")); 
				read(inputStream); 
				} catch (FileNotFoundException e) { 
					e.printStackTrace(); 
				} catch (IOException e) { 
					e.printStackTrace(); 
				}finally{ 
					try { 
						if(inputStream != null){ 
							inputStream.close(); 
						} 
					} catch (IOException e) { 
						e.printStackTrace(); 
					} 
				} 
			}
		


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值