gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:41 34 4
gpt4 key购买 nike

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

此代码获取我的网格数据并将其导出到 excel 删除我试图用谷歌搜索的所有网格线找不到突破口有人可以帮助我吗

  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();
}

enter image description here

最佳答案

看看我之前写的下面的函数,有什么不明白的地方请问,希望对你有帮助

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();
}
}

关于c# - 将数据从 gridview 导出到 excel 而不会丢失网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28232498/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com