Java POI Import Excel file

Source: Internet
Author: User

Today in the company need to do a import Excel file function, so the study, the reference on the Internet some of the information is finally done, in this record to prevent later forget how to get.

I use poi3.8, so I need the jar package as follows:

Poi-3.8.jar

Poi-excelant-3.8-20120326.jar

Poi-ooxml-3.8-20120326.jar

Poi-ooxml-schemas-3.8-20120326.jar

Poi-scratchpad-3.8-20120326.jar

Xmlbeans-2.3.0.jar

Attached to the share address of Baidu Cloud disk, jar package from Apache official: Http://pan.baidu.com/s/1bnzzheR

Attach method details, method from Csdn a blog, the specific address I forgot, it is not original, but on the basis of modified, the code is more readable, in order to let the first time to do this person to understand, so the comments part of the details of the written, the actual use of the time can be deleted, This method can be compatible with XLS and xlsx two formats, and the others have not been tested:

Public String Importexcel () {Try{inputstream InputStream = new FileInputStream ("c:\\users\\administrator\\desktop\\ Test.xls ");//Get Workbook (entire Excel file) Workbook Workbook = Workbookfactory.create (InputStream) based on input stream;// Get first worksheet (first sheet in Excel file) Sheet Sheet = workbook.getsheetat (0);//Get row iterator iterator<row> Rowiterator = Sheet.rowiterator (); while (Rowiterator.hasnext ()) {//Gets the current row row row = (row) rowiterator.next ();//Gets the cell iterator iterator< cell> coliterator = Row.celliterator (); while (Coliterator.hasnext ()) {//Gets the current cell cell cell = (cell) coliterator.next ();//print column number and line number, starting with 0, Excel A1 cell print out is 0:0, B2 cells are printed out as 1:1system.out.print (Cell.getrowindex () + ":" + cell.getcolumnindex () + "");//data switch based on the data type of the cell ( Cell.getcelltype ()) {Case Cell.CELL_TYPE_STRING:System.out.println (Cell.getrichstringcellvalue (). getString ()); Break;case Cell.CELL_TYPE_NUMERIC:if (dateutil.iscelldateformatted (Cell)) {System.out.println ( Cell.getdatecellvalue ());} Else{system.out.println (Cell.getnumericcellvalue ());} Break;case Cell.cell_type_booleAN:System.out.println (Cell.getbooleancellvalue ()); Break;case Cell.CELL_TYPE_FORMULA:System.out.println ( Cell.getcellformula ()); BREAK;DEFAULT:SYSTEM.OUT.PRINTLN ("null");}}} catch (Exception e) {e.printstacktrace ();} Return "";}

Java POI Import Excel file

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.