Poi is an API provided by Apache for processing files such as Excel.
1. Download the JAR File
Http://poi.apache.org/download.html#POI-3.7
The current stable version is 3.7.
Development and download in window:
Poi-bin-3.7-20101029.zip
2. decompress the package.
3. Create a project under eclipse.
Import the following JAR files to the project.
Jar files in the root directory
Jar files under the lib directory
Ooxml-libooxml-lib JAR File
4. Test the class
package poi;import java.io.FileInputStream;import java.util.Iterator;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 POITest {private static void ReadAndPrintExcelFile(String filePath, String sSheetName) {try {FileInputStream fis = new FileInputStream(filePath);XSSFWorkbook wb = new XSSFWorkbook(fis);XSSFSheet sheet = wb.getSheet(sSheetName);for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {String cellNovalue = "";XSSFRow row = sheet.getRow(i);Iterator it = row.cellIterator();while (it.hasNext()) {XSSFCell cell = (XSSFCell) it.next();try {cellNovalue = cell.getStringCellValue();} catch (IllegalStateException e) {try {double dcellNovalue = sheet.getRow(i).getCell(0).getNumericCellValue();cellNovalue = String.valueOf(dcellNovalue);} catch (IllegalStateException e2) {cellNovalue = "";e.printStackTrace();}} catch (Exception e3) {cellNovalue = "";e3.printStackTrace();}System.out.println("Row=" + i + "; Cell="+ cell.getColumnIndex() + "; Value=" + cellNovalue);}}} catch (Exception e) {e.printStackTrace();}}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubReadAndPrintExcelFile("E:\\1.xlsx","test");}}
Note:
Xssfworkbook can only parse Versions later than 2007 in Excel (with the XLSX suffix ),
To parse an Excel file in the xls Format of 2003, use hssfworkbook