而出口在asp.net GridView中为E​​xcel格式错误 [英] Format error while exporting to excel from gridview in asp.net

查看:102
本文介绍了而出口在asp.net GridView中为E​​xcel格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在该网站提到的如使用System.Microsoft.Office.Excel 的Excel = System.Microsoft.Office.Excel几个解决方案,但力的工作....
在这里,我试图让一个表中的数据,并下载到文件中的指定位置.xls格式的服务器,然后给用户一个链接,下载该文件。
这是code出口`

I Tried a few solutions mentioned in the site like Using System.Microsoft.Office.Excel and Excel = System.Microsoft.Office.Excel, but it dint work.... Here, I'm trying to get the data in a table and download to a file in the specified location in the server in .xls format and then giving the users a link to download the file. this is the code for export `

protected void btnExcelExport_Click(object sender, EventArgs e)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using( HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            //  Create a form to contain the grid
            Table table = new Table();
            // get gridlines from gridview
            table.GridLines = GridView2.GridLines;
            if (GridView2.HeaderRow != null)
            {                       
                table.Rows.Add(GridView2.HeaderRow);
            }
            foreach (GridViewRow row in GridView2.Rows)
            {
                table.Rows.Add(row);
            }
            if (GridView2.FooterRow != null)
            {
                table.Rows.Add(GridView2.FooterRow);
            }    
            //  render the table into the htmlwriter
            table.RenderControl(htw);    
        }
        var myRootPath = Server.MapPath("~");
        var docPath = Path.GetFullPath(Path.Combine(myRootPath, "/Compare/c.xls")); 
        File.WriteAllText(docPath, sw.ToString());  
    }
    dwndlink.Visible = true;

}

而当这是code为LinkBut​​ton的:

And when this is code for linkbutton:

 protected void dwnlink(object sender, EventArgs e)
 {   
     var webRootPath = Server.MapPath("~");
     var docPath = Path.GetFullPath(Path.Combine(webRootPath, "/Compare/c.xls"));

     string name = Path.GetFileName(docPath);
     Response.AppendHeader("content-disposition", "attachment; filename=" + name);
     Response.ContentType = "Application/vnd.ms-excel";
     Response.TransmitFile(docPath);
     Response.End();
 }

因此​​,当用户打开所下载的文件,发出警告作为其不在同一扩展。
这是怎么回事??

so when the user opens the downloaded file, gives a warning as its not in the same extension. Why is this happening..??

我试图在各种网站提供的解决方案的工作,但无济于事...

I tried working with the solutions provided in various sites, but to no avail...

感谢

推荐答案

试着改变 Response.ContentType =应用程序/ vnd.xls; Response.ContentType =应用程序/ vnd.ms - Excel的

此外

微软文件说:

目前的设计不允许你打开在Excel网站HTML内容...所以返回HTML和MIME类型设置为类似XLS的ASP页面,试图迫使HTML在Excel中打开,而不是Web浏览器(如预期)总是会得到安全警告......如果你使用HTML MIME类型,那么Web浏览器将打开内容,而不是Excel中,所以没有很好的办法解决这种情况下,由于缺乏一个特殊的MIME类型HTML / MHTML是Excel的具体。如果你控制了Web服务器需要访问它的客户端桌面,但除此之外,最好的选择是使用不同的文件格式,你可以添加自己的MIME类型或者你的提醒警告的用户,让他们选择是对话。

"The current design does not allow you to open HTML content from a web site in Excel... So ASP pages that return HTML and set the MIME type to something like XLS to try to force the HTML to open in Excel instead of the web browser (as expected) will always get the security alert... If you use an HTML MIME type, then the web browser will open the content instead of Excel. So there is no good workaround for this case because of the lack of a special MIME type for HTML/MHTML that is Excel specific. You can add your own MIME type if you control both the web server and the client desktops that need access to it, but otherwise the best option is to use a different file format or alert your users of the warning and tell them to select Yes to the dialog."

访问这个网站太:的 http://devblog.grinn.net/2008/06/file-you-are-trying-to-open-is-in.html

这篇关于而出口在asp.net GridView中为E​​xcel格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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