将GridView导出到Excel,并在网页上显示完全相同的样式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将GridView导出到Excel,并在网页上显示完全相同的样式相关的知识,希望对你有一定的参考价值。
我可以将gridview数据导出到excel,但我无法在excel文件中设置表格的样式。
这是我的代码:
protected void Button3_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName =DateTime.Now.ToString("yyyy-MM-dd")+".xls";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
using (StringWriter sw = new StringWriter())
{
htmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
}
}
我的Gridview设置是这样的:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="料號">
<ItemTemplate>
<asp:Label ID="Part_No" runat="server" Text='<%# Eval("Part_No")%>' ></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="bhead" />
<ItemStyle CssClass="bbody" />
</asp:TemplateField>
<asp:TemplateField HeaderText="End user" >
<ItemTemplate>
<asp:Label ID="Dircu" runat="server" Text='<%# Eval("Dircu")%>' ></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="bhead" />
<ItemStyle CssClass="bbody" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Assembly" >
<ItemTemplate>
<asp:Label ID="Assfct" runat="server" Text='<%# Eval("Assfct")%>' ></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="bhead" />
<ItemStyle CssClass="bbody" />
</asp:TemplateField>
</Columns>
</asp:GridView>
因为我使用cssclass来设置我的Gridview样式,所以当我导出这个Gridview时,我不知道如何将样式复制到Excel。
有人可以帮帮我吗?
答案
我在这个网站找到了一个很好的解决方案:get the exact same layout
我从下面的代码中得到了解决方案:
protected void ExportToExcel(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";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
this.BindGrid();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
如果你像我这样用cssclass设置你的gridview样式。您可以在这里添加描述string style = @"<style> .textmode { } </style>";
的样式并将cell.BackColor =
更改为cell.CssClass="textmode"
然后您可以获得自定义样式。
这是获得完全相同布局的代码:
protected void Button3_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName =DateTime.Now.ToString("yyyy-MM-dd")+".xls";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.CssClass = "bhead";
}
foreach (GridViewRow row in GridView1.Rows)
{
foreach (TableCell cell in row.Cells)
{
cell.CssClass = "bbody";
}
}
GridView1.RenderControl(hw);
string style = @"<style> .bhead{padding:5px;
border:1px solid #fff;
background:#003d79;
color:white;
text-align:center; } .bbody{color:Black;
text-align:center;
background:#d7ffee;
padding:5px;
border:1px solid #fff;}</style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.End();
}
}
我希望这可以帮助一些有同样问题的人。
以上是关于将GridView导出到Excel,并在网页上显示完全相同的样式的主要内容,如果未能解决你的问题,请参考以下文章