文件不能被电网输出到Excel打开后 [英] File can not be open after exporting grid to excel

查看:310
本文介绍了文件不能被电网输出到Excel打开后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要出口电网出类拔萃。一切正常,如果网格具有100或更少行。但我的网拥有超过2000行。我没有得到任何错误或异常,但出口和Excel被停止工作后的文件是不开放。当我打开文件,我得到一个警告信息,如文件格式和'filename.xls'不匹配扩展。该文件可能被损坏或不安全的。除非你相信它的来源,不要打开它。你要打开它?

我使用低于code

  Response.ClearContent();
        将Response.Buffer =真;
        Response.AddHeader(内容处置的String.Format(附件;文件名= {0},IAParts.xls));
        Response.ContentType =应用程序/ MS-Excel的;
        System.IO.StringWriter SW =新System.IO.StringWriter();
        HtmlTextWriter的HTW =新的HtmlTextWriter(SW);
        GridView1.AllowPaging = FALSE;
        BindGrid(); //该方法结合的数据格
        GridView1.RenderControl(HTW);
        的Response.Write(sw.ToString());
        sw.Flush();
        到Response.End();


解决方案

亲爱的朋友,你对扩展文件错误我可以说,是关于延长硬化,可以通过查看这个链接

解决

<一个href=\"http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

或使用TXT文件这一行,并保存为.reg但这些线可以与你的版本和需求的变化[我写这几行对我的使用方法】

[开始]

Windows注册表编辑器5.00版

[HKEY_CURRENT_USER \\软件\\微软\\办公室\\ 15.0 \\ EXCEL \\安全]
ExtensionHardening= DWORD:00000000

[结束]

之后,你将永远不会看到错误有关扩展或...
以及使用Excel文件我为您提供这个功能,我搜查,在我的项目使用;)

 公共无效ExportToExcel(DataTable的DT)
    {
        如果(dt.Rows.Count大于0)
        {
            System.IO.StringWriter TW =新System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter HW =新System.Web.UI.HtmlTextWriter(TW);
            DataGrid中dgGrid =新的DataGrid();
            dgGrid.DataSource = DT;
            dgGrid.DataBind();
            dgGrid.RenderControl(HW);
            Response.ContentType =应用程序/ vnd.ms-EXCEL; // 2003 Office Excel中
            Response.AppendHeader(内容处置,文件名= OutPut.xls);
            串风格= @&LT;风格&GT;表{边界:1px的纯黑色,边界崩溃:崩溃;} {TD MSO的数字格式:\\ @;}&LT; /风格与GT;
            的Response.Write(样式);
            的Response.Write(tw.ToString());
            到Response.End();
        }
    }

I want to export grid to excel. Everything is working fine if the grid has 100 or less rows. But my grid has more than 2000 rows. I am not getting any error or exception but file is not opening after exporting and Excel gets stopped working. When I open file I am getting one warning message like "The file format and extension of 'filename.xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway ?".

I am using below code

        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename=   {0}", "IAParts.xls"));
        Response.ContentType = "application/ms-excel";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        BindGrid();//this method binds the data to grid
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        sw.Flush();
        Response.End();

解决方案

dear friend for your error about extension file i can say that is about Extension Hardening that can be solved by looking at this link

http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

or use this lines in txt file and save as .reg but these lines can be change with your versions and needs [i wrote these lines for my use]

[start]

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Excel\Security] "ExtensionHardening"=dword:00000000

[end]

after that you will never see error about Extension or ... and for using excel file I offer you this function that i searched and used in my projects ;)

public void ExportToExcel(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            DataGrid dgGrid = new DataGrid();
            dgGrid.DataSource = dt;
            dgGrid.DataBind();
            dgGrid.RenderControl(hw);
            Response.ContentType = "application/vnd.ms-excel"; // 2003 office excel
            Response.AppendHeader("Content-Disposition", "filename=OutPut.xls");
            string style = @"<style> TABLE { border: 1px solid black; border-collapse: collapse; } TD {mso-number-format:\@; }</style> ";
            Response.Write(style);
            Response.Write(tw.ToString());
            Response.End();
        }
    }

这篇关于文件不能被电网输出到Excel打开后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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