Read a summary of the POI read and write Excel

Source: Internet
Author: User

First, the POI read Excel file (in Excel2003 version, for example, version 2007 is based on the file extension xlsx will hssfworkbook to Xssfworkbook, and its sheet, Row, cell and corresponding replacement)

FilePath full path to Excel file

1.//Create a File object

File File = new file (FilePath);

2,//To determine whether the file exists, there is no direct exit function

if (!file.exists ()) {return null}

3.//Read the file into the file stream

InputStream InputStream = new FileInputStream (file);

4.//Create a Hssfworkbook object and save the file stream in the object

Hssfworkbook Hssfworkbook = new Hssfworkbook (InputStream);

5,//Get Excel File Sheet page, you can get the number of all sheet pages in the file by Hssfworkbook.getnumberofsheets ()

Take only the first one below

Hssfsheet sheet = hssfworkbook.getsheetat (0);

6,///Get the data row in sheet page, can get the last line number of data row in the file by Sheet.getlastrownum ()

Take only the first line below

Hssfrow row = sheet.getrow (0);

7.//Get each row of data cells

The following gets the first cell

Hssfcell cell0 = Row.getcell (0);

8,//based on the cell data type to obtain the corresponding data type value

Take the numeric type as an example cell.getcelltype () = = Cell. Cell_type_numeric

String value=string.valueof (Cell.getnumericcellvalue ());

9.//Release Resources

Inputstream.close ();

Hssfworkbook. Close ();

10, it is best to use try Catch

Read a summary of the POI read and write Excel

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.