繁体   English   中英

将gridview导出到Excel 2007女士

[英]exporting gridview to Ms Excel 2007

我正在尝试将网格视图数据导出到excel 2007,即xlsx格式。 但它给人错误。

我正在使用以下代码

受保护的void Button1_Click(对象发送者,EventArgs e)

    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=text.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.DataSource = Presenter.CurrentModel.BillStatementGlModelRecords;
        GridView1.DataBind();
        GridView1.AllowPaging = false;

        GridView1.DataBind();
        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            //Change Color back to white
            row.BackColor = System.Drawing.Color.White;
            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");
        }
        GridView1.RenderControl(hw);
        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }

但无法正常工作,并且在打开文件时出现以下错误“ Excel无法打开文件'ChangeRequestList [2] .xlsx',因为文件格式或文件扩展名无效。请验证文件是否未损坏以及文件是否正确扩展名与文件格式匹配”

谁能帮我?

谢谢

看看这段代码-我已经用过了,并且可以正常工作。 但是它会生成纯文本(我猜是csv),而不是xlsx,但是我认为这应该不是问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM