To read a excle file using the POI component

Source: Internet
Author: User


The key code is as follows:

public void Importpostgroupquality () throws Exception {//inputstream fis=new fileinputstream ("e:\\ employee annual assessment. xls");         InputStream Fis=this.path (); Parse out the data of Excel to store the list of the entities//parse out every data in Excel encapsulated entity try {//create Excel workbook Hssfworkbook                      HWB = new Hssfworkbook (FIS);              Get the first worksheet hssfsheet sheet = hwb.getsheetat (0);   Hssfrow row = null;            System.out.println ("Altogether:" +HWB.GETNU);                            Iterating through all the worksheets in the table, I represents the number of worksheets Getnumberofsheets represents the total number of worksheets for (int i = 0; i < hwb.getnumberofsheets (); i++) {                Sheet = Hwb.getsheetat (i);                System.out.println ("Altogether how many lines:" +sheet.getphysicalnumberofrows ()); Traverse all rows of the row, J represents the total number of rows getphysicalnumberofrows for (int j = 1; J < Sheet.getphysicalnumberofrows ();                    {row = Sheet.getrow (j);  System.out.println ("A total column:" +row.getphysicalnumberofcells ());                  int Total=row.getphysicalnumberofcells (); for (int a=0;a<total;a++) {if (Getcellvalue (Row.getcell (a))! = "") {System.out.print (                                        Getcellvalue (Row.getcell (a)) + ":");            }}}}} catch (Exception e) {            Fis.close ();          E.printstacktrace ();          }}//To determine the format of the data parsed from the Excel file public String getcellvalue (Hssfcell cell) {string value = null;                  Simple search column type switch (Cell.getcelltype ()) {case hssfcell.cell_type_string://string                  Value = Cell.getrichstringcellvalue (). getString ();              Break                  Case hssfcell.cell_type_numeric://digit long DD = (long) cell.getnumericcellvalue ();                  Value = dd+ "";              Break           Case Hssfcell.cell_type_blank:       Value = "";                 Break                  Case HSSFCell.CELL_TYPE_FORMULA:value = string.valueof (Cell.getcellformula ());              Break                  Case Hssfcell.cell_type_boolean://boolean value = string.valueof (Cell.getbooleancellvalue ());            Break                  Case HSSFCell.CELL_TYPE_ERROR:value = string.valueof (Cell.geterrorcellvalue ());              Break          Default:break;      } return value; }



Among them InputStream Fis=this.path (); Get the file stream, modify according to the actual situation, get the input stream code of the local file is: InputStream fis=new fileinputstream ("e:\\ employee annual assessment. xls");

To read a excle file using the POI component

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.