用html的按钮如何导出excel 用html的按钮如何导出excel,不用JS的那种调ActiveXObject,因为要降低IE的安全级别。我post到ashx和WebService都导不出。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 void ExportExcel(System.Web.UI.Control source){ HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8)); HttpContext.Current.Response.ContentType = "application/ms-excel";HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //关闭控件的视图状态 source.Page.EnableViewState = false; //初始化HtmlWriter System.IO.StringWriter writer = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer); source.RenderControl(htmlWriter); //然后将控制输出HttpContext.Current.Response.Write("<html xmlns:v=\"urn:schemas-microsoft-com:vml\""); HttpContext.Current.Response.Write("xmlns:o=\"urn:schemas-microsoft-com:office:office\""); HttpContext.Current.Response.Write("xmlns:w=\"urn:schemas-microsoft-com:office:word\""); //HttpContext.Current.Response.Write("xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\""); HttpContext.Current.Response.Write("xmlns=\"http://www.w3.org/TR/REC-html40\">"); HttpContext.Current.Response.Write(""); HttpContext.Current.Response.Write("<head>"); HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=unicode\">"); HttpContext.Current.Response.Write("<meta name=ProgId content=Word.Document>");//out put control HttpContext.Current.Response.Write(writer.ToString()); HttpContext.Current.Response.Write("</div>"); HttpContext.Current.Response.Write(""); HttpContext.Current.Response.Write("</body>"); HttpContext.Current.Response.Write(""); HttpContext.Current.Response.Write("</html>"); HttpContext.Current.Response.End();} 你生成xml格式的文件就可以 了,参见http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx直接客户端导出是存在安全性和兼容性问题的 直接工作流的方式导出,当然不是标准的excel <a id="export" href="Excel.xls"></a><input type="button" value="导出Excel" onclick="document.getElementById('export').click();"/> DataSet过滤XML问题 三层模式和工厂模式有什么重要区别?罗辑层又有什么作用?讨论贴 请问哪种测试工具可以模拟出多人同时在线并发操作的情况来? 问一个在后台使用TEXTBOX值的问题 两个小问题?请教了. .net 2005中 如何套用 javascript 脚本 焦点问题 哪有水晶报表v10的企业版下载啊?? 分页中的AllowPaging="True"问题? 请指教!关于DATAGRID的事件的3个问题 为什么网站第一次打开,JS写的图片特效会错位,刷新一下又好了 jquery怎么选择xml第一级的节点
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel";HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //关闭控件的视图状态
source.Page.EnableViewState = false; //初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
//然后将控制输出
HttpContext.Current.Response.Write("<html xmlns:v=\"urn:schemas-microsoft-com:vml\"");
HttpContext.Current.Response.Write("xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
HttpContext.Current.Response.Write("xmlns:w=\"urn:schemas-microsoft-com:office:word\"");
//HttpContext.Current.Response.Write("xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"");
HttpContext.Current.Response.Write("xmlns=\"http://www.w3.org/TR/REC-html40\">");
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("<head>");
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=unicode\">");
HttpContext.Current.Response.Write("<meta name=ProgId content=Word.Document>");
//out put control
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.Write("</div>");
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("</body>");
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("</html>"); HttpContext.Current.Response.End();}
http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx直接客户端导出是存在安全性和兼容性问题的
<input type="button" value="导出Excel" onclick="document.getElementById('export').click();"/>