Java reads all the data from the xlsx file

Source: Internet
Author: User

The jar used in this demo. (Click to enter)

Java:

 Packagecom.test;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFDataFormat;ImportOrg.apache.poi.ss.usermodel.*;ImportOrg.apache.poi.xssf.usermodel.XSSFSheet;ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.text.DecimalFormat;ImportJava.text.SimpleDateFormat;Importjava.util.Date;/*** Created by Wdnnccey on 17/2/27. * <p> * <p> * February 27, 2017 21:27:26, **/ Public classTESTXLSX2 { Public Static voidMain (string[] args) {File excelfile=NewFile ("/users/wdnnccey/desktop/test/test.xlsx"); Replace your document address Xssfworkbook WB=NULL; Try{WB=NewXssfworkbook (NewFileInputStream (excelfile)); } Catch(IOException e) {e.printstacktrace (); }        intNumberofsheets =wb.getnumberofsheets (); String Str= "";  for(intx = 0; x < numberofsheets; X + +) {Xssfsheet sheet=Wb.getsheetat (x); intColumnnum = 0; if(Sheet.getrow (0)! =NULL) {Columnnum= Sheet.getrow (0). Getlastcellnum ()-Sheet.getrow (0). Getfirstcellnum (); }            if(Columnnum > 0) {                 for(Row row:sheet) {string[] singlerow=NewString[columnnum]; intn = 0;  for(inti = 0; i < Columnnum; i++) {cell cell=Row.getcell (i, Row.create_null_as_blank); Switch(Cell.getcelltype ()) { CaseCell.cell_type_blank:singlerow[n]= ""; if(Cell = =NULL|| Cell.equals ("") | | Cell.getcelltype () = =Hssfcell.cell_type_blank) {System.out.print ("<null>|"); } Else{System.out.print (Singlerow[n]+ "|"); }                                 Break;  CaseCell.cell_type_boolean:singlerow[n]=boolean.tostring (cell. Getbooleancellvalue ()); System.out.print (Singlerow[n]+ "|");  Break; //value                             CaseCell.cell_type_numeric:if(dateutil.iscelldateformatted (cell)) {SimpleDateFormat SDF=NULL; if(Cell.getcellstyle (). getDataFormat () = =Hssfdataformat. Getbuiltinformat ("H:mm") ) {SDF=NewSimpleDateFormat ("hh:mm"); } Else{//DateSDF =NewSimpleDateFormat ("Yyyy-mm-dd"); } Date Date=Cell.getdatecellvalue (); System.out.print (Sdf.format (date)+ "|"); } Else{cell.setcelltype (cell.cell_type_string); String Temp=Cell.getstringcellvalue (); //determines whether a decimal point is included, if it does not contain a decimal point, is read as a string and converted to a string of type double if it contains a decimal point                                    if(Temp.indexof (".") >-1) {Singlerow[n]= String.valueof (NewDouble (temp)). Trim (); System.out.print (Singlerow[n]+ "|"); } Else{Singlerow[n]=Temp.trim (); System.out.print (Singlerow[n]+ "|"); }                                }                                 Break;  CaseCell.cell_type_string:singlerow[n]=cell.getstringcellvalue (). Trim (); System.out.print (Singlerow[n]+ "|");  Break;  CaseCell.cell_type_error:singlerow[n]= ""; System.out.print (Singlerow[n]+ "|");  Break;  CaseCell.CELL_TYPE_FORMULA:cell.setCellType (cell.cell_type_string); String Temp=Cell.getstringcellvalue (); //determines whether a decimal point is included, if it does not contain a decimal point, is read as a string and converted to a string of type double if it contains a decimal point                                if(Temp.indexof (".") >-1) {Temp= String.valueof (NewDouble (temp)). Trim (); Double CNY= double.parsedouble (temp);//6.2041DecimalFormat DF =NewDecimalFormat ("0.00"); String CNY=Df.format (CNY); System.out.print (CNY+ "|"); } Else{Singlerow[n]=Temp.trim (); System.out.print (Singlerow[n]+ "|"); }                            default: Singlerow[n]= "";  Break; } N++;                } System.out.println (); } System.out.println ("===========================================================sheet Split Line =========================================== ================"); }        }    }}

Java reads all the data from the xlsx file

Related Article

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.