我可以将gridview数据导出为excel,但我无法在excel文件中设置表格的样式。
这是我的代码:
<label for="{{ name + '-' + op }}" (click)="formControl.setValue(op)">{{ label + op }}</label>
我的Gridview设置如下:
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();
}
}
因为我使用cssclass来设置我的Gridview样式,所以当我导出这个Gridview时,我不知道如何将样式复制到Excel。
有人可以帮帮我吗?
答案 0 :(得分:0)
我在这个网站找到了一个很好的解决方案: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();
}
}
我希望这可以帮助一些有同样问题的人。