23 Reading Excel

Source: Internet
Author: User

    /** Index mappingname originalname datatype family 0 task_id t     Askindexcode STRING Info 1 task_type resourcedesctype INT32 Info 2 Camera_index_code cameraindexcode STRING info 3 camera_address Resourcelo Cation STRING Info * @param path * @return * @throws IOException*/     Public StaticList<string>readhbasexlsx (String path) throws IOException {InputStream is=NewFileInputStream (path); Xssfworkbook Xssfworkbook=NewXssfworkbook ( is); List<String> jsonlist =NewArraylist<>(); intSheetnum =xssfworkbook.getnumberofsheets (); //Read the Sheet         for(intNumsheet =0; Numsheet < Sheetnum; numsheet++) {Xssfsheet Xssfsheet=Xssfworkbook.getsheetat (Numsheet); if(Xssfsheet = =NULL) {                Continue; }            //Read the Row             for(intRowNum =1; RowNum <= xssfsheet.getlastrownum (); rownum++) {Xssfrow Xssfrow=Xssfsheet.getrow (RowNum); if(Xssfrow! =NULL) {Xssfcell Indexcell= Xssfrow.getcell (0); if(Indexcell = =NULL)Continue; Xssfcell Mappingnamecell= Xssfrow.getcell (1); Xssfcell Originalnamecell= Xssfrow.getcell (2); Xssfcell Datatypecell= Xssfrow.getcell (3); Xssfcell Familycell= Xssfrow.getcell (4); String Index=Indexcell.getstringcellvalue ();                    String Originalname; Try{originalname=Originalnamecell.getstringcellvalue (); }Catch(NullPointerException e) {originalname=""; }                    if(NULL==originalname) {Originalname=""; } String MappingName=Mappingnamecell.getstringcellvalue (); String datatype=Datatypecell.getstringcellvalue (); String Family=Familycell.getstringcellvalue (); String JSON=index+","+originalname+","+mappingname+","+datatype+","+family;                Jsonlist.add (JSON);        }} xssfworkbook.clonesheet (Numsheet); }         is. Close (); returnjsonlist; }    /** Index mappingname originalname datatype family 0 task_id t     Askindexcode STRING Info 1 task_type resourcedesctype INT32 Info 2 Camera_index_code cameraindexcode STRING info 3 camera_address Resourcelo Cation STRING Info * @param path * @return * @throws IOException*/     Public StaticList<string>readesxlsx (String path) throws IOException {InputStream is=NewFileInputStream (path); Xssfworkbook Xssfworkbook=NewXssfworkbook ( is); List<String> jsonlist =NewArraylist<>(); intSheetnum =xssfworkbook.getnumberofsheets (); //Read the Sheet         for(intNumsheet =0; Numsheet < Sheetnum; numsheet++) {Xssfsheet Xssfsheet=Xssfworkbook.getsheetat (Numsheet); if(Xssfsheet = =NULL) {                Continue; }            //Read the Row             for(intRowNum =1; RowNum <= xssfsheet.getlastrownum (); rownum++) {Xssfrow Xssfrow=Xssfsheet.getrow (RowNum); if(Xssfrow! =NULL) {Xssfcell Indexcell= Xssfrow.getcell (0); if(Indexcell = =NULL)Continue; Xssfcell Mappingnamecell= Xssfrow.getcell (1); Xssfcell Originalnamecell= Xssfrow.getcell (2); Xssfcell Datatypecell= Xssfrow.getcell (3); String Index=Indexcell.getstringcellvalue ();                    String Originalname; Try{originalname=Originalnamecell.getstringcellvalue (); }Catch(NullPointerException e) {originalname=""; }                    if(NULL==originalname) {Originalname=""; } String MappingName=Mappingnamecell.getstringcellvalue (); String datatype=Datatypecell.getstringcellvalue (); String JSON=index+","+originalname+","+mappingname+","+datatype;                Jsonlist.add (JSON);        }} xssfworkbook.clonesheet (Numsheet); }         is. Close (); returnjsonlist; }    /** * @param xssfrow Excel cell * @return cell data * @Method Description 2010excel cells data, cell data is divided into: Boolean, numeri C, String*/    Private StaticObject getValue (Xssfcell xssfrow) {if(Xssfrow.getcelltype () = =Xssfrow.cell_type_boolean) {            returnXssfrow.getbooleancellvalue (); } Else if(Xssfrow.getcelltype () = =xssfrow.cell_type_numeric) {            returnXssfrow.getnumericcellvalue (); } Else {            returnXssfrow.getstringcellvalue (); }    }

Write the string to the file

 Public Staticboolean writestringtofile (String filepath,string str) {Boolean success=true; if(!NewFile (FilePath). Exists ()) {            Try {                NewFile (FilePath). CreateNewFile (); } Catch(IOException e) {e.printstacktrace (); }        }Else{            NewFile (filePath). Delete (); Try {                NewFile (FilePath). CreateNewFile (); } Catch(IOException e) {e.printstacktrace (); }} FileWriter FileWriter=NULL; Try{FileWriter=NewFileWriter (FilePath); } Catch(IOException e) {e.printstacktrace (); }        if(NULL!=fileWriter) {            Try{filewriter.write (str); } Catch(IOException e) {e.printstacktrace (); }finally {                Try{filewriter.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }        returnsuccess; }

23 Reading Excel

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.