开发者

Remove Image in excel header after exporting the gidview

开发者 https://www.devze.com 2023-04-01 07:46 出处:网络
Kindly help me on how to remove image in my excel header. I generate the excel using export command using gridview as source data

Kindly help me on how to remove image in my excel header. I generate the excel using export command using gridview as source data

here is my code

Response.AddHeader("content-disposition", "attachment;filename=" & strFilename & ".xls")
        Response.Clear()
 开发者_运维知识库       Response.Charset = ""
        Response.ContentType = "application/vnd.xls"


        Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
        Dim htw As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(sw)


        grvData.AllowPaging = False
        grvData.AllowSorting = False
        PopulateGrid()

        grvData.RenderControl(htw)
        Response.Write(sw.ToString)
        Response.End()

Everything was set -except that one my header had a blank header name because of the image that was now shown - the image is came from gridview (I'm using arrow for asc and desc) - sorry i cant post any image here now


Give this ClearControls method a shot. It should remove any controls from the Grid before exporting:

protected void btnExport_Click(object sender, EventArgs e)
{       
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";

    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

    this.ClearControls(DataGrid1);
    DataGrid1.RenderControl(oHtmlTextWriter);

    DataGrid1.GridLines = GridLines.Both;
    DataGrid1.Style.Clear();

    Response.Write(oStringWriter.ToString());
    Response.End();
}

private void ClearControls(Control control)
{
    for (int i = control.Controls.Count - 1; i >= 0; i--)
    {
        ClearControls(control.Controls[i]);
    }

    if (!(control is TableCell))
    {
        if (control.GetType().GetProperty("SelectedItem") != null)
        {
            LiteralControl literal = new LiteralControl();
            control.Parent.Controls.Add(literal);

            try
            { 
                literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null); 
            }
            catch
            { 
                //do nothing 
            }
            finally
            { 
                control.Parent.Controls.Remove(control); 
            }
        }
        else
        {
            if (control.GetType().GetProperty("Text") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                control.Parent.Controls.Remove(control);
            }
        }
    }

    return;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消