General Excel export class in Java

Source: Internet
Author: User
Tags export class

 

Package com. Ss. util. Excel;

Import java. Lang. annotation. incluented;
Import java. Lang. annotation. elementtype;
Import java. Lang. annotation. retention;
Import java. Lang. annotation. retentionpolicy;
Import java. Lang. annotation. target;

/**
* Attribute annotation of an Excel Object Bean
* @ Author mrgao
* 05:10:37
*/
@ Brief ented
@ Retention (retentionpolicy. runtime)
@ Target (elementtype. Field)
Public @ interface excelannotation {
 
String name (); // name of the Excel Column
Int width (); // Excel column width
Int ID (); // Excel column ID
 
}

Package com. Ss. util. Excel;

Import java. Lang. Reflect. field;
Import java. util. comparator;

@ Suppresswarnings ("unchecked ")
Public class fieldcomparator implements comparator {

Public int compare (Object arg0, object arg1 ){
Field fieldone = (field) arg0;
Field fieldtwo = (field) arg1;
Excelannotation annoone = fieldone. getannotation (excelannotation. Class );
Excelannotation annotwo = fieldtwo. getannotation (excelannotation. Class );
Return annoone. ID ()-annotwo. ID ();
}

}

Package com. Ss. util. Excel;

Import java. AWT. color;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. outputstream;
Import java. Lang. Reflect. field;
Import java. Lang. Reflect. invocationtargetexception;
Import java. Lang. Reflect. method;
Import java. util. arrays;
Import java. util. List;

Import jxl. workbook;
Import jxl. format. alignment;
Import jxl. format. border;
Import jxl. format. borderlinestyle;
Import jxl. format. colour;
Import jxl. format. pattern;
Import jxl. format. RGB;
Import jxl. format. underlinestyle;
Import jxl. format. verticalalignment;
Import jxl. Write. label;
Import jxl. Write. writablecellformat;
Import jxl. Write. writablefont;
Import jxl. Write. writablesheet;
Import jxl. Write. writableworkbook;
Import jxl. Write. writeexception;

/**
* Export an Excel file
*
* @ Author mrgao 02:19:51
*/
Public class excelexport {

/**
* Generate an Excel file
*
* @ Param Models
* Encapsulate data beans everywhere
* @ Param classname
* Name of the bean package imported into excel. Class Name
* @ Param temppath
* Generate a temporary path for storing Excel files
* @ Param excelname
* Generated Excel name
*/
@ Suppresswarnings ("unchecked ")
Public static void createexcel (list models, string classname,
String temppath, string excelname ){
Class clasvo = NULL;

Try {
Clasvo = Class. forname (classname );
Outputstream OS = new fileoutputstream (temppath + "\" + excelname
+ ". Xls ");
Writableworkbook workbook = Workbook. createworkbook (OS );
Writablesheet sheet = Workbook. createsheet (excelname, 0 );
// Used for title
Writablefont titlefont = new writablefont (writablefont. Arial, 17,
Writablefont. Bold, false, underlinestyle. no_underline,
Jxl. format. colour. White );
Writablecellformat wcf_title = new writablecellformat (titlefont );
Wcf_title.setbackground (colour. Teal, pattern. Solid );
Wcf_title.setborder (border. All, borderlinestyle. Double, colour. ocean_blue );
Wcf_title.setverticalalignment (verticalalignment. centre); // Vertical Alignment
Wcf_title.setalignment (alignment. centre );

// Used for text
Writablefont normalfont = new writablefont (writablefont. tahoma, 11 );
Writablecellformat wcf_center = new writablecellformat (normalfont );
Wcf_center.setborder (border. All, borderlinestyle. Double, colour. gray_25 );
Wcf_center.setverticalalignment (verticalalignment. centre); // Vertical Alignment
Wcf_center.setalignment (alignment. centre );
Wcf_center.setwrap (true); // whether to wrap

Sheet. addcell (new label (0, 0, excelname, wcf_title ));
Sheet. mergecells (0, 0, clasvo. getdeclaredfields (). Length-1, 0 );

// Get attributes
Field [] fields = clasvo. getdeclaredfields ();
// Sort the Excel columns by annotation ID
Arrays. Sort (fields, new fieldcomparator ());
For (INT I = 0; I <fields. length; I ++ ){
Field field = Fields [I];
If (field. isannotationpresent (excelannotation. Class )){
// Obtain the annotation object for this field
Excelannotation Anno = Field
. Getannotation (excelannotation. Class );
Sheet. setcolumnview (I, Anno. Width ());
Sheet. addcell (new label (I, 1, Anno. Name (), wcf_center ));
}
}

Int rowid = 2; // write the first row of the row to the first row of the column header data from the second row
For (Object sstopmodel: models ){
Int columnid = 0; // write the number of columns. The first column is the automatically calculated row number. Data is written from the second column.
// Obtain the class and obtain its own method
Class clazz = sstopmodel. getclass ();
For (INT I = 0; I <fields. length; I ++ ){
Field field = Fields [I];
If (field. isannotationpresent (excelannotation. Class )){
String methodname = "get" + field. getname (). substring (0, 1). touppercase () + field. getname (). substring (1 );
Method method = clazz. getmethod (methodname );
Try {
Sheet. addcell (new label (columnid, rowid, Method
. Invoke (sstopmodel) = NULL? ""
: Method. Invoke (sstopmodel)
. Tostring (), wcf_center ));
} Catch (illegalargumentexception e ){
E. printstacktrace ();
} Catch (illegalaccessexception e ){
E. printstacktrace ();
} Catch (invocationtargetexception e ){
E. printstacktrace ();
}
Columnid ++;
}
}
Rowid ++;
}

Workbook. Write ();
Workbook. Close ();
OS. Flush ();
OS. Close ();
} Catch (writeexception e ){
E. printstacktrace ();
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Catch (classnotfoundexception e ){
E. printstacktrace ();
} Catch (securityexception e ){
E. printstacktrace ();
} Catch (nosuchmethodexception e ){
E. printstacktrace ();
}
}
}

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.