Web Development-Write a simple tabular export operation

Source: Internet
Author: User

First, the front page:

It is mainly a button and a table, the table has display data, the button is responsible for the data in the table to selectively export. In addition, you can attach a small window and a progress bar to display the download progress.

1. Button: <a href= "javascript:;" class= "Easyui-linkbutton" iconcls= "Icon-redo" data-options= "Plain:true" id= "Btn-exp" onclick= "fun_export()" > Export details </a>

2. Table: <div id= "dataGrid" style= "margin-bottom:5px;margin-top:1px" >

<table id= "DG" data-options= "toolbar: ' #tb '" ></table>
</div>

3. Progress bar and Small window
<div id= "win" class= "Easyui-window" title= "Download" style= "width:500px; height:80px "data-options=" iconcls: ' Icon-save ', modal:true ' >
<div id= "p" class= "Easyui-progressbar" data-options= "Value:10" style= "width:400px;margin-top:10px; margin-left:50px; " ></div>
</div>

4. Download box: <iframe id= "bgfiledownframe" src= "style=" display:none; Visibility:hidden; " ></iframe>

Second, JS request

<script>

function Fun_export() {
$ (' #win'). Window (' open ');
Setprogress ();

         //ajax request
$.ajax ({
URL: ' ${PAGECONTEXT.R Equest.contextpath}/media/getexl.action ',//todo
data:{},
Type: ' Post ',
DataType: ' JSO N ",
Success:function (data) {
//alert (" Export!!!!! ");
if (data.issuccess = = "true") {
$ ("#bgfiledownframe "). attr ("src", "${pagecontext.request.contextpath}/media/downloadexl.action?doctoken= " + Data.token);
value = 100;
Setprogress ();
$ (' #win '). Window (' close ');
}

else{
$ (' #win'). Window (' close ');
Alert ("Error generating statistics!") ");
}
},
});
}

Set the value of the progress bar
function setprogress () {
var value = $ (' #p '). ProgressBar (' GetValue ');
if (value < 100) {
Value + = Math.floor (Math.random () * 20);
$ (' #p '). ProgressBar (' SetValue ', value);
SetTimeout (Arguments.callee, 200);
}
}
</script>

Third, action request method:


@Action (value= "Getexl")
Public String Getexl () {

map<string,string> result = new hashmap<string,string> ();
Result.put ("Issuccess", "false");


/* Data Source */
Map<string, object> dataMap = new hashmap<string, object> ();//Export Results
List<map<string, object>> resultTable1 = Mediamanagebpo.getmediainfo ();
DataMap. Put ("rows", resultTable1);
Path
String Webpath = This.request.getSession (). Getservletcontext (). Getrealpath ("");
MDoc MDoc = new MDoc ();
try {

String Tmpfiledir = webpath + file.separator + "DocTemplate";
Check if the Temp folder exists

Util.checkdirexist (Tmpfiledir);
20161227105212956742620
String Filetoken = Util.generatetmpfilename (Tmpfiledir);

Filetoken is the generated temporary file name
String Doctemplatepath = webpath + file.separator + "DocTemplate" + File.separator + filetoken;

MEDIA.FTL is a template file, first renamed as an FTL file based on the XLS file-Save as XML file--eclipse
String templatename = "media.ftl";


Mdoc. Createxls (DataMap, Doctemplatepath,templatename);
  Result.put ("Issuccess", "true");
Result.put ("token", Filetoken);
This.request.getSession (). SetAttribute ("DownloadFile", "Data Tab" +utilslxj.getdate ());
} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Outputjson (result);

return NONE;
}
Download
@Action (value= "downloadexl")
Public String Downloadexl () {
Data tab 20161227105333.xls
String downloadname = (string) this.request.getSession (). getattribute ("DownloadFile");
if (Util.trim (downloadname). IsEmpty ()) {
Downloadname = "Data statistics. xls";
}else{
Downloadname = Downloadname + ". xls";
}
try {
OutputStream OS = This.response.getOutputStream ();

if (Util.isie (this.request)) {
Downloadname = Urlencoder.encode (Downloadname, "utf-8");
} else {
Downloadname = new String (downloadname.getbytes ("Utf-8"),
"Iso-8859-1");
}
This.response.setContentType ("Application/x-download");
This.response.addHeader ("Content-disposition",
"attachment;filename=\" "+ downloadname +" \"");
This.response.flushBuffer ();
  
String Webpath = This.request.getSession (). Getservletcontext (). Getrealpath ("");
Tmpfiledir Location of temporary files
String Tmpfiledir = webpath + file.separator + "DocTemplate";
Add file token after temporary file
String bgfile = tmpfiledir + File.separator +Doctoken;//doctoken is fromGetexlThe action came from there.
FileInputStream fis = new FileInputStream (bgfile);
Output
Util.copystrem (FIS, OS);
Fis.close ();
Os.close ();
Delete temporary files
Util.delfile (Bgfile);

} catch (IOException e) {
E.printstacktrace ();
}
return NONE;
}

The result is as follows:

Web Development-Write a simple tabular export operation

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.