【问题标题】:label backbround color not working properly when export to excel in asp.net在asp.net中导出到excel时标签背景颜色无法正常工作
【发布时间】:2014-01-16 05:09:39
【问题描述】:

谁能帮我解决这个问题 我正在动态地为标签提供背景颜色,但是当我导出到 excel 时它没有显示在 excel 表中 她是我的密码

DataSet ds = DST;
ds.Tables.Remove(ds.Tables[0]);

// string style = @"<style> .text { mso-number-format:\@Medium Date; } </style> ";

lblTitle.Text = "<h3 border='0' align='center'><b>Sales Report </b></h3>";
HtmlForm form = new HtmlForm();
string attachment = "attachment; filename=BPOSalesReport.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";

StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
GridView gv;
Panel PNl = new Panel();
Label lbldate;
LiteralControl ltlbldate;

for (int k = 0; k < ds.Tables.Count; k++)
{
    gv = new GridView();
    lbldate = new Label();
    ltlbldate = new LiteralControl();
    //lbldate.CssClass = "required";
    gv.ID = ds.Tables[k].TableName;
    gv.DataSource = ds.Tables[k];
    gv.DataBind();
    gv.HeaderRow.Style.Add("background-color", "#6699FF");
    gv.HeaderStyle.ForeColor = System.Drawing.Color.Black;     

    lbldate.Text = datechecking(ds.Tables[k].TableName.ToString());
    lbldate.BackColor = System.Drawing.Color.Yellow;
    PNl.Controls.Add(lbldate);
    PNl.Controls.Add(gv);
    PNl.Controls.Add(new LiteralControl("<br />"));
    PNl.Controls.Add(new LiteralControl("<br />"));
    PNl.Controls.Add(new LiteralControl("<br />"));
}       

form.Attributes["runat"] = "server";
form.Controls.Add(PNl);
this.Controls.Add(form);
lblTitle.Visible = true;
lblTitle.RenderControl(htextw);
form.RenderControl(htextw);
// Response.Write(style);

Response.Write(stw.ToString());
Response.End();

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    这行代码可能会对你有所帮助。

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Details.xls"));
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        //Change the Header Row back to white color
        GridView1.HeaderRow.Style.Add("background-color", "#000000");
        //Applying stlye to gridview header cells
        for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
        {
            GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#000000");
        }
        int j = 1;
        //This loop is used to apply stlye to cells based on particular row
        foreach (GridViewRow gvrow in GridView1.Rows)
        {
    
            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", "#FFFFFF");
                    }
                }
            }
            j++;
        }
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
    

    【讨论】:

    • grid 工作正常,但导出到 excel 时标签不显示
    猜你喜欢
    • 1970-01-01
    • 2019-01-05
    • 2023-02-09
    • 1970-01-01
    • 2021-02-16
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    相关资源
    最近更新 更多