Excel Export附件未显示 [英] Excel Export attachments not displaying

查看:76
本文介绍了Excel Export附件未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我已经陷入了网格视图导出选项到Excel工作表的一些问题.请帮助我解决这个问题.附件不在导出的excel中.
谢谢.
这是代码




Hi All,
I have got stuck with some issue with export option of gridview to excel sheet. Kindly help me is sloving the porblem. Attachments are not coming in the excel exported.
Thank you.
Here is code




public void Export(string fileName, GridView gv) 
        { 
            HttpContext.Current.Response.Clear(); 
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName)); 
            HttpContext.Current.Response.ContentType = "application/ms-excel"; 
 
            using (StringWriter sw = new StringWriter()) 
            { 
                using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
                { 
                    //  Create a form to contain the grid  
                    Table table = new Table(); 
                     
                    table.GridLines = GridLines.Both; 
                     
 
                    //  add the header row to the table  
                    if (gv.HeaderRow != null) 
                    { 
                        PrepareControlForExport(gv.HeaderRow); 
                        table.Rows.Add(getheader()); 
                        table.Rows.Add(gv.HeaderRow); 
                        table.Rows[0].BackColor = System.Drawing.Color.FromArgb(37, 96, 133); 
                    } 
 
                    //  add each of the data rows to the table  
                    foreach (GridViewRow row in gv.Rows) 
                    {                         
                        PrepareControlForExport(row); 
                        table.Rows.Add(row); 
                    } 
 
                    //  add the footer row to the table  
                    if (gv.FooterRow != null) 
                    { 
                        PrepareControlForExport(gv.FooterRow); 
                        table.Rows.Add(gv.FooterRow); 
                    } 
 
                    //  render the table into the htmlwriter  
                    table.RenderControl(htw); 
 
                    //  render the htmlwriter into the response  
                    HttpContext.Current.Response.Write(sw.ToString()); 
                    HttpContext.Current.Response.End(); 
                } 
            } 
        } 
 
 
/// <summary>  
        /// Replace any of the contained controls with literals  
        /// </summary>  
        /// <param name="control"></param>  
        private static void PrepareControlForExport(Control control) 
        { 
            for (int i = 0; i < control.Controls.Count; i++) 
            { 
                Control current = control.Controls[i]; 
                if (current is LinkButton) 
                { 
                    control.Controls.Remove(current); 
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); 
                } 
                else if (current is ImageButton) 
                { 
                    control.Controls.Remove(current); 
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); 
                } 
                else if (current is HyperLink) 
                { 
                    control.Controls.Remove(current); 
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); 
                } 
                else if (current is DropDownList) 
                { 
                    control.Controls.Remove(current); 
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); 
                } 
                else if (current is CheckBox) 
                { 
                    control.Controls.Remove(current); 
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); 
                } 
 
                if (current.HasControls()) 
                { 
                    PrepareControlForExport(current); 
                } 
            } 
        } 

推荐答案

此代码仅在页面加载本身中有效...
创建一个新表单并将代码放在page_load本身上,然后您的excel导出功能将起作用...


如果要在按钮上单击获取表单,然后在该Buuton单击操作上编写此代码

Response.Redirect(〜Example.Aspx")
This code will work only in page load itself...
Create a new form and put your code on page_load itself then your excel export functionality will work...


If you want to get the form on button click then write this code on that Buuton click action

Response.Redirect("~Example.Aspx")


这篇关于Excel Export附件未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆