首页 新闻 赞助 找找看

急 急,在线等 谢谢: asp.net FileUpload与页面数据导出的问题

0
悬赏园豆:20 [已解决问题] 解决于 2012-11-26 14:50

我的页面大概是这样的:两个gridview(一个用来存放计算的原始数据,一个用来存放结果数据,然后把结果数据导出到excel中)用户添加原始数据有两种方式:一种是在线添加原始数据,还有一种是通过把txt文件中的数据导入到gridview中去,其中第二种方法,我在界面添加了FileUpload 供用户上传要导入数据的文件,现在遇到的问是:
之前没有加第二种方法的时候 结果数据导出到excel 完全正常,但是后来加了一个FileUpload  以实现用户把文件数据导入到gridview,但是加了一个FileUpload  控件后 结果数据导出到excel 就不正常了,打开导出的excel 提示什么文件丢失,并且导出内容还有界面上的控件 ,和之前只导出结果数据完全不同,然后我把FileUpload 屏蔽后,导出的excel又完全正常,现在很是费解 请大家帮忙 谢谢

#region excel //这段把gridview中的数据导出excel的代码应该没有问题  
         
         /// <summary>   
         /// 导出为Excel   
         /// </summary>   
         /// <param name="ctl">控件ID</param>   
         /// <param name="FileName">文件名</param>   
         public static void ToExcel(System.Web.UI.Control ctl, string FileName)
         {
             HttpContext.Current.Response.Charset = "UTF8";
             HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
             HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));

             HttpContext.Current.Response.ContentType = "application/ms-excel"; //image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword  

             ctl.Page.EnableViewState = false;
             System.IO.StringWriter tw = new System.IO.StringWriter();
             HtmlTextWriter hw = new HtmlTextWriter(tw);
             ctl.RenderControl(hw);
             HttpContext.Current.Response.Write(tw.ToString());
             HttpContext.Current.Response.End();
         }
         #endregion
study_hard_01的主页 study_hard_01 | 初学一级 | 园豆:126
提问于:2012-08-08 11:19
< >
分享
最佳答案
0
ctl是什么,这个控件里面是不是包含了FileUpload控件,如果包含的话,导出时就会将该控件也渲染到你的excel中去,尝试将
FileUpload放到ctl之外,或者导出之前替换ctl中的服务器控件
public class ExcelHelper
    {
        public static void PrepareControlForExport(System.Web.UI.Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                System.Web.UI.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);
                }
            }
        } 
    }

调用

ExcelHelper.PrepareControlForExport(GridView1);

就可以替换其中的服务器控件,方法可能需要你再扩充下

另外,页面中需要有

public override void VerifyRenderingInServerForm(Control control)
{
}

收获园豆:10
Johnny Yan | 菜鸟二级 |园豆:256 | 2012-08-10 10:40
其他回答(3)
0

这样导出的文件还不是二进制文件,你用记事本打开就看到了,纯原生的Excel还是用NPOI比较理想。

http://www.cnblogs.com/downmoon/archive/2011/04/16/2017603.html

收获园豆:5
邀月 | 园豆:25475 (高人七级) | 2012-08-08 12:02
0

 打断点调试吧,主要想看

hw的内容是啥
八戒的师傅 | 园豆:1472 (小虾三级) | 2012-08-08 23:53
0

导出前先

HttpContext.Current.Response.Clear()
收获园豆:5
向往-SONG | 园豆:4853 (老鸟四级) | 2012-08-09 14:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册