Java poi reading Excel (compatible)

Source: Internet
Author: User

I. Latest poi Toolkit

Http://www.apache.org/dyn/closer.cgi/jakarta/poi/

See the documentation

Poi home: http://jakarta.apache.org/poi,

Excel Structure

Hssfworkbook Excell object Introduction
Hssfsheet Excell form
Hssfrow Excell row
Lattice unit of hssfcell Excell
Hssffont Excell font
Hssfname
Hssfdataformat Date Format
Poi1.7 contains the following two items:
Hssfheader sheet Header
End of hssffooter Sheet
And this style
Hssfcellstyle cell style
Auxiliary operations include
Hssfdateutil date
Hssfprintsetup print
Hssferrorconstants error information table

Hssfworkbook: indicates the operation of excel20031. the extension name is .xls.

Xssfworkbook: it is the worker that operates the excel2007, and the extension name is .xlsx /**

Example

/*** Read Excel (compatible with 2003, 2007) ** @ Param filepath */Public hashmap <integer, object []> loadxlsx (string filepath) {// construct the xssfworkbook object, and pass the strpath to the file path workbook = NULL; try {file = new file ("D:/poitest .xlsx"); workbook = workbookfactory. create (File);} catch (exception e) {system. out. println ("file reading error"); E. printstacktrace ();} // read the table content in Chapter 1 Sheet sheet = workbook. getsheetat (0); // xwb. getsheetat (1 );// Define row and cellrow row; cell; stringbuffer sb = NULL; // int totalrow = sheet. getlastrownum (); // total number of rows, including the middle empty row int totalrow = sheet. getphysicalnumberofrows (); // total number of rows, excluding the intermediate empty line int totalcell = 0; // The number of data columns hashmap <integer, object []> map = new hashmap <integer, object []> (); // the content in the loop output table for (INT I = sheet. getfirstrownum (); I <totalrow; I ++) {ROW = sheet. getrow (I); If (row! = NULL) {totalcell = row. getphysicalnumberofcells (); object [] dataary = new object [totalcell]; for (Int J = row. getfirstcellnum (); j <totalcell; j ++) {sb = new stringbuffer (); cell = row. getcell (j); Switch (cell. getcelltype () {Case xssfcell. cell_type_numeric: // number sb. append (cell. getnumericcellvalue (); break; Case xssfcell. cell_type_string: // string sb. append (cell. getstringcellvalue (); break; Case xssfcell. cell_type_formula: // formula sb. append (cell. getcellformula (); break; Case xssfcell. cell_type_blank: // formula sb. append (""); break; default: break;} dataary [J] = cell. tostring (); // No matter what type, convert all to string processing // system. out. print (cell + "\ t");} map. put (I, dataary);} // system. out. println ("");} return map ;}

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.