【发布时间】:2014-11-18 05:45:37
【问题描述】:
谁能帮帮我,
我正在尝试导出到 excel 模块,我有这个导出代码:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=exportTest.xls");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.Output.Write(excelExport);
HttpContext.Current.ApplicationInstance.CompleteRequest();
与我的问题有关,当我将此代码块放入由 jquery 通过 ajax 调用的 webmethod 时,它只是在消息弹出窗口中返回要导出的字符串,而当我将此代码块放入单击时一个 asp 按钮控件的方法(例如 ExcelExportButton_Click)它可以工作。
不工作的代码:
[WebMethod]
public static void ExportReportsTableToExcel(string ExportReport)
{
string excelExport = "a string to export to excel";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=exportTest.xls");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.Output.Write(excelExport);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
工作代码:
protected void ExportReportButton_Click(object sender, EventArgs e)
{
string excelExport = "a string to export to excel";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=exportTest.xls");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.Output.Write(excelExport);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
请不要介意示例字符串。
【问题讨论】:
-
基本上,我知道webmethod和普通方法的区别。我列出了我必须进一步解释标题中的意思的案例。如果标题具有误导性,我深表歉意。