showModalDialog从gridview导出excel

摘要 : showModalDialog from gridview export excel

showModalDialog从DataGrid导出excel 也可以用这个方法

在弹出窗口的页面内放置一个iframe 标记,iframe的src设置为导出按钮所在的页面地址 就可以了
IFRAME
<IFRAME ID=IFrame1 FRAMEBORDER=0 SCROLLING=NO xsrc="Export.aspx"></IFRAME>
其实这种方法是不可行的

首先 把 把Gridvew或DataGrid控件放到div中的
        <div id="divContent">
            <asp:DataGrid runat="server" ID="myReapt" AutoGenerateColumns="False" CssClass="cGrid1"
                HorizontalAlign="Center" Style="margin-top: 7px" Width="98%" OnItemDataBound="myReapt_ItemDataBound">
                <HeaderStyle CssClass="cGridHeader1" />
                <ItemStyle CssClass="cGridItem1" />
                <AlternatingItemStyle CssClass="cGridAlterItem1" />
                <Columns>
                    <asp:BoundColumn DataField="fname" HeaderText="姓名"></asp:BoundColumn>
            </asp:DataGrid>
        </div>
然后用 javascript 得到div 中的html代码
    <script type="text/javascript">
    function GetHTML()
    {
        var hfHTML=document.getElementById("hfHTML");
        if(hfHTML)
        {
            var divContent=document.getElementById("divContent");
            hfHTML.value=divContent.innerHTML;
        }
    }
    </script>
放到一个隐藏域中
<asp:HiddenField ID="hfHTML" runat="server" />
在点导出按钮的时候调用 GetHTML方法
                        <asp:Button ID="btn_Out" runat="server" CssClass="cBtn7" Text="导出Excel" OnClick="btn_Out_Click"
                            OnClientClick="GetHTML()" />
然后在服务器端处理这个 btn_Out_Click事件

    /// <summary>
    /// 导出Excel
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Out_Click(object sender, EventArgs e)
    {
        this.RepeatToExcel(this.myReapt, "企业人员查重信息");
    }
    void RepeatToExcel(DataGrid rept, string Title)
    {
        string sFileName = Title;
        string sHeadTittle = Title;
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "utf-8";  //设置了类型为中文防止乱码的出现  
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(sFileName, System.Text.Encoding.UTF8) + ".xls"); //定义输出文件和文件名
        Response.ContentEncoding = System.Text.Encoding.UTF8;//设置输出流为简体中文
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 

        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        oHtmlTextWriter.Write("<div align=center height=25 valign=middle><font size='6'>" + sHeadTittle + "</font></div><br>");
        oHtmlTextWriter.Write(hfHTML.Value);
        Response.Write(oStringWriter.ToString());
        oHtmlTextWriter.Close();
        oStringWriter.Close();
        Response.End();
    }
这样就可以了
上一篇 :javascript弹出窗口和模式对话框
下一篇 :dataGridView右键选中行