2013-04-15 15 views
1

我已經嘗試過將整個gridview數據保存到excel文件的代碼,它工作正常,但現在我想只保存gridview的特定列到excel文件。我已經使用mysql數據庫和asp.net與C#。請任何人都幫助我。發送選定的gridview列到excel文件

**default.aspx** 
<asp:GridView ID="GridView1" runat="server"> 
    </asp:GridView> 

    <asp:Button ID="btnSave" runat="server" Text="Save to Excel" OnClick="btnSave_Click" /> 

**default.cs** 
protected void btnSave_Click(object sender, EventArgs e) 
    { 
     ExportGridToExcel(GridView1, "StudentMarks.xls"); 
    } 

    public void ExportGridToExcel(GridView grdGridView, string fileName) 
    { 
     Response.Clear(); 
     Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName)); 
     Response.Charset = ""; 
     Response.ContentType = "application/vnd.xls"; 
     StringWriter stringWrite = new StringWriter(); 
     HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
     grdGridView.RenderControl(htmlWrite); 
     Response.Write(stringWrite.ToString()); 
     Response.End(); 
    } 

    public override void VerifyRenderingInServerForm(Control control) 
    { 
     return; 
    } 

this is my gridview

this is the excel file which is generated

+1

顯示你的代碼,並告訴人們你嘗試過什麼到目前爲止.. –

+0

可以此鏈路解決方案解決您的問題,HTTP://stackoverflow.com/questions/15940895/export-datagridview-with-padding-to-excel/15941307#15941307 –

+0

您可以使用一種方法來生成上述評論鏈接中提到的excel。 –

回答

0

您可以用asp:BoundField的選擇要在GridView中顯示的列。然後將該gridview導出爲ex​​cel。 Ok。在你的情況下,你可以綁定一個新的gridview,但是沒有Edit/Delete列。您可以將這個新的gridview的可見性設置爲false。

+0

你可以請給我完整的代碼如何保存選定的列到excel文件 – rupinder18

+0

其實我有一個gridview,其中我有第一列,其中我已經使用兩個圖像一個是編輯和一個是刪除操作。所以現在當我應用上面的代碼我得到編輯/刪除也列。但我只想將數據保存在excel文件中不是該列。所以請告訴如何實現這個 – rupinder18

+0

亞我知道這是解決方案,或者我們也可以使用數據集對象而不是GridView,但如果我想只保存特定列的GridView到Excel文件...檢查此鏈接http://aspsnippets.com/Articles/ASP.Net-GridView-Export-only-selected-columns-to-Excel-Sheet.aspx – rupinder18

0

生成Excel使用此方法..

protected void btnExportExcel_Click(object sender, EventArgs e) 
     { 
      Response.Clear(); 

      Response.Buffer = true; 
      Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls"); 
      Response.Charset = ""; 
      Response.ContentType = "application/vnd.ms-excel"; 

      StringWriter sw = new StringWriter(); 
      HtmlTextWriter hw = new HtmlTextWriter(sw); 
      GridView.AllowPaging = false; 

      // Re-Bind data to GridView 

      using (MSEntities1 CompObj = new MSEntities1()) 
      { 
       // code for bind grid view 
       GridView.DataBind(); 
      } 

      // Change the Header Row back to white color 

      GridViewC.Style.Add(" font-size", "10px"); 

      GridView.HeaderRow.Style.Add("background-color", "#FFFFFF"); 


      //Apply style to Individual Cells 

      GridView.HeaderRow.Cells[0].Style.Add("background-color", "green"); 
      GridView.HeaderRow.Cells[1].Style.Add("background-color", "green"); 
      GridView.HeaderRow.Cells[2].Style.Add("background-color", "green"); 
      GridView.HeaderRow.Cells[3].Style.Add("background-color", "green"); 
      GridView.HeaderRow.Cells[4].Style.Add("background-color", "green"); 
      GridView.HeaderRow.Cells[5].Style.Add("background-color", "green"); 
      GridView.HeaderRow.Cells[6].Style.Add("background-color", "green"); 
      GridView.HeaderRow.Cells[7].Style.Add("background-color", "green"); 


      int k = GridView.Rows.Count; 

      for (int i = 1; i < GridView.Rows.Count; i++) 
      { 

       GridViewRow row = GridView.Rows[i]; 


       //Change Color back to white 

       row.BackColor = System.Drawing.Color.White; 


       //Apply text style to each Row 

       row.Attributes.Add("class", "textmode"); 


       //Apply style to Individual Cells of Alternating Row 

       if (i % 2 != 0) 
       { 
        row.Cells[0].Style.Add("background-color", "#C2D69B"); 
        row.Cells[1].Style.Add("background-color", "#C2D69B"); 
        row.Cells[2].Style.Add("background-color", "#C2D69B"); 
        row.Cells[3].Style.Add("background-color", "#C2D69B"); 
        row.Cells[4].Style.Add("background-color", "#C2D69B"); 
        row.Cells[5].Style.Add("background-color", "#C2D69B"); 
        row.Cells[6].Style.Add("background-color", "#C2D69B"); 
        row.Cells[7].Style.Add("background-color", "#C2D69B"); 
       } 
      } 
      GridView.RenderControl(hw); 

      // style to format numbers to string. 

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

      Response.Write(style); 
      Response.Output.Write(sw.ToString()); 
      Response.Flush(); 
      Response.End(); 
     } 
+0

但在這種方法中,我們正在檢查哪些gridview列我們要保存到excel文件? – rupinder18

+0

你想在excel中存儲哪一列,根據哪個標準 –

+0

在我的gridview中看到上面我有編輯/刪除的第一列,現在我不想把這個列保存在我的excel文件 – rupinder18