Two universal Excel export of Java Reflection Learning

Source: Internet
Author: User

First, the implementation of Excel export process

Suppose you have a collection of objects, and now you need to export all of the objects in this collection to Excel, with n properties for the object, and that's how we do it:

Loop this collection, all the properties of an object in the Loop collection, all the properties of this object as columns of Excel, which occupy one line of Excel

Second, universal Excel Export (take JXL as an example, JXL, POI and other Excel import exported jar package does not introduce)

1. Create Java project, introduce JXL jar package

2, directly on the code

Book.java

    

/*** Book Object *@authorBwy **/ Public classBook {PrivateString name; Private DoublePrice ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public DoubleGetPrice () {returnPrice ; }     Public voidSetprice (DoublePrice ) {         This. Price =Price ; }     PublicBook (String name,DoublePrice ) {         This. Name =name;  This. Price =Price ; }    }

Excel Tools Class Excelutil.java

1 /**2 * Excel Tool class3  * 4  * @authorBwy5  *6  */7  Public classExcelutil {8 9     /**Ten * Excel Export Method One      *  A      * @paramList - * Exported collection of objects -      * @paramFilePath the * Export Path -      */ -@SuppressWarnings ("Rawtypes") -      Public Static voidexportexcel (ArrayList list, String filePath) { +  -         //Create a Workbook object +Writableworkbook WorkBook =NULL; A  at         //Sheet Object -Writablesheet sheet =NULL; -         Try { -WorkBook = Workbook.createworkbook (NewFile (FilePath)); -Sheet = Workbook.createsheet ("book", 0); -  in             //loops The collection of the object -              for(inti = 0; I < list.size (); i++) { to  +                 //Get Object -Object obj =List.get (i); the  *                 //gets the class of the object $Class CLS =Obj.getclass ();Panax Notoginseng  -                 //get all the properties of the class thefield[] FIS =cls.getdeclaredfields (); +  A                 //Looping class Properties the                  for(intj = 0; J < Fis.length; J + +) { +  -                     //setting private property access, this step is important $Fis[j].setaccessible (true); $  -                     //Create a label -Label label =NewLabel (J, I, Fis[j].get (obj). toString ()); the  -                     //put into the sheet.Wuyi Sheet.addcell (label); the                 } -             } Wu  -             //Write a file About workbook.write (); $}Catch(Exception e) { - e.printstacktrace (); -}finally { -  A             //Close the stream +             Try { the workbook.close (); -}Catch(WriteException e) { $ e.printstacktrace (); the}Catch(IOException e) { the e.printstacktrace (); the             } the         } -     } in}

Finally, the test class Excelexporttest.java

1  Public classExcelexporttest {2      Public Static voidMain (string[] args) {3Book B1 =NewBook ("The days when I lived with Sister Feng", 32.00);4Book B2 =NewBook ("HTML5 Development Manual", 18.00);5Book B3 =NewBook ("Thiking in Java", 90.0);6Book B4 =NewBook ("Data Structure", 22); 7Book B5 =NewBook ("Mao Zedong Thought", 10);8         9Arraylist<book> Booklist =NewArraylist<book>();Ten Booklist.add (B1); One Booklist.add (B2); A Booklist.add (b3); - Booklist.add (b4); - Booklist.add (B5); the          -Excelutil.exportexcel (Booklist, "C:/export.xls"); -     } -}

Run the test class and you can see that the Export.xls file was generated in the C packing directory

Of course, this export function is not the most complete, but only through this function to understand the use of Java reflection.

Two universal Excel export of Java Reflection Learning

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.