C# 将数据从gridview导出到excel而不丢失网格线

C# 将数据从gridview导出到excel而不丢失网格线,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试将网格视图数据导出到excel。我可以在excel中绑定数据。但是,当我打开网格视图时,我丢失了它的网格线,这看起来非常糟糕。我在下面添加了我的代码 这段代码获取我的网格数据并将其导出到excel删除我试图搜索的所有网格线google找不到断点有人能帮我吗 protected void btn_export_Click(object sender, EventArgs e) { Response.Buffer = true; Respons

我正在尝试将网格视图数据导出到excel。我可以在excel中绑定数据。但是,当我打开网格视图时,我丢失了它的网格线,这看起来非常糟糕。我在下面添加了我的代码

这段代码获取我的网格数据并将其导出到excel删除我试图搜索的所有网格线google找不到断点有人能帮我吗

  protected void btn_export_Click(object sender, EventArgs e)
    {

        Response.Buffer = true;
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Charset = "";
        string FileName = "Dashboard_FOR_PM" + DateTime.Now + ".xls";
        StringWriter strwritter = new StringWriter();
        HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
        grv_dashboard.GridLines = GridLines.Both;

        grv_dashboard.HeaderStyle.Font.Bold = true;
        grv_dashboard.RenderControl(htmltextwrtter);
        Response.Write(strwritter.ToString());

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
              Table table = new Table();
        table.BorderStyle = BorderStyle.Solid;
        table.BorderWidth=new Unit(1);
        table.BorderColor = System.Drawing.Color.Black;
        table.GridLines = GridLines.Both;
            }
        }


        strwritter.Write(@"<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">");
        strwritter.Write(@"<head>
                           <xml>
                             <x:ExcelWorkbook> 
                               <x:ExcelWorksheets>
                                   <x:ExcelWorksheet>
                                      <x:WorksheetOptions>
                                         <x:Panes></x:Panes>
                                         <x:Print><x:Gridlines /></x:Print>
                                      </x:WorksheetOptions>
                                    </x:ExcelWorksheet>
                                  </x:ExcelWorksheets>
                                </x:ExcelWorkbook>
                              </xml>
                            </head>");
        Response.End();
    }
受保护的无效btn\u导出\u单击(对象发送方,事件参数e)
{
Response.Buffer=true;
Response.ClearContent();
Response.ClearHeaders();
响应。Charset=“”;
字符串FileName=“Dashboard\u FOR_PM”+DateTime.Now+“.xls”;
StringWriter strwriter=新StringWriter();
HtmlTextWriter htmltextwrtter=新的HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType=“application/vnd.ms excel”;
Response.AddHeader(“内容处置”、“附件;文件名=“+filename”);
grv_dashboard.GridLines=网格线。两者都有;
grv_dashboard.HeaderStyle.Font.Bold=true;
grv_仪表板渲染控制(htmltextwrtter);
Write(strwriter.ToString());
使用(StringWriter sw=new StringWriter())
{
使用(HtmlTextWriter htw=新的HtmlTextWriter(sw))
{
Table Table=新表();
table.BorderStyle=BorderStyle.Solid;
表.边框宽度=新单元(1);
table.BorderColor=System.Drawing.Color.Black;
table.GridLines=网格线。两者都有;
}
}
书写(@“);
strwriter.Write(@)
");
Response.End();
}

看看我以前写的下面的函数,如果有什么不清楚的地方,请询问,希望能有所帮助

public void ToExcel(DataTable dt, string reportName)
    {
        if (dt.Rows.Count > 0)
        {
            string filename = string.Format("{0}.xls", reportName);
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            HtmlForm hf = new HtmlForm();

            DataGrid dg = new DataGrid();

            dg.DataSource = dt;
            dg.HeaderStyle.BackColor = Color.Tomato;//change to your own colors
            dg.HeaderStyle.ForeColor = Color.White;//change to your own colors
            dg.BackColor = Color.LightGoldenrodYellow;//change to your own colors
            dg.AlternatingItemStyle.BackColor = Color.PaleGoldenrod;//change to your own colors
            dg.Font.Name = "Tahoma";
            dg.Font.Size = 10;
            dg.GridLines = GridLines.Both;
            dg.BorderColor = System.Drawing.Color.Black;
            dg.BorderStyle = BorderStyle.Solid;
            dg.BorderWidth = new Unit(1);
            dg.DataBind();

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);

            HttpContext.Current.Response.Charset = "";
            EnableViewState = false;
            Controls.Add(hf);
            hf.Controls.Add(dg);
            hf.RenderControl(htw);
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }

您是否考虑过使用类似NPOI的库?我在string ReportName中提到了什么?只是excel文件的名称,比如“Dashboard\u for\u PM”+DateTime.现在我尝试了你的代码,但我的问题是我在网格单元格中显示了颜色,希望按原样显示,因此我无法更改数据网格颜色。我是否必须向我的代码中添加一些内容,以显示网格线扫描你看看我的问题我已经编辑了它,并显示了所需的输出,因此我认为你的代码不适合我我无法将这样的gridview数据保存在datatable@anita实际上,您可以根据需要生成datatable