Introduction to the Java reflection mechanism 04

Source: Internet
Author: User

Need Jxl.jar

 PackageCom.rainmer.main;ImportJava.io.File;Importjava.io.IOException;Importjava.util.ArrayList;ImportJXL. Cell;ImportJXL. Sheet;ImportJXL. Workbook;ImportJxl.write.Label;ImportJxl.write.WritableSheet;ImportJxl.write.WritableWorkbook;Importjxl.write.WriteException;ImportCom.rainmer.bean.Book; Public classExclebook { Public voidExcleout (arraylist<book>ar) {Writableworkbook Book=NULL;//Excle Object        Try{            //Create a Excle objectBook = Workbook.createworkbook (NewFile ("G:/book.xls")); //Creating a Tab object from a Excle objectWritablesheet sheet = book.createsheet ("sheet", 0);  for(inti = 0; I < ar.size (); i++) {                //Create a Cell objectLabel la0 =NewLabel (0, i,string.valueof (Ar.get (i). GetId ())); Label LA1=NewLabel (1,i,ar.get (i). GetName ());//Create a cell. Parameter description: column, row, valueLabel LA2 =NewLabel (2, I,ar.get (i). GetType ()); //put the created Cell object into the tabSheet.addcell (LA0);                Sheet.addcell (LA1);            Sheet.addcell (LA2); }            //write to Destination pathBook.write (); }Catch(Exception e) {e.printstacktrace (); }finally{            Try{book.close (); } Catch(WriteException |IOException E) {                //TODO auto-generated Catch blockE.printstacktrace (); }        }            }         PublicArraylist<book>Exclein () {ArrayList<Book> AR =NewArraylist<book>(); Workbook Book=NULL; Try{ Book= Workbook.getworkbook (NewFile ("G:/book.xls")); Sheet Sheet= Book.getsheet (0);  for(inti = 0; I < sheet.getrows (); i++) {book Bo=NewBook (); Cell Cell= Sheet.getcell (0,i);//cell, column, row when the cell is readBo.setid (integer.valueof (Cell.getcontents ())); Bo.setname (Sheet.getcell (1, i). getcontents ()); Bo.settype (Sheet.getcell (2, i). getcontents ());            Ar.add (BO); }        }Catch(Exception e) {e.printstacktrace (); }finally{book.close (); }        returnar; }         Public Static voidMain (string[] args) {exclebook Book=NewExclebook (); ArrayList<Book> AR =NewArraylist<book>(); Book Bo1=NewBook (); Bo1.setid (1); Bo1.setname ("The Naruto is reborn."); Bo1.settype (Doujinshi); Book Bo2=NewBook (); Bo2.setid (2); Bo2.setname ("Me and my wife, my lord."); Bo2.settype (Urban);        Ar.add (BO1);                Ar.add (BO2);                Book.excleout (AR); ArrayList<Book> result =Book.exclein ();  for(inti = 0; I < result.size (); i++) {System.out.println (Result.get (i). GetName ()); }    }}

Operation Result:

Called the SetName method
Called the SetName method
Called the GetName method
Called the GetName method
Called the SetName method
Called the SetName method
Called the GetName method
The Naruto is reborn.
Called the GetName method
Me and my wife, my lord.

——————————————————————————————————————————————————————————————————————————

If there is a project that requires you to implement the import and export of book information, user information import and export, borrowing information import and export, maintenance information import and export, management information import and export .... Using reflection can be a good way to implement code reuse.

 PackageCom.rainmer.util;ImportJava.io.File;ImportJava.lang.reflect.Field;Importjava.util.ArrayList;ImportJXL. Sheet;ImportJXL. Workbook;ImportJxl.write.Label;ImportJxl.write.WritableSheet;ImportJxl.write.WritableWorkbook;ImportCom.rainmer.bean.Book; Public classExcleutil {//If there is a project,//ask you to achieve the import and export of books information, user information import and export, borrowing information import and export, maintenance information import and export, management information import and export .....      Public voidexcleout (ArrayList ar, String str) {writableworkbook Book=NULL; Try{ Book= Workbook.createworkbook (NewFile (str)); Writablesheet sheet= Book.createsheet ("sheet", 0);  for(inti = 0;i<ar.size (); i++) {Object ob=Ar.get (i); Class CL=Ob.getclass (); Field[] fi=Cl.getdeclaredfields ();  for(intj = 0;j<fi.length;j++) {fi[j].setaccessible (true); Label La=NewLabel (J, I, String.valueof (Fi[j].get (OB)));                Sheet.addcell (LA);        }} book.write (); } Catch(Exception e) {e.printstacktrace (); }finally{            Try{book.close (); } Catch(Exception e) {e.printstacktrace (); }         }    }         PublicArrayList Exclein (Class cl, String str) {ArrayList ar=NewArrayList (); Workbook Book=NULL; Try{ Book= Workbook.getworkbook (NewFile (str)); Sheet Sheet= Book.getsheet (0); Field[] fi=Cl.getdeclaredfields ();  for(inti = 0; I < sheet.getrows (); i++) {Object ob=cl.newinstance ();  for(intj = 0; J < Fi.length; J + +) {fi[j].setaccessible (true); String Con=Sheet.getcell (J, i). getcontents (); if(Fi[j].gettype (). toString (). Equals ("Class java.lang.String") {fi[j].set (ob, con); }Else if(Fi[j].gettype (). toString (). Equals ("int") {fi[j].setint (ob, integer.valueof (Con));            }} ar.add (OB); }        }Catch(Exception e) {e.printstacktrace (); }        returnar; }         Public Static voidMain (string[] args) {excleutil util=NewExcleutil (); ArrayList<Book> AR =NewArraylist<book>(); Book Bo1=NewBook (); Bo1.setid (1); Bo1.setname ("The Naruto is reborn."); Bo1.settype (Doujinshi); Book Bo2=NewBook (); Bo2.setid (2); Bo2.setname ("Me and my wife, my lord."); Bo2.settype (Urban);        Ar.add (BO1);        Ar.add (BO2); Util.excleout (AR,"G:/booka.xls");

arraylist<book> result = Util.exclein (Book.class, "G:/book.xls");
for (book Book:result) {
System.out.println ("id=" +book.getid () + "; name=" +book.getname () + "; type=" +book.gettype ());
}

    }}

Operation Result:

Called the SetName method
Called the SetName method
Called the GetName method
Id=1;name= the Naruto of the Fire Shadow is reborn; type= people
Called the GetName method
Id=2;name= me and my wife, Type= City.

Introduction to the Java reflection mechanism 04

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.