Prepare the required parameters first
Parameter 1:string title= "user Information"
Parameter 2:string[] Headers
String[] headers = {"User name", "nickname", "department", "User description", "gender", "responsibility", "address", "Fax", "Mail", "Home Phone", "mobile phone", "office Phone", "User Priority"};
Parameter 3:list<t> DataSet:
list<user> list = new arraylist<user> ();
if (! Stringutils.isempty (batchexp) && "yes". Equals (Batchexp)) {//If the quantity is greater than 500, export in batches of 500
int startIndex = Integer.parseint (Request.getparameter ("StartIndex"));
List = Usermanager.getallorgusersnoadmin (Buildsearch (), startindex,500);
}else{
List = Usermanager.getallorgusersnoadmin (Buildsearch (), -1,-1);
}
Parameter 4:list<string> exportfile
list<string> exportfile = new arraylist<string> ();
Exportfile.add ("LoginName"); Exportfile.add ("Realname");
Exportfile.add ("Depname");
Exportfile.add ("Userdescription");
Exportfile.add ("Filegender"); Exportfile.add ("Duty"); Exportfile.add ("useraddress");
Exportfile.add ("Userfax"); Exportfile.add ("UserEmail"); Exportfile.add ("Userhometel");
Exportfile.add ("Usermobiletel"); Exportfile.add ("Userofficetel"); Exportfile.add ("userpriority");
Parameter 5 OutputStream out
Parameter 6 String pattern
public void Exportexcel (String title, string[] headers,
List<t> dataset,list<string> Exportfile, outputstream out, String pattern) {
Declaring a working book
Hssfworkbook workbook = new Hssfworkbook ();
Create a table
Hssfsheet sheet = workbook.createsheet (title);
Set table Default column width is 15 bytes
Sheet.setdefaultcolumnwidth ((short) 15);
Build and set another style
Hssfcellstyle style2 = Workbook.createcellstyle ();
Style2.setverticalalignment (Hssfcellstyle.vertical_top);
Declaring a paint top-level manager
Hssfpatriarch Patriarch = Sheet.createdrawingpatriarch ();
Generate table header row
Hssfrow row = sheet.createrow (0);
for (short i = 0; i < headers.length; i++) {
Hssfcell cell = Row.createcell (i);
hssfrichtextstring Text = new hssfrichtextstring (headers[i]);
Cell.setcellvalue (text);
}
Iterating through the collection data, producing rows of data
Iterator<t> it = Dataset.iterator ();
int index = 0;
while (It.hasnext ()) {index++;
row = Sheet.createrow (index);
T t = (t) it.next ();
int j = 0;
for (short i = 0; i < exportfile.size (); i++) {
try {
String name = Exportfile.get (i);
Object value = Beanutils.getproperty (t, name);
Hssfcell cell = Row.createcell (j);
Cell.setcellstyle (Style2);
j + +;
Coercion type conversion After judging the type of the value
String TextValue = null;
/*if (value instanceof Date) {
Date date = (date) value;
SimpleDateFormat SDF = new SimpleDateFormat (pattern);
TextValue = Sdf.format (date);
} else {*/
Other data types are simple to handle as strings
if (null! = value)
TextValue = Value.tostring ();
//}
If it is not picture data, use regular expressions to determine whether the TextValue are all made up of numbers
if (TextValue! = null) {
Pattern p = pattern.compile ("^//d+ (//.//d+)? $");
Matcher Matcher = P.matcher (TextValue);
if (Matcher.matches ()) {
is a number treated as double
Cell.setcellvalue (Double.parsedouble (TextValue));
}else if (name.equals ("Qrcodepath")) {
The property named Qrcodepath is the picture path information
Bytearrayoutputstream bytearrayout = new Bytearrayoutputstream ();
BufferedImage bufferimg = Imageio.read (new File (TextValue));
Imageio.write (bufferimg, "PNG", bytearrayout);
When there is a picture, set the row height to 327px;
Row.setheightinpoints (327);
Set the image column width to 195px, note a conversion of the unit here
Sheet.setcolumnwidth (i, (short) (40 * 195));
Sheet.autosizecolumn (i);
Hssfclientanchor anchor = new Hssfclientanchor (0, 0, 680, up, (short) I, Index, (short) I, index);
Hssfpicture pic = patriarch.createpicture (anchor, Workbook.addpicture (Bytearrayout.tobytearray (), Hssfworkbook.picture_type_png));
Pic.resize ();
}else {
hssfrichtextstring richstring = new hssfrichtextstring (TextValue);
Hssffont Font3 = Workbook.createfont ();
Font3.setcolor (HSSFColor.BLACK.index);
Richstring.applyfont (FONT3);
Cell.setcellvalue (richstring);
}
}
} catch (Exception e) {
E.printstacktrace ();
} finally {
}
}
}
try {
Workbook.write (out);
} catch (IOException e) {
E.printstacktrace ();
}
}
public void Exportexcel (String sheetname, string[] headers,
collection<t> DataSet, List<string> Exportfile,
HttpServletResponse response) throws IOException {
OutputStream out = null;
Response.setcontenttype ("application/vnd.ms-excel");
Response.setcharacterencoding ("GBK");
Response.setheader ("Content-disposition", "Attachment;filename="
+ New String (Sheetname.getbytes ("GBK"), "iso-8859-1") + ". xls");
out = Response.getoutputstream ();
Hssfworkbook workbook = new Hssfworkbook ();
Createsheet (SheetName, headers, DataSet, Exportfile, Out, "Yyyy-mm-dd",
Workbook);
try {
Workbook.write (out);
} catch (IOException e) {
E.printstacktrace ();
}
Out.flush ();
Out.close ();
}
An API for POI export