public class Filezip {
/**
*
* @param srcfile file name array
* @param zipfile Compressed file
*/
public static void Zipfiles (file[] srcfile, File zipfile) {
byte[] buf = new byte[1024];
try {
Zipoutputstream out = new Zipoutputstream (New FileOutputStream (
ZipFile));
for (int i = 0; i < srcfile.length; i++) {
FileInputStream in = new FileInputStream (Srcfile[i]);
Out.putnextentry (New ZipEntry (Srcfile[i].getname ()));
int Len;
while (len = In.read (buf)) > 0) {
Out.write (buf, 0, Len);
}
Out.closeentry ();
In.close ();
}
Out.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
@RequestMapping ("/exportcoursedataall.do")
Public String Exportcoursedatalistall (httpservletrequest req, httpservletresponse res) throws IOException {
String StartDate = Req.getparameter ("StartDate"); Start date
String endDate = Req.getparameter ("EndDate");End Date
String reporttype = Req.getparameter ("reporttype");Weekly Monthly: ' Day ', ' Week ', ' month '.
String companyName = Req.getparameter ("CompanyName");Company Name
Can only export yesterday's course user learning details, date empty
if (StartDate = = NULL | | endDate = = NULL) {
StartDate = datetimeutils.getdatestring ("Yyyy-mm-dd", Calendar.date,-1);
EndDate = datetimeutils.getdatestring ("Yyyy-mm-dd", calendar.date, 0);
}
map<string, object> params = new hashmap<string, object> ();
Params.put ("StartDate", StartDate);
Params.put ("EndDate", endDate);
Params.put ("Reporttype", reporttype);
Params.put ("CompanyName", companyName);
Res.setcontenttype ("Application/octet-stream;charset=utf-8");
String fileName = datetimeutils.formatdateno_ (New Date ()) + "User detail data";
Req.setcharacterencoding ("UTF-8");
Res.setcharacterencoding ("UTF-8");
Res.setheader ("Content-disposition", "Attachment;filename="
+ Java.net.URLEncoder.encode (fileName, "UTF-8")
+ ". zip");
Res.addheader ("Pargam", "No-cache");
Res.addheader ("Cache-control", "No-cache");
When the data volume is large, the paging export data
Org.extremecomponents.table.context.Context context = new Httpservletrequestcontext (req);
Org.extremecomponents.table.limit.LimitFactory limitfactory = new Tablelimitfactory (context);
Limit limit = new Tablelimit (limitfactory);
OutputStream out = null;
try {
int totalrows = Limit.gettotalrows ();
list<coursedetaildto> Listall = new arraylist<coursedetaildto> ();
Totalrows = Wholedataservice.getcoursedetaillistforpagecount (params);
int pageSize = 5000;
int totalpage = integer.valueof (Commonutility.calctotalpage (PageSize, totalrows));
for (int i = 0; i < totalpage; i++) {
Params.put ("PageNo", (i+1));
Params.put ("PageSize", pageSize);
list<coursedetaildto> listpage = Wholedataservice.getcoursedetailall (params);
Listall.addall (Listpage);
}
Write Data
out = Res.getoutputstream ();
Toexcel (listall,req,50000,filename,out);
} catch (Exception e) {
Logger.error ("Export user detail exception", e);
}
return null;
}
/**
* @Title: Toexcel
* @Description: TODO (Compress export data in Excel table)
* @ 2015-8-18 5:29:26
* @param list
* @param request
* @param length
* @param F
* @param out
* @throws IOException
*/
public void Toexcel (list<coursedetaildto> List, httpservletrequest request,
int length, String F, outputstream out) throws IOException {
list<string> fileNames = new ArrayList ()///For storing generated file name s
File Zip = new file (Request.getrealpath ("/") + F + ". zip");//Compressed file
Build Excel
int totalnum = 0;
Determine the number of Excel generated
if (list.size ()% length = = 0) {
if (list.size () = = length) {
Totalnum = 1;
} else {
Totalnum = list.size ()/length;
}
} else {
Totalnum = list.size ()/length + 1;
}
for (int j = 0, n = totalnum; j < N; j + +) {
Workbook book = new Hssfworkbook ();
Sheet Sheet = Book.createsheet ("Userdatadetail");
String file = Request.getrealpath ("/") + F + "-" + j + ". xls";
Filenames.add (file);
FileOutputStream o = null;
try {
o = new FileOutputStream (file);
Row row = Sheet.createrow (0);
Row.createcell (0). Setcellvalue ("Time");
Row.createcell (1). Setcellvalue ("User Details");
Row.createcell (2). Setcellvalue ("User employee ID");
int m = 1;
for (int i = 1, min = (list.size ()-J * length + 1) > (length + 1)? (length + 1)
: (List.size ()-J * length + 1); i < min; i++) {
m++;
Coursedetaildto coursedetail = list.get (length * (j) + i-1);
row = Sheet.createrow (i);
Row.createcell (0). Setcellvalue (Coursedetail.gettopdate ());
Row.createcell (1). Setcellvalue (Coursedetail.getuserid ());
Row.createcell (2). Setcellvalue (Coursedetail.getemployeeid ());
}
CellStyle cellStyle2 = Book.createcellstyle ();
Cellstyle2.setalignment (Cellstyle.align_center);
row = Sheet.createrow (m);
Cell cell0 = Row.createcell (0);
Cell0.setcellstyle (CellStyle2);
Cell cell1 = Row.createcell (1);
Cell1.setcellstyle (CellStyle2);
Cell cell2 = Row.createcell (2);
Cell2.setcellstyle (CellStyle2);
} catch (Exception e) {
E.printstacktrace ();
}
try {
Book.write (o);
} catch (Exception ex) {
Ex.printstacktrace ();
} finally {
O.flush ();
O.close ();
}
}
File srcfile[] = new file[filenames.size ()];
for (int i = 0, n = filenames.size (); i < n; i++) {
Srcfile[i] = new File (Filenames.get (i));
}
Filezip.zipfiles (srcfile, Zip);
FileInputStream instream = new FileInputStream (Zip);
byte[] buf = new byte[4096];
int readlength;
while (((Readlength = Instream.read (buf)) =-1)) {
Out.write (buf, 0, readlength);
}
Instream.close ();
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POI Bulk Export Data