如何用ajax导出excel-CSDN论坛
2个回答
展开全部
<input type="button" value="导出" style="width: 50px;" onclick="getExcel();" />
function getExcel() {
var tabHtml = $("#tb_Class").html();
$.post("../Ajax/GetExcel", { tab: encodeURIComponent(tabHtml) });
}
Controler中:
public void GetExcel()
{
string sHtml = Server.UrlDecode(Request.Form["tab"]);
ExportToExcel("application/ms-excel", "XXXXX表.xls", sHtml.ToString());
}
public void ExportToExcel(string FileType, string FileName, string ExcelContent)
{
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
StringWriter tw = new StringWriter();
Response.Output.Write(ExcelContent.ToString());
Response.Flush();
Response.End();
}
function getExcel() {
var tabHtml = $("#tb_Class").html();
$.post("../Ajax/GetExcel", { tab: encodeURIComponent(tabHtml) });
}
Controler中:
public void GetExcel()
{
string sHtml = Server.UrlDecode(Request.Form["tab"]);
ExportToExcel("application/ms-excel", "XXXXX表.xls", sHtml.ToString());
}
public void ExportToExcel(string FileType, string FileName, string ExcelContent)
{
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
StringWriter tw = new StringWriter();
Response.Output.Write(ExcelContent.ToString());
Response.Flush();
Response.End();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询