2011-09-02 27 views
0

請幫助我如何刪除excel標題中的圖像。我生成使用export命令使用的GridView作爲源數據在導出gidview後在excel標題中刪除圖像

這裏是我的代碼

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() 
Excel的

一切都被設置-except一個我的頭有,因爲這是現在顯示的圖像的空白頭名 - 圖像來自gridview(我正在使用箭頭asc和desc) - 對不起,我現在不能在這裏發佈任何圖像

+0

目前還不清楚(對我而言)你想要什麼。你想在Excel中顯示圖像還是你想隱藏它?你知道你並不是真的生成一個excel文件,而只是帶有xls擴展名的HTML,它可以正確處理excel。 –

回答

0

給這個ClearControls方法一槍。它應該在出口之前從網格中移除任何控件:

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; 
} 
相關問題