[英]Exporting Gridview to excel without gridlines
I use this code to export datagridview to excel 我使用此代码将datagridview导出到excel
HtmlForm form = new HtmlForm();
Response.Clear();
Response.Buffer = true;
string fileName = "TRAIL" + "[" + DatefromTxtBox.Text.Replace("/", "") + "_" + DatetoTxtBox.Text.Replace("/", "") + "]" + ".xls";
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
AuditTrailGV.AllowPaging = false;
AuditTrailGV.DataSource = (DataSet)ViewState["audit"];
AuditTrailGV.DataBind();
form.Controls.Add(AuditTrailGV);
this.Controls.Add(form);
form.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
the problem is this code also catches the formatting/borders of my gridview 问题是此代码还捕获了我的gridview的格式/边界
here are the sample screen shots 这是示例屏幕截图
This is my gridview in asp.net 这是我在asp.net中的gridview
and this is what appears in my excell 这就是我的前任
as you can see it transformed all of the lines like the gridview, i do not want it to happen, as much as possible if i can only retain the gridlines for the rows with data only, if its not possible, remove all the gridlines.. 如您所见,它像网格视图一样改变了所有行,我不希望它发生,如果我只能为仅包含数据的行保留网格线,则不希望这样做,如果不可能,请删除所有网格线。 。
any help? 有什么帮助吗? i really do not like those gridlines in my excell 我真的不喜欢我的excell中的那些网格线
VB VB
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
'base.VerifyRenderingInServerForm(control);
'This remains empty
End Sub
Protected Sub btnExcel_Click(sender As Object, e As ImageClickEventArgs) Handles btnExcel.Click
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.ms-excel"
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
htmlWrite.Write("<html xmlns:o=""urn:schemas-microsoft-com:office:office"" ")
htmlWrite.Write("xmlns:x=""urn:schemas-microsoft-com:office:excel"" ")
htmlWrite.Write("xmlns=""http://www.w3.org/TR/REC-html40""> ")
htmlWrite.Write("<head> ")
htmlWrite.Write("<!--[if gte mso 9]><xml> ")
htmlWrite.Write("<x:ExcelWorkbook> ")
htmlWrite.Write("<x:ExcelWorksheets> ")
htmlWrite.Write("<x:ExcelWorksheet> ")
htmlWrite.Write("<x:Name>Sheet1</x:Name> ")
htmlWrite.Write("<x:WorksheetOptions> ")
htmlWrite.Write("<x:Selected/> ")
htmlWrite.Write("<x:ProtectContents>False</x:ProtectContents> ")
htmlWrite.Write("<x:ProtectObjects>False</x:ProtectObjects> ")
htmlWrite.Write("<x:ProtectScenarios>False</x:ProtectScenarios> ")
htmlWrite.Write("</x:WorksheetOptions> ")
htmlWrite.Write("</x:ExcelWorksheet> ")
htmlWrite.Write("</x:ExcelWorksheets> ")
htmlWrite.Write("</x:ExcelWorkbook> ")
htmlWrite.Write("</xml><![endif]--> ")
htmlWrite.Write("</head>")
htmlWrite.WriteLine("")
gridView.HeaderStyle.Reset()
gridView.FooterStyle.Reset()
gridView.AlternatingRowStyle.Reset()
gridView.RowStyle.Reset()
gridView.BackColor = Color.Transparent
gridView.GridLines = GridLines.None
gridView.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.[End]()
End Sub
C# C#
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
//This remains empty
}
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
htmlWrite.Write("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" ");
htmlWrite.Write("xmlns:x=\"urn:schemas-microsoft-com:office:excel\" ");
htmlWrite.Write("xmlns=\"http://www.w3.org/TR/REC-html40\"> ");
htmlWrite.Write("<head> ");
htmlWrite.Write("<!--[if gte mso 9]><xml> ");
htmlWrite.Write("<x:ExcelWorkbook> ");
htmlWrite.Write("<x:ExcelWorksheets> ");
htmlWrite.Write("<x:ExcelWorksheet> ");
htmlWrite.Write("<x:Name>Sheet1</x:Name> ");
htmlWrite.Write("<x:WorksheetOptions> ");
htmlWrite.Write("<x:Selected/> ");
htmlWrite.Write("<x:ProtectContents>False</x:ProtectContents> ");
htmlWrite.Write("<x:ProtectObjects>False</x:ProtectObjects> ");
htmlWrite.Write("<x:ProtectScenarios>False</x:ProtectScenarios> ");
htmlWrite.Write("</x:WorksheetOptions> ");
htmlWrite.Write("</x:ExcelWorksheet> ");
htmlWrite.Write("</x:ExcelWorksheets> ");
htmlWrite.Write("</x:ExcelWorkbook> ");
htmlWrite.Write("</xml><![endif]--> ");
htmlWrite.Write("</head>");
htmlWrite.WriteLine("");
gridView.HeaderStyle.Reset();
gridView.FooterStyle.Reset();
gridView.AlternatingRowStyle.Reset();
gridView.RowStyle.Reset();
gridView.BackColor = Color.Transparent;
gridView.GridLines = GridLines.None;
gridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
You can try below code and custom the color of the downloaded data. 您可以尝试以下代码并自定义下载数据的颜色。 It will also not color the Column and rows other then the data. 除了数据,它也不会为列和行着色。
protected void DownloadExcel_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Report.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#bfc2c7");
}
int j = 1;
foreach (GridViewRow gvrow in GridView1.Rows)
{
//gvrow.BackColor = color.White;
if (j <= GridView1.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.