Java poi read Excel-from start to instance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.