POI组件读Excel

所需Jar包:poi-3.9-20121203.jar
Excel文件存放路径:C:/Planet Power/demo.xls,这个可以根据你自己想要读取文件的路径而改变,

修改这条语句中的路径即可:

FileInputStream excelFIS = new FileInputStream("C:\\Planet Power\\demo.xls");

/**
 * @author Shaene M. Siders, Dragon Under Glass
 * http://www.DragonUnderGlass.com
 * date: 2010
 */

//Import the Java packages to use
import java.io.FileInputStream;
import java.io.IOException;

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;

public class ExcelReader {

	/**
	 * @param args is not used.  
	 */
	public static void main(String[] args) {
	
		
		   // Create a FileInputStream to hold the file.  Use double back-slashes to create one "escaped" slash.
		   // Use error handling (try/catch) around its creation in case 
		   // the file to read does not exist.
		   // Be sure to import java.io.FileNotFoundException and java.io.IOException, or just use 
		   // the superclass IOException to handle both.  
		
		try {
					FileInputStream excelFIS = new FileInputStream("C:\\Planet Power\\demo.xls");
	
		 // Create an Excel Workbook Object using the FileInputStream created above 
		 // (which contains the file).
		 // Use error handling around its creation in case of Input/Output Exception
	
			HSSFWorkbook excelWB = new HSSFWorkbook(excelFIS);//创建excel工作对象

			// Next, get information out of that Workbook.
			// Start by getting the Spreadsheet (Excel books can have several 
			// sheets).  Assuming there is just one sheet, it's the zero sheet.
			
			HSSFSheet topSheet = excelWB.getSheetAt(0); 
			

			// getRow() returns an HSSFRow object, but the numbering
			// system is logical, not physical, and zero based.
			// e.g. use getRow(2) to get the third row.
			
			HSSFRow thirdRow = topSheet.getRow(2);
			
	
			// Get the first two cells in the row
			HSSFCell lastnameCell = thirdRow.getCell(0);
			HSSFCell firstnameCell = thirdRow.getCell(1);
			
			// Get the string information in the cells
			String firstName = firstnameCell.getStringCellValue();
			String lastName = lastnameCell.getStringCellValue();
			
			// Print out the value of the cells
			System.out.println(firstName + "************** " + lastName);
					
			// Traverse the sheets by looping through sheets, rows, and cells.			
			// Remember, excelWB is the workbook object obtained earlier.
			// Outer Loop:  Loop through each sheet
			for (int sheetNumber = 0; sheetNumber < excelWB.getNumberOfSheets(); sheetNumber++) {
				HSSFSheet oneSheet = excelWB.getSheetAt(sheetNumber);//获取excel中的Sheet表对象
				System.out.println("The sheet num is " + excelWB.getNumberOfSheets());
				// Now get the number of rows in the sheet
				int rows = oneSheet.getPhysicalNumberOfRows();		//获取每一张Sheet表中的物理总行数
				System.out.println("the num of row is :" + rows);
				// Middle Loop:  Loop through rows in the sheet
				for (int rowNumber = 0; rowNumber < rows; rowNumber++) {
					HSSFRow oneRow = oneSheet.getRow(rowNumber);    //获取每一行对象
					
					// Skip empty (null) rows.
					if (oneRow == null) {
						continue;
					}
					
					// Get the number of cells in the row
					int cells = oneRow.getPhysicalNumberOfCells();	//获取每一行中的单元格
					System.out.println("the number of cells in the row "+ cells);
					// Inner Loop:  Loop through each cell in the row

					for (int cellNumber = 0; cellNumber < cells; cellNumber++) {
						HSSFCell oneCell = oneRow.getCell(cellNumber); //获取单元格对象
			
						// Test the value of the cell.
						// Based on the value type, use the proper 
						// method for working with the value.


				         // If the cell is blank, the cell object is null, so don't 
				         // try to use it.  It will cause errors.
						 // Use continue to skip it and just keep going.
						if (oneCell == null) {
							continue;
						}
						switch (oneCell.getCellType()) {	//获取单元格类型并判断
						
							case HSSFCell.CELL_TYPE_STRING:
								System.out.println(oneCell.getStringCellValue());	
								break;
						
			
							case HSSFCell.CELL_TYPE_FORMULA:
								System.out.println(oneCell.getCellFormula());
								break;

							case HSSFCell.CELL_TYPE_NUMERIC:
								System.out.println(oneCell.getNumericCellValue());
								break;
								
							case HSSFCell.CELL_TYPE_ERROR:
								System.out.println("Error!");
								break;
						
						}	
						

					// End Inner Loop
					}
				// End Middle Loop	
				}
			// End Outer Loop
			}
		// End Try	
		}
		catch (IOException e) {
			System.out.println("Input/Output Exception!");
		}
	

		
//End Main Method		
	}

	
//End Class Definition	
}


转载于:https://my.oschina.net/u/148367/blog/164078

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值