1, the service side, according to the parameters to obtain data data
referencing DLLs:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Services;
Using Npoi. HSSF. Usermodel;
Using Npoi. HPSF;
Using Npoi. Poifs. filesystem;
Using Npoi. Ss. Usermodel;
Using Npoi. Ss. Formula.functions;
Using System.IO;
Initialize the Npoi entity:
Hssfworkbook Hssfworkbook;
void Initializeworkbook ()
{
Hssfworkbook = new Hssfworkbook ();
Create a entry of documentsummaryinformation
Documentsummaryinformation dsi = Propertysetfactory.createdocumentsummaryinformation ();
Dsi.company = "AP Analysis";
Hssfworkbook. documentsummaryinformation = DSi;
Create a entry of summaryinformation
SummaryInformation si = propertysetfactory.createsummaryinformation ();
Si. Subject = "Export analysis result";
Hssfworkbook. SummaryInformation = si;
}
Set header and data correspondence:
private string Pengzhuang (int taskid, int tasktype)
{
String url = "";
Ap. Bll. Crashresult CRASHBLL = new AP. Bll. Crashresult ();
List<ap. model.crashresult> list = Crashbll. Getmodellist ("taskid=" + TaskID);
int count = 0;
Use Npoi to write the head of the table first
Initializeworkbook ();
Isheet Sheet1 = Hssfworkbook. Createsheet ("task" + TaskID + "export result");
IRow Row1 = Sheet1. CreateRow (0);
Row1. Createcell (0). Setcellvalue ("serial number");
Row1. Createcell (1). Setcellvalue ("Mac");
Row1. Createcell (2). Setcellvalue ("collision rate");
int x = 1;
foreach (AP. Model.crashresult CS in list)
{
IRow row = Sheet1. CreateRow (x);
Row. Createcell (0). Setcellvalue (cs.id);
Row. Createcell (1). Setcellvalue (CS. MAC);
Row. Createcell (2). Setcellvalue (CS. Crashcount.tostring ());
x + +;
}
url = writetofile ();
return URL;
}
Returns the URL of the file download:
[WebMethod]
public void Exceldown (int taskid,int tasktype)
{
String url = "";
Switch (tasktype)
{
Case 2://
url = Pengzhuang (Taskid,tasktype);
Break
Default
url = "#";
Break
}
Checkresponse.sendresponse (URL);
}
File generation:
String WriteToFile ()
{
Write the stream data of workbook to the root directory
String Serverimagepath = "Cardxls";
string filename = Hssfworkbook. Getsheetat (0). SheetName + ". xls";
String TempPath = System.Web.HttpContext.Current.Server.MapPath (Serverimagepath);
if (! Directory.Exists (TempPath))
{
Directory.CreateDirectory (TempPath);
}
FileStream file = new FileStream (System.Web.HttpContext.Current.Server.MapPath (Serverimagepath + "/" + filename), FileMode.Create);
Hssfworkbook. Write (file);
File. Close ();
Return "/" + Serverimagepath + "/" + filename;
}
2. JS Ajax Call Service
<%@ Page language= "C #" autoeventwireup= "true" codebehind= "FileDown.aspx.cs" inherits= "Ap.Service.FileDown"%>
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div style= "color: #4fcaa5; text-align:center;padding-top:80px;" >
<div id= "divprocess" style= "color: #4fcaa5; text-align:center;" >
Generating files, please wait ...
</div>
<div id= "Divdown" style= "Display:none;" >
<a href= "#" id= "Afileurl" >
</a>
</div>
</div>
</form>
<script src= "Scripts/jquery-1.4.1.min.js" ></script>
<script type= "Text/javascript" >
function Getserviceurl (URL) {
var Sessions = $.cookie (' Sessions ');
alert (Sessions);
var resulturl = "/file.asmx/" + url;
return resulturl;
}
function Requestparm (paras) {
var url = location.href;
var parastring = url.substring (Url.indexof ("?") + 1, url.length). Split ("&");
var paraobj = {}
for (i = 0; j = parastring[i]; i++) {
Paraobj[j.substring (0, j.indexof ("=")). toLowerCase ()] = j.substring (j.indexof ("=") + 1, j.length);
}
var returnvalue = Paraobj[paras.tolowercase ()];
if (typeof (returnvalue) = = "undefined") {
Return "";
} else {
Return returnvalue;
}
}
$ (document). Ready (function (e) {
var serviceurl = Getserviceurl ("Exceldown");
$.ajax ({
Url:serviceurl,
DataType: ' <a href= ' WWW.111CN.NET "class=" Keylink "title=" JSONP "target=" _blank ">JSONP</a>",
Data: {
Taskid:requestparm ("TaskID"),
Tasktype:requestparm ("TaskType")
},
JSONP: ' Callback ',
Success:function (Result) {
var json_data = $.parsejson (Result.jsondata);
if (json_data!= null) {
$ ("#divprocess"). HTML ("File Build success!") Click on the download button below to download. It is recommended that the "right key Save As ... \" is saved. ");
$ ("#afileurl"). attr ("href", json_data);
$ ("#divdown"). Show ();
}
},
Complete:function (XMLHttpRequest, Textstatus) {
alert (textstatus);
},
Error:function (e) {
$ ("#divprocess"). HTML (' server exception, please contact system administrator: ' + e.responsetext ');
Return
},
timeout:50000
});
//------------------------------------------
});
</script>
</body>