用html的按钮如何导出excel,不用JS的那种调ActiveXObject,因为要降低IE的安全级别。我post到ashx和WebService都导不出。

解决方案 »

  1.   

    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();}
      

  2.   

    你生成xml格式的文件就可以 了,参见
    http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx直接客户端导出是存在安全性和兼容性问题的
      

  3.   

    直接工作流的方式导出,当然不是标准的excel
      

  4.   

    <a id="export" href="Excel.xls"></a>
    <input type="button" value="导出Excel" onclick="document.getElementById('export').click();"/>