Using Apache POI to read Excel files

Source: Internet
Author: User

The Apache POI is an open source library of the Apache Software Foundation to help Java programs read and write to Microsoft Office format files. POI provides the following types for parsing Microsoft Office format files:

HSSF-provides the ability to read and write Microsoft Excel xls format files.

XSSF-provides the ability to read and write Microsoft Excel OOXML xlsx format files.

HWPF-provides the ability to read and write Microsoft Word doc format files.

HSLF-provides the ability to read and write Microsoft PowerPoint format files.

HDGF-provides the ability to read Microsoft Visio format files.

HPBF-provides the ability to read Microsoft Publisher format files.

You can visit POI's homepage http://poi.apache.org/download your favorite version and learn more about it.

This only describes using POI to read Excel files, first locate the Excel file location when reading Excel, and then generate a worksheet Hssfworkbook object through the POI API:

File file = new File(filePath);
     FileInputStream fint = new FileInputStream(file);
     POIFSFileSystem poiFileSystem = new POIFSFileSystem(fint);
     HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem);

The corresponding child worksheet Hssfsheet can be accessed through the getsheetat (int sheetnum) provided by Hssfworkbook, starting with ' 0 '. After obtaining the Hssfsheet object through Sheet.getrow (rownum) method to obtain the specified row of the Hssfrow,hssfrow provides a Getcell (short) method to access the Cell object. When working with cells, be aware that you cannot simply use the Getstringcellvalue of Hssshell () Method gets the value in the cell, and in the version I use (2.0) does not provide the function of automatic type conversion, so determine the value based on the type:

public static String getCell(HSSFCell cell) {
         if (cell == null)
             return "";
         switch (cell.getCellType()) {
           case HSSFCell.CELL_TYPE_NUMERIC:
               return cell.getNumericCellValue() + "";
           case HSSFCell.CELL_TYPE_STRING:
               return cell.getStringCellValue();
           case HSSFCell.CELL_TYPE_FORMULA:
               return cell.getCellFormula();
           case HSSFCell.CELL_TYPE_BLANK:
               return "";
           case HSSFCell.CELL_TYPE_BOOLEAN:
               return cell.getBooleanCellValue() + "";
           case HSSFCell.CELL_TYPE_ERROR:
              return cell.getErrorCellValue() + "";
          }
          return "";
       }

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.