// my hyperlink for user to click
<a href="#" ID="hyperLink">Click Here to Export </a>
我的J-Query / AJAX代码
<script type="text/javascript">
$(document).ready(function () {
$("#hyperLink").click(function (e) {
var result = $('#output').html();
var newRes = result.replace('\n', '');
$.ajax({
url: "ExportToExcelhandler.ashx",
data: { 'htmlData': newRes },
dataType: "json",
type: "POST",
success: function (data) {
alert(data);
}
});
});
});
</script>
和我的经纪人:
public void ProcessRequest(HttpContext context)
{
string htmlStuff = context.Request["htmlData"];
string trimStart = "";
string trimEnd = "";
if (htmlStuff != null)
{
trimStart = htmlStuff.Substring(75, htmlStuff.Length - 75);
trimEnd = trimStart.Remove(trimStart.Length - 8, 8) + "";
}
string final= trimEnd;
context.Response.Clear();
context.Response.Buffer = true;
context.Response.AddHeader("content-disposition", "attachment; filename=excelData.xls");
context.Response.ContentType = "application/vnd.ms-excel";
HttpResponse response = context.Response;
context.Response.Output.Write(finalHtmlData);
context.Response.Flush();
context.Response.End();
}
- 当然,我正在使用我的J-Query中的replace函数和我的处理程序中的Substring和Remove做一些奇怪的事情;这是因为我必须修剪我的html数据,以便只包含其中包含数据的表(否则会导致错误)。 html数据只是报告数据。因此,html数据可以很好地传递给处理程序,并且它可以很好地通过ProcessRequest方法,但不会导出到excel。非常感谢任何帮助,谢谢。
答案 0 :(得分:1)
将其拆分为两个HTTP处理程序,一个用于生成Excel数据,另一个用于检索数据并具有资源点,如下所示:
GenerateExcelDocument HTTP处理程序代码:
public void ProcessRequest(HttpContext context)
{
string htmlStuff = context.Request["htmlData"];
var docIdentifier = this.GenerateExcelDocument(htmlStuff);
context.Response.ContentType = "text/plain";
context.Response.Write(docIdentifier.ToString("N"));
}
private Guid GenerateExcelDocument()
{
var identifier = Guid.NewGuid();
string trimStart = "";
string trimEnd = "";
if (htmlStuff != null)
{
trimStart = htmlStuff.Substring(75, htmlStuff.Length - 75);
trimEnd = trimStart.Remove(trimStart.Length - 8, 8) + "";
}
// Logic that generates your document and saves it using the identifier
// Can save to database, file system, etc.
return identifier;
}
现在你可以调用这个HTTP处理程序,如下所示:
$(document).ready(function () {
$("#hyperLink").click(function (e) {
var result = $('#output').html();
var newRes = result.replace('\n', '');
$.ajax({
url: "GenerateExcelDocument.ashx",
data: { 'htmlData': newRes },
dataType: "json",
type: "POST",
success: function (result) {
window.location.href = '/RetrieveExcelDocument.ashx/' + result;
}
});
});
});
注意:
success
回调是您可以将HTML资源连接到从服务器检索文件的地方(想想锚标记的href
,但之前没有将数据传递给处理程序)。
最后,我们需要构建检索HTTP处理程序逻辑,以便根据GenerateExcelDocument
HTTP处理程序调用返回的标识符实际获取Excel文档,如下所示:
RetrieveExcelDocument HTTP处理程序代码:
public void ProcessRequest(HttpContext context)
{
var identifier = new Guid(context.Request.Url.Segments[1]);
// Get Excel document content from database, file system, etc. here
var fileContent = GetExcelDocument(identifier);
context.Response.AddHeader("content-disposition",
"attachment; filename=excelData.xls");
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.OutputStream.Write(fileContent, 0, fileContent.Length);
}