public class Exportasexcel {
Final Logger Logger = Loggerfactory.getlogger (GetClass ());
Static SimpleDateFormat dateformat=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
/**
* Common components, export Excel tabular data method <p>
* Usage: Data is list<object> type, note that the attribute of Object must theoretically be Java basic data type
* It also supports format like date, that is, the field type in the full support database
* @author Pery
* @param the class full name of the ClassName data model object
*, Eg:com.swust.kelab.Department
* @param the column names for the properties of the PropertyName data Model class,
* such as "depaid" corresponding list "agency ID",
* Properties that do not specify a column name are not exported
* @param tableName The name of the table in the exported Excel
* @param record the absolute path of the exported file (must be an existing file, in this method does not do file path check)
* @param objectList the dataset to be exported
* @return successfully returns TRUE, the failure returns false;
*/
public static Boolean Exportexcel (String classname,map propertyname,string tablename,file record,list<? extends Object> objectList) {
Hssfworkbook Wbook = new Hssfworkbook ();
Hssfsheet Wsheet = Wbook.createsheet (tableName); Table name
Hssfcellstyle Cellstyle=getstyle (Wbook);
Set up an Excel table header
Hssfrow exceltitle = wsheet.createrow (0);
Exceltitle.setheightinpoints (22);
if (Objectlist.size () <=0) {
Wsheet.autosizecolumn (0);
Hssfcell Titlecell = Exceltitle.createcell (0);
Titlecell.setcellvalue ("Sorry, no data export available");
Titlecell.setcellstyle (CellStyle);
}
Class c = null;
try {
c = Class.forName (ClassName);
}
catch (ClassNotFoundException E1) {
System.out.println ("Wrong class name used in the piercing reflex");
E1.printstacktrace ();
return false;
}
java.lang.reflect.field[] Flds = C.getdeclaredfields ();
int j=0;
for (int i = 0; i < flds.length; i++) {
String Columname=flds[i].getname ();
if (Propertyname.containskey (Columname)) {
Wsheet.setcolumnwidth (j+1, (int) (100*35.7));
Hssfcell Titlecell = Exceltitle.createcell (j + +);
Titlecell.setcellvalue (String) propertyname.get (columname));
Titlecell.setcellstyle (CellStyle);
}
}
for (int i = 0; i < objectlist.size (); i++) {
Object obj = Objectlist.get (i);
Hssfrow row = Wsheet.createrow (i + 1);
int k=0;
for (j = 0; J < Flds.length; J + +) {
String Columname=flds[j].getname ();
if (!propertyname.containskey (Columname)) {
Continue
}
Hssfcell Hssfcell = Row.createcell (k++);
try {
Flds[j].setaccessible (TRUE);
Object t = flds[j].get (obj);
if (t instanceof Date) {
t = Dateformat.format (t);
}
if (t!=null) {
Hssfcell.setcellvalue (string.valueof (t));
}
} catch (IllegalArgumentException e) {
System.out.println ("This field is not a basic data format field");
E.printstacktrace ();
} catch (Illegalaccessexception e) {
SYSTEM.OUT.PRINTLN ("illegal access");
E.printstacktrace ();
try {
FileOutputStream fOut = new FileOutputStream (record);
Wbook.write (FOut);
Fout.flush ();
Fout.close ();
} catch (FileNotFoundException e) {
System.out.println ("File path error");
E.printstacktrace ();
} catch (IOException e) {
SYSTEM.OUT.PRINTLN ("I/O error");
E.printstacktrace ();
}
return true;
* Excel Table Format Property adjustment
* @param wbook
* @return
*/http://www.huiyi8.com/moban/html templates
private static Hssfcellstyle GetStyle (Hssfworkbook wbook) {
Hssfcellstyle CellStyle = Wbook.createcellstyle ();
Cellstyle.setborderleft (hssfcellstyle.border_double);
Cellstyle.setborderright (hssfcellstyle.border_double);
Cellstyle.setbordertop (hssfcellstyle.border_double);
Cellstyle.setborderbottom (hssfcellstyle.border_double);
Hssffont font = Wbook.createfont ();
Font.setfontheightinpoints ((short) 12);
Font.setboldweight (Hssffont.boldweight_bold);
Cellstyle.setfont (font);
return cellstyle;
}