在导出到excel时,删除excel中的第一行内容
本文关键字:excel 一行 删除 | 更新日期: 2023-09-27 18:09:36
我使用下面的代码导出gridview到excel。当我导出内容看起来很好,没有问题,但我在A1
单元格的第一个单元格中得到一个像System.WebControls.UI.Style
这样的控制标签。
是否有办法隐藏这个或删除,下面是我的代码。
CS:protected void Unnamed1_Click(object sender, ImageClickEventArgs e)
{
// ExportToExcel();
if (ViewState["DashboardDetails"] != null)
{
lblError.Text = string.Empty;
lblError.Style.Add("display", "None");
grdHorizontalSeatDashboard.AllowPaging = false;
DataTable dtHorizontalDashboard = (DataTable)ViewState["DashboardDetails"];
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=HorizontalDashboard.xls");
Response.Charset = "utf-8";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
grdHorizontalSeatDashboard.AllowPaging = false;
grdHorizontalSeatDashboard.DataSource = (DataTable)ViewState["DashboardDetails"];
grdHorizontalSeatDashboard.DataBind();
grdHorizontalSeatDashboard.HeaderRow.BackColor = Color.White;
//Horizontal Cell
TableCell cell0 = grdHorizontalSeatDashboard.HeaderRow.Cells[0];
cell0.BackColor = ColorTranslator.FromHtml("#9AD6EE");
cell0.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[0].Width;
TableCell cell1 = grdHorizontalSeatDashboard.HeaderRow.Cells[1];
cell1.BackColor = ColorTranslator.FromHtml("#96B060");
cell1.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[1].Width;
TableCell cell2 = grdHorizontalSeatDashboard.HeaderRow.Cells[2];
cell2.BackColor = ColorTranslator.FromHtml("#96B060");
cell2.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[2].Width;
TableCell cell3 = grdHorizontalSeatDashboard.HeaderRow.Cells[3];
cell3.BackColor = ColorTranslator.FromHtml("#96B060");
cell3.Width = Unit.Pixel(100);
TableCell cell4 = grdHorizontalSeatDashboard.HeaderRow.Cells[4];
cell4.BackColor = ColorTranslator.FromHtml("#C57838");
cell4.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[4].Width;
TableCell cell5 = grdHorizontalSeatDashboard.HeaderRow.Cells[5];
cell5.BackColor = ColorTranslator.FromHtml("#C57838");
cell5.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[5].Width;
TableCell cell6 = grdHorizontalSeatDashboard.HeaderRow.Cells[6];
cell6.BackColor = ColorTranslator.FromHtml("#C57838");
cell6.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[6].Width;
// Likely $ Impact & Overall Horizontal Utilization
TableCell cell10 = grdHorizontalSeatDashboard.HeaderRow.Cells[10];
cell10.BackColor = ColorTranslator.FromHtml("#9AD6EE");
cell10.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[10].Width;
TableCell cell11 = grdHorizontalSeatDashboard.HeaderRow.Cells[11];
cell11.BackColor = ColorTranslator.FromHtml("#9AD6EE");
cell11.Width = grdHorizontalSeatDashboard.HeaderRow.Cells[11].Width;
foreach (GridViewRow row in grdHorizontalSeatDashboard.Rows)
{
foreach (TableCell cell in row.Cells)
{
cell.BackColor = row.BackColor;
cell.HorizontalAlign = HorizontalAlign.Center;
cell.CssClass = "textmode";
}
}
foreach (TableCell cell in grdHorizontalSeatDashboard.FooterRow.Cells)
{
cell.BackColor = grdHorizontalSeatDashboard.FooterRow.BackColor;
cell.HorizontalAlign = HorizontalAlign.Center;
}
Style style = new Style();
style.BackColor = Color.White;
Response.Write(style);
grdHorizontalSeatDashboard.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
}
要隐藏第一列或第一行(A1),只需计算当前行/列迭代并截取第一个并隐藏A1单元格。
var rowCounter = 0;
var cellCounter = 0;
foreach (GridViewRow row in grdHorizontalSeatDashboard.Rows)
{
foreach (TableCell cell in row.Cells)
{
if (rowCounter == 0 && cellCounter == 0)
{
cell.Text = "";
}
cell.BackColor = row.BackColor;
cell.HorizontalAlign = HorizontalAlign.Center;
cell.CssClass = "textmode";
}
cellCounter = 0;
rowCounter++;
}