导出到Excel时,删除excel中的第一行内容

时间:2015-08-24 09:51:44

标签: c# asp.net .net export-to-excel

我使用以下代码将gridview导出为ex​​cel。当我导出内容看起来很好没有问题,但我在System.WebControls.UI.Style单元格的第一个单元格中获得了A1这样的控件标签。

有没有办法隐藏这个或删除,下面是我的代码。

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

1 个答案:

答案 0 :(得分:1)

要隐藏第一列或第一行(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++;
}