Results 1 to 3 of 3

Thread: Export Grid vision to Excel in.Net

  1. #1
    BarnesHarris is offline Senior Member
    Join Date
    Dec 2009
    Posts
    213
    Rep Power
    3

    Default Export Grid vision to Excel in.Net

    I am working one live project. I am developing one bank application using .net, In that i want to export a user information of gridview to the excel file. I tried various codes but none of them worked out. Anybody have proper code related with this then let me know that. Please reply me as soon as possible.

  2. #2
    HowardAllen is offline Senior Member
    Join Date
    Dec 2009
    Posts
    196
    Rep Power
    3

    Default

    Code:
    public void ExportGridToExcel(string FileName, DataGrid ToExport)
    		{
    			string strBody = FileIO.GetMailContentFile("GenericGridExport.htm");
    
    			ToExport.GridLines = GridLines.Both;
    			ToExport.BorderColor = System.Drawing.Color.Black;
    			ToExport.BorderWidth = Unit.Pixel(1);
    			
    			foreach (DataGridItem i in ToExport.Items)
    			{
    				foreach (Control cnt in i.Controls)
    				{
    					RemoveHyperlinkURL(cnt);
    				}
    
    				if (i.ItemType == ListItemType.Header)
    				{
    					foreach (Control cnt in i.Controls)
    					{
    						RemoveAnchorURL(cnt);
    					}
    				}
    			}
    
    			HttpResponse objResponse = HttpContext.Current.Response;
    
    			objResponse.Clear();
    			objResponse.ContentType = "application/vnd.ms-excel";
    			objResponse.Charset = "";
    			objResponse.AddHeader ("content-disposition", "attachment; filename="" + FileName + """);
    			
    			StringWriter txtWriter = new StringWriter();
    			HtmlTextWriter writer = new HtmlTextWriter(txtWriter);
    			ToExport.RenderControl(writer);
    
    			string strGrid = txtWriter.ToString();
    			txtWriter.Close();
    
    			string strStyle = FileIO.GetTextFileDataVirtual("/interface/BlueStyleSheet.css");
    			
    			strBody = strBody.Replace("#grid#", strGrid);
    			strBody = strBody.Replace("#style#", strStyle);
    			
    			objResponse.Write(strBody);
    			objResponse.End();
    		}
    
    		public void ExportRepeaterToExcel(string FileName, Repeater ToExport)
    		{
    			HttpResponse objResponse = HttpContext.Current.Response;
    
    			string strBody = FileIO.GetMailContentFile("GenericGridExport.htm");
    
    			foreach (RepeaterItem i in ToExport.Items)
    			{
    				foreach (Control cnt in i.Controls)
    				{
    					RemoveHyperlinkURL(cnt);
    				}
    			}
    
    			objResponse.Clear();
    			objResponse.ContentType = "application/vnd.ms-excel";
    			objResponse.Charset = "";
    			objResponse.AddHeader ("content-disposition", "attachment; filename="" + FileName + """);
    			
    			StringWriter txtWriter = new StringWriter();
    			HtmlTextWriter writer = new HtmlTextWriter(txtWriter);
    			ToExport.RenderControl(writer);
    
    			string strGrid = txtWriter.ToString();
    			txtWriter.Close();
    
    			string strStyle = FileIO.GetTextFileDataVirtual("/interface/StandardStyles.css");
    			
    			strBody = strBody.Replace("#grid#", strGrid);
    			strBody = strBody.Replace("#style#", strStyle);
    			
    			objResponse.Write(strBody);
    			objResponse.End();
    		}*/
    
    		public static bool CanBindDataSet(DataSet ToCheck)
    		{
    			if (ToCheck.Tables.Count == 0)
    				return false;
    
    			if (ToCheck.Tables[0].Rows.Count == 0)
    				return false;
    
    			return true;
    		}
    
    		public void RemoveHyperlinkURL(Control CurrentControl)
    		{
    			if (CurrentControl == null)
    				return;
    
    			if (CurrentControl.GetType() == typeof(HyperLink))
    			{
    				HyperLink hyper = (HyperLink) CurrentControl;
    				hyper.NavigateUrl = "";
    				hyper.CssClass = "";
    			}
    			else
    			{
    				foreach (Control cnt in CurrentControl.Controls)
    				{
    					if (cnt.GetType() == typeof(HyperLink))
    					{
    						HyperLink hyper = (HyperLink) cnt;
    						hyper.NavigateUrl = "";
    						hyper.CssClass = "";
    					}
    					else
    					{
    						if (cnt.Controls.Count > 0)
    							RemoveHyperlinkURL(cnt);
    					}
    				}
    			}
    		}
    
    		public void RemoveAnchorURL(Control CurrentControl)
    		{
    			if (CurrentControl == null)
    				return;
    
    			if (CurrentControl.GetType() == typeof(System.Web.UI.HtmlControls.HtmlAnchor))
    			{
    				System.Web.UI.HtmlControls.HtmlAnchor hyper = (System.Web.UI.HtmlControls.HtmlAnchor) CurrentControl;
    				hyper.HRef = "";
    				//hyper.Class = "";
    			}
    			else
    			{
    				foreach (Control cnt in CurrentControl.Controls)
    				{
    					if (cnt.GetType() == typeof(System.Web.UI.HtmlControls.HtmlAnchor))
    					{
    						System.Web.UI.HtmlControls.HtmlAnchor hyper = (System.Web.UI.HtmlControls.HtmlAnchor) cnt;
    						hyper.HRef = "";
    						//hyper.CssClass = "";
    					}
    					else
    					{
    						if (cnt.Controls.Count > 0)
    							RemoveAnchorURL(cnt);
    					}
    				}
    			}
    		}
    
    		public void RemoveCheckBox(Control CurrentControl)
    		{
    			if (CurrentControl == null)
    				return;
    
    			if (CurrentControl.GetType() == typeof(CheckBox))
    			{
    				CheckBox chk = (CheckBox) CurrentControl;
    				chk.Visible = false;
    			}
    			else
    			{
    				foreach (Control cnt in CurrentControl.Controls)
    				{
    					if (cnt.GetType() == typeof(CheckBox))
    					{
    						CheckBox chk = (CheckBox) CurrentControl;
    						chk.Visible = false;
    					}
    					else
    					{
    						if (cnt.Controls.Count > 0)
    							RemoveCheckBox(cnt);
    					}
    				}
    			}
    		}

  3. #3
    MyersGray is offline Senior Member
    Join Date
    Dec 2009
    Posts
    197
    Rep Power
    3

    Default

    Refer the given problem. It is very simple code.

    Code:
    Protected Sub Button1_Click(ByVal sender As Object, _
         ByVal e As EventArgs)
    
            Response.AddHeader("content-disposition", _
             "attachment;filename=FileName.xls")
    
            Response.Charset = String.Empty
    
            Response.ContentType = "application/vnd.xls"
    
            Dim sw As System.IO.StringWriter = _
              New System.IO.StringWriter()
    
            Dim hw As System.Web.UI.HtmlTextWriter = _
                New HtmlTextWriter(sw)
    
            GridView1.RenderControl(hw)
    
            Response.Write(sw.ToString())
    
            Response.End()
    
        End Sub

Similar Threads

  1. How to export Importing Contacts from Outlook Express 6
    By joshin in forum Networking Jargons
    Replies: 2
    Last Post: 10-15-2009, 04:45 PM
  2. How to Edit and Export a movie
    By Felipe Pollard in forum Applications
    Replies: 0
    Last Post: 08-26-2009, 08:41 AM
  3. Import and Export blog
    By niks54 in forum Everything Else
    Replies: 0
    Last Post: 03-14-2009, 07:49 AM
  4. Import And Export News Feeds
    By neil97 in forum Applications
    Replies: 0
    Last Post: 08-15-2008, 04:07 PM
  5. Export to PDF
    By zonal5747 in forum Web. 2.0
    Replies: 0
    Last Post: 06-26-2008, 02:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48