所需Jar包:poi-3.9-20121203.jar
Excel文件存放路徑:C:/Planet Power/demo.xls,這個能夠根據你本身想要讀取文件的路徑而改變, java
修改這條語句中的路徑便可: apache
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 }