Two ways that Java reads Excel files

Source: Internet
Author: User

Way One:

Borrow

 PackageCom.ij34.util;/** * @authoradmin* @date creation time: August 29, 2017 pm 2:07:59 *@version1.0* @type_name MyClass*/ImportJava.io.File; Importjava.io.IOException; ImportJXL.  Cell; ImportJXL.  Sheet; ImportJXL.  Workbook; Importjxl.read.biff.BiffException;  Public classTest05 { Public Static voidMain (String args[]) {File F=NewFile ("Table01.xls"); Try{Workbook Book=workbook.getworkbook (f);//Sheet sheet=book.getsheet (0);//get the first sheet object         for(intI=0;i<sheet.getrows (); i++){               for(intJ=0;j<sheet.getcolumns (); j + +) {cell cell=sheet.getcell (J, I);//Get cellSystem.out.print (cell.getcontents () + ""); } System.out.print ("\ n"); }      } Catch(biffexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }  }  }  

Way two:
 PackageCom.ij34.util;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.ss.usermodel.Cell;ImportOrg.apache.poi.ss.usermodel.DateUtil;ImportOrg.apache.poi.ss.usermodel.Row;/** * @authoradmin* @date creation time: August 29, 2017 pm 4:01:06 *@version1.0* @type_name test02* read xls*/ Public classTest02 { Public Static voidMain (string[] args)throwsFileNotFoundException, IOException {File excelfile=NewFile ("Table01.xls"); Hssfworkbook WB=NewHssfworkbook (NewFileInputStream (excelfile)); Hssfsheet sheet= Wb.getsheetat (0);  for(Row row:sheet) { for(Cell cell:row) {Switch(Cell.getcelltype ()) { CaseCell.cell_type_string://stringSystem.out.print (Cell.getrichstringcellvalue (). getString ()); System.out.print (" ");  Break;  CaseCell.cell_type_numeric://values and dates                 if(dateutil.iscelldateformatted (cell)) {System.out.print (string.valueof (Cell.getdatecellvalue ()))                 ; } Else{System.out.print (Cell.getnumericcellvalue ()); } System.out.print (" ");  Break;  CaseCell.cell_type_boolean://Boolean typeSystem.out.print (Cell.getbooleancellvalue ()); System.out.print (" ");  Break; default:             }         }     System.out.println (); }}}

Jar Package Included

Two ways that Java reads Excel files

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.