2014-01-10 141 views
0

我使用此代碼出口的datagridview到excel導出的GridView到Excel沒有網格

HtmlForm form = new HtmlForm(); 
Response.Clear(); 
Response.Buffer = true; 
string fileName = "TRAIL" + "[" + DatefromTxtBox.Text.Replace("/", "") + "_" + DatetoTxtBox.Text.Replace("/", "") + "]" + ".xls"; 
Response.AddHeader("content-disposition", "attachment;filename=" + fileName); 
Response.Charset = ""; 
Response.ContentType = "application/vnd.ms-excel"; 
StringWriter sw = new StringWriter(); 
HtmlTextWriter hw = new HtmlTextWriter(sw); 
AuditTrailGV.AllowPaging = false; 
AuditTrailGV.DataSource = (DataSet)ViewState["audit"]; 
AuditTrailGV.DataBind(); 
form.Controls.Add(AuditTrailGV); 
this.Controls.Add(form); 
form.RenderControl(hw); 
Response.Output.Write(sw.ToString()); 
Response.Flush(); 
Response.End(); 

的問題是這樣的代碼還捕捉格式/我的GridView的邊界

這裏的屏幕截圖,

這是我在asp.net enter image description here

GridView和這是出現在我的EXCELL enter image description here

正如你所看到的那樣,它改變了所有的行像gridview,我不希望它發生,儘可能多,如果我只能保留數據行的網格線,如果它不可能,刪除所有網格線。

有幫助嗎?我真的不喜歡我excell中的網格線

回答

0

您可以嘗試下面的代碼並自定義下載數據的顏色。它也不會着色列和數據以外的行。

protected void DownloadExcel_Click(object sender, EventArgs e) 
{ 
    Response.ClearContent(); 
    Response.Buffer = true; 
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Report.xls")); 
    Response.ContentType = "application/ms-excel"; 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 
    GridView1.AllowPaging = false; 
    GridView1.DataBind(); 
    GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF"); 

    for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++) 
    { 
     GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#bfc2c7"); 
    } 

    int j = 1; 
    foreach (GridViewRow gvrow in GridView1.Rows) 
    {   
     //gvrow.BackColor = color.White; 
     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", "#EFF3FB");  
       } 
      } 
     } 
     j++; 
    } 
    GridView1.RenderControl(htw); 
    Response.Write(sw.ToString()); 
    Response.End(); 
} 
+0

還是一樣在我的問題的第二張照片。網格線仍然顯示在數據後面 – user2705620

+0

。像上面一樣。, – user2705620

1

VB

Public Overrides Sub VerifyRenderingInServerForm(control As Control) 
    'base.VerifyRenderingInServerForm(control); 
    'This remains empty 
End Sub 

Protected Sub btnExcel_Click(sender As Object, e As ImageClickEventArgs) Handles btnExcel.Click 

    Response.Clear() 
    Response.AddHeader("content-disposition", "attachment;filename=FileName.xls") 
    Response.Charset = "" 
    Response.Cache.SetCacheability(HttpCacheability.NoCache) 
    Response.ContentType = "application/vnd.ms-excel" 

    Dim stringWrite As New System.IO.StringWriter() 
    Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite) 

    htmlWrite.Write("<html xmlns:o=""urn:schemas-microsoft-com:office:office"" ") 
    htmlWrite.Write("xmlns:x=""urn:schemas-microsoft-com:office:excel"" ") 
    htmlWrite.Write("xmlns=""http://www.w3.org/TR/REC-html40""> ") 
    htmlWrite.Write("<head> ") 
    htmlWrite.Write("<!--[if gte mso 9]><xml> ") 
    htmlWrite.Write("<x:ExcelWorkbook> ") 
    htmlWrite.Write("<x:ExcelWorksheets> ") 
    htmlWrite.Write("<x:ExcelWorksheet> ") 
    htmlWrite.Write("<x:Name>Sheet1</x:Name> ") 
    htmlWrite.Write("<x:WorksheetOptions> ") 
    htmlWrite.Write("<x:Selected/> ") 
    htmlWrite.Write("<x:ProtectContents>False</x:ProtectContents> ") 
    htmlWrite.Write("<x:ProtectObjects>False</x:ProtectObjects> ") 
    htmlWrite.Write("<x:ProtectScenarios>False</x:ProtectScenarios> ") 
    htmlWrite.Write("</x:WorksheetOptions> ") 
    htmlWrite.Write("</x:ExcelWorksheet> ") 
    htmlWrite.Write("</x:ExcelWorksheets> ") 
    htmlWrite.Write("</x:ExcelWorkbook> ") 
    htmlWrite.Write("</xml><![endif]--> ") 
    htmlWrite.Write("</head>") 
    htmlWrite.WriteLine("") 

    gridView.HeaderStyle.Reset() 
    gridView.FooterStyle.Reset() 
    gridView.AlternatingRowStyle.Reset() 
    gridView.RowStyle.Reset() 

    gridView.BackColor = Color.Transparent 
    gridView.GridLines = GridLines.None 
    gridView.RenderControl(htmlWrite) 

    Response.Write(stringWrite.ToString()) 
    Response.[End]() 
End Sub 

C#

public override void VerifyRenderingInServerForm(Control control) 
{ 
    //base.VerifyRenderingInServerForm(control); 
    //This remains empty 
} 


protected void btnExcel_Click(object sender, ImageClickEventArgs e) 
{ 
    Response.Clear(); 
    Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); 
    Response.Charset = ""; 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.ContentType = "application/vnd.ms-excel"; 
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 

    htmlWrite.Write("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" "); 
    htmlWrite.Write("xmlns:x=\"urn:schemas-microsoft-com:office:excel\" "); 
    htmlWrite.Write("xmlns=\"http://www.w3.org/TR/REC-html40\"> "); 
    htmlWrite.Write("<head> "); 
    htmlWrite.Write("<!--[if gte mso 9]><xml> "); 
    htmlWrite.Write("<x:ExcelWorkbook> "); 
    htmlWrite.Write("<x:ExcelWorksheets> "); 
    htmlWrite.Write("<x:ExcelWorksheet> "); 
    htmlWrite.Write("<x:Name>Sheet1</x:Name> "); 
    htmlWrite.Write("<x:WorksheetOptions> "); 
    htmlWrite.Write("<x:Selected/> "); 
    htmlWrite.Write("<x:ProtectContents>False</x:ProtectContents> "); 
    htmlWrite.Write("<x:ProtectObjects>False</x:ProtectObjects> "); 
    htmlWrite.Write("<x:ProtectScenarios>False</x:ProtectScenarios> "); 
    htmlWrite.Write("</x:WorksheetOptions> "); 
    htmlWrite.Write("</x:ExcelWorksheet> "); 
    htmlWrite.Write("</x:ExcelWorksheets> "); 
    htmlWrite.Write("</x:ExcelWorkbook> "); 
    htmlWrite.Write("</xml><![endif]--> "); 
    htmlWrite.Write("</head>"); 
    htmlWrite.WriteLine(""); 

    gridView.HeaderStyle.Reset(); 
    gridView.FooterStyle.Reset(); 
    gridView.AlternatingRowStyle.Reset(); 
    gridView.RowStyle.Reset(); 

    gridView.BackColor = Color.Transparent; 
    gridView.GridLines = GridLines.None; 
    gridView.RenderControl(htmlWrite); 

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

}