Export database any table data as Excel

Source: Internet
Author: User
Tags dateformat

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;
}

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.