Java Operations Excel table (2003)

Source: Internet
Author: User

 Packagetest;ImportJava.io.BufferedInputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.text.DecimalFormat;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.Date;ImportJava.util.HashMap;Importjava.util.List;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFDateUtil;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.poifs.filesystem.POIFSFileSystem;ImportDemo. HttpRequest; Public classexceloperate{ Public Static voidMain (string[] args)throwsException {File file=NewFile ("D:/12345678.xls"); String[][] Result= GetData (file, 1); intRowlength =result.length;  for(inti=0;i<rowlength;i++) {                for(intj=0;j<result[i].length;j++) {System.out.print (Result[i][j]+ "\t\t");           } System.out.println (); }        }                 Public StaticString[][] GetData (file file,intIgnorerows)throwsFileNotFoundException, IOException {List<String[]> result =NewArraylist<string[]>(); introwsize = 0; Bufferedinputstream in=NewBufferedinputstream (Newfileinputstream (file)); //Open HssfworkbookPoifsfilesystem FS=NewPoifsfilesystem (in); Hssfworkbook WB=NewHssfworkbook (FS); Hssfcell Cell=NULL;  for(intSheetindex = 0; Sheetindex < Wb.getnumberofsheets (); sheetindex++) {Hssfsheet St=Wb.getsheetat (Sheetindex); //first act title, not take                     for(intRowIndex = ignorerows; RowIndex <= st.getlastrownum (); rowindex++) {Hssfrow row=St.getrow (RowIndex); if(Row = =NULL) {                           Continue; }                       intTemprowsize = Row.getlastcellnum () + 1; if(Temprowsize >rowsize) {Rowsize=temprowsize; } string[] Values=NewString[rowsize]; Arrays.fill (values,""); BooleanHasValue =false;  for( Shortcolumnindex = 0; ColumnIndex <= row.getlastcellnum (); columnindex++) {String value= ""; Cell=Row.getcell (columnindex); if(Cell! =NULL) {                              //Note: Must be set to this, or it may appear garbled//cell.setencoding (hssfcell.encoding_utf_16);                                                    Switch(Cell.getcelltype ()) { CaseHSSFCell.CELL_TYPE_STRING:value=Cell.getstringcellvalue ();  Break;  CaseHssfcell.cell_type_numeric:if(hssfdateutil.iscelldateformatted (cell)) {Date Date=Cell.getdatecellvalue (); if(Date! =NULL) {Value=NewSimpleDateFormat ("Yyyy-mm-dd"). Format (date); } Else{Value= ""; }                                  } Else{Value=NewDecimalFormat ("0"). Format (cell. Getnumericcellvalue ()); }                                   Break;  CaseHssfcell.cell_type_formula://no value when data generated for a formula is imported                                  if(!cell.getstringcellvalue (). Equals ("") ) {value=Cell.getstringcellvalue (); } Else{Value= Cell.getnumericcellvalue () + ""; }                                   Break;  CaseHssfcell.cell_type_blank: Break;  CaseHSSFCell.CELL_TYPE_ERROR:value= "";  Break;  CaseHSSFCell.CELL_TYPE_BOOLEAN:value= (Cell.getbooleancellvalue () = =true? Y                                         : N);  Break; default: Value= ""; }                           }                           if(ColumnIndex = = 0 && value.trim (). Equals ("")) {                               Break; } Values[columnindex]=Righttrim (value); HasValue=true; }                                       if(hasValue) {Result.add (values);                }}} in.close (); String[][] ReturnArray=Newstring[result.size ()][rowsize];  for(inti = 0; i < returnarray.length; i++) {Returnarray[i]=(string[]) result.get (i); }                returnReturnArray;} /*** Remove the space to the right of the string *@paramstr to process the string *@returnthe string after processing*/      Public Staticstring Righttrim (String str) {if(str = =NULL) {               return""; }           intLength =str.length ();  for(inti = length-1; I >= 0; i--) {               if(Str.charat (i)! = 0x20) {                   Break; } length--; }           returnStr.substring (0, length); }}

Java Operations Excel table (2003)

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.