繁体   English   中英

将网格导出到Excel

[英]Exporting grid to excel

我使用这段代码来探索网格以求精益求精。

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}

当我使用此代码时,网格数据会在excel中显示,但是没有垂直和水平线。它显示的是gridview列名称为链接而非普通文本。我没有对gridview使用任何主题,并且列是自动生成的。比如说col3包含更多描述,所以第二行开始时它的数据显示在顶部,仍然在单元格2中显示它的标题。所以我想在其中添加垂直和水平线。因此很容易知道属于哪个数据我想格式化col 1也只格式化数字,小数点后零。

 1 col1    col2              col3
                                     hjhhjhjhjhjhjhjhjhjhjbbv
                                     gfgfgfuiuiuiuiui
    2 9.12E+11   gkjj                etwtrtrwqtrtttwwtretqwfe
                                     dear member
                                      gffgf

    3 565E+11 hghgh

在页面加载事件上绑定网格数据,然后尝试再次导出..按照此链接的代码..可能是好的,而不是其他的...

将GridView数据导出到Excel

代码段:

private void ExportGridView()
    {
        string attachment = "attachment; filename=Contacts.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

注意: 也发布了一些评论,其中也包含一些信息。

要在将数据导出到excel时根据网格控件设置GridView的格式,请访问以下链接: ASP.Net 2.0:将GridView导出到Excel

暂无
暂无

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

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