导出到excel [.xlsx] [英] export to excel[ .xlsx ]

查看:175
本文介绍了导出到excel [.xlsx]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试将数据导出到excel文件[.xlsx文件].
但我无法打开该文件.
其警告消息
"Excel无法打开文件"MyExport.xlsx",因为文件格式或文件扩展名无效.请验证文件未损坏且文件扩展名与文件格式匹配"

有谁知道该解决方案如何解决此2007 excel出口问题.

Hi all,

I am trying to export data to an excel file [.xlsx file].
but I am unable to open that file.
Its giving warning message
"Excel cannot open the file ''MyExport.xlsx'' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file"

Is any one knows the solution how to resolve this 2007 excel export issue.

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
myGrid.RenderControl(hw);

Response.Clear();
Response.Buffer = true;       
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xlsx");
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.Write(sw);
Response.End();

推荐答案

尝试这段代码,我认为它将对您有帮助....
但是有一件事我很清楚,如果您将gridview放在UpdatePanel中,那么您的任何代码都将无法正常工作,并且会给出如下错误...

try this code i think it will help you....
but one thing i clear that if your have put your gridview inside UpdatePanel Then any of your code will not work and its give the error like this...

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.




将您的网格放在UpdatePanel之外,并使用此代码,它将对我有用...




put your grid out side the UpdatePanel and use this code it will work for me...

protected void Export2Excel()
{
    try
    {
        this.GridView1.AllowPaging = false;
        this.GridView1.AllowSorting = false;
        this.GridView1.EditIndex = -1;

        // Let's bind data to GridView
        this.BindData();

        // Let's output HTML of GridView
        Response.Clear();
        Response.ContentType = "application/vnd.xls";
        // OR .xlsx file use this..
        // ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

        Response.AddHeader("content-disposition",
                "attachment;filename=MyList.xls");
        Response.Charset = "";
        StringWriter swriter = new StringWriter();
        HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
        GridView1.RenderControl(hwriter);
        Response.Write(swriter.ToString());
        Response.End();
    }
    catch (Exception exe)
    {
        throw exe;
    }
 
}       


请参阅类似的问题
Refer to the similar question How to export gridview to excel ?[^], that was answered earlier.

Also, make sure you have mime map configured for xlsx in IIS.


这篇关于导出到excel [.xlsx]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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