poi-iterating through the rows and columns of a workbook gets the contents of the cell

Source: Internet
Author: User
Preface

This article mainly reads the Excel document and reads the contents by traversing the rows and columns. Where the content type of the cell needs to be read separately. Example

public static void Main (string[] args) throws Exception
{
  //input stream
  InputStream is = new FileInputStream ("f:\\ Poi\\ffff.xls ");
  File System
  Poifsfilesystem fs = new Poifsfilesystem (is);
  Define Workbook
  Hssfworkbook wb = new Hssfworkbook (fs);
  Gets the first sheet page
  hssfsheet hssfsheet = wb.getsheetat (0);
  if (Hssfsheet = = null) {
    return;
  }
  
  Traverse rows row
  for (int rowNum = 0; Rownum<=hssfsheet.getlastrownum (); rownum++) {
    //Get each row
    hssfrow row = Hssfsheet.getrow (rowNum);
    if (row = = null) {
      continue;
    }
    
    Traverse column cell
    for (int cellnum = 0; Cellnum<=row.getlastcellnum (); cellnum++) {
      //Get each column
      Hssfcell cell = Row.getcell (cellnum);
      if (cell = = null) {
        continue;
      }
      
      System.out.print ("" +getvalue (cell));
    }
    System.out.println ();
  }
  
}
/**
 * Different values range for different types
 * @param cell
 * @return *
/private static String GetValue (Hssfcell cell) {
  if (Cell.getcelltype () ==hssfcell.cell_type_boolean) {
    return string.valueof (Cell.getbooleancellvalue ());
  } else if (Cell.getcelltype () ==hssfcell.cell_type_numeric) {
    return string.valueof (Cell.getnumericcellvalue ());
  }
  
    return string.valueof (Cell.getstringcellvalue ());
}

Summary: Here the method of reading the value is not very perfect, read the value of the number, the integer will read out the decimal point.



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.