Results 1 to 3 of 3

Thread: ASP.NET - DataGrid Export to Excel - Alignment problem

  1. #1
    Join Date
    Aug 2006
    Location
    Malaysia
    Posts
    1,576
    Rep Power
    189

    ASP.NET - DataGrid Export to Excel - Alignment problem

    In the UI, the DataGrid width for each column have been defined,
    but when it is exported to the Excel doc, alignment for each column is messed up in the Excel doc.

    The problem is how to align the columns in Excel doc properly, exactly like the UI DataGrid columns.

    Please advise.

    HTML - Define the width of columns
    Code:
               <asp:TemplateColumn HeaderText="No">           <HeaderStyle Font-Bold="True" Width="1%"></HeaderStyle>
    ......
    <asp:TemplateColumn HeaderText="No">           <HeaderStyle Font-Bold="True" Width="20%"></HeaderStyle>
    ...........
    <asp:TemplateColumn HeaderText="No">           <HeaderStyle Font-Bold="True" Width="20%"></HeaderStyle>
    ......
    ......
    Code Behind - Export the whole DataGrid to the Excel doc
    Code:
     private void btnExportToDoc_Click(object sender, System.EventArgs e)
     {
      string strTime = DateTime.Now.ToUniversalTime().ToString()+"_"+DateTime.Now.Millisecond.ToString();
      strTime = strTime.Replace("/", "_");
      strTime = strTime.Replace(":", "_");
      strTime = strTime.Replace(" ", "_");
     
      Response.Clear();
      Response.Buffer = true;
      Response.AddHeader("content-disposition", "attachment; filename=FileDoc_"+strTime+".xls");
      Response.ContentType = "application/vnd.ms-excel";
      Response.Charset = "";
      this.EnableViewState = false;
           
      System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
      System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
     
      // Get the HTML for the control.
      dgProdReleaseValid.RenderControl(oHtmlTextWriter);
      // Write the HTML back to the browser.
      Response.Write(oStringWriter.ToString());
    
      Response.End();  
     }

  2. #2
    Join Date
    Aug 2006
    Location
    Malaysia
    Posts
    1,576
    Rep Power
    189
    The solution for this is:

    Code:
    private void btnExportToDoc_Click(object sender, System.EventArgs e)
    {
    ...
    ...
    		// Remove unneccessary controls of the Datagrid.
    		RemoveDatagridControls(dgProdReleaseValid);
    				
    		// Output the DataGrid control content to the HTML TextWriter.
    		dgProdReleaseValid.RenderControl(oHtmlTextWriter);
    ...
    ...
    }
    
    		/// <remarks>
    		/// Note
    		/// -----
    		/// This is used to override void VerifyRenderingInServerForm to 
    		/// avoid the below error when "Export to Doc" is clicked
    		/// as this is a bug reported by MS.
    		/// http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackid=e4e08db1-8788-4e58-9e1f-daba86195d9f
    		///
    		/// The error message looks like:
    		/// Server Error in '/PPS' Application. 
    		/// --------------------------------------------------------------------------------
    		/// Control 'dgProdReleaseValid__ctl2_chkRelease' of type 'CheckBox' must be placed inside a form tag with runat=server. 
    		/// Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 		
    		///
    		/// References:
    		/// GridView Export to Excel Problems
    		/// http://geekswithblogs.net/azamsharp/archive/2005/07/25/48213.aspx
    		/// </remarks>
    		public override void VerifyRenderingInServerForm(Control control)
    		{
    			// Confirms that an HtmlForm control is rendered 
    			// for the specified ASP.NET server control at run time.
    			
    			// No code is required here.
    		}
    		
    		/// <summary>
    		/// Gets the custom date time format as a part of the filename.
    		/// </summary>
    		/// <remarks>
    		/// References:
    		/// - String Formatting in C#
    		/// http://blog.stevex.net/index.php/string-formatting-in-csharp/					
    		/// <summary>
    		/// Removes unneccessary controls of a DataGrid.
    		/// </summary>
    		/// <remarks>
    		/// References:
    		/// - Extensive Study of GridView Export to Excel (Used by this method)
    		/// http://www.gridviewguy.com/ArticleDetails.aspx?articleID=197
    		/// - Export ASP.NET DataGrid to Excel
    		/// http://www.c-sharpcorner.com/Code/2003/Sept/ExportASPNetDataGridToExcel.asp
    		/// </remarks>
    		private void RemoveDatagridControls(Control dg)
    		{
    			LinkButton lb = new LinkButton();
    			Literal lit = new Literal(); 
    			int i;
    			
    			for (i = 0; i < dg.Controls.Count; i++)
    			{
    				// If LinkButton type is found, remove it.
    				if (dg.Controls[i].GetType() == typeof(LinkButton))
    				{
    					if (dg.Controls[i].Visible)
    					{
    						lit.Text = (dg.Controls[i] as LinkButton).Text; 
    						dg.Controls.Remove(dg.Controls[i]);
    						dg.Controls.AddAt(i, lit);
    					} // End if.
    				}
    				// If DropDownList type is found, remove it.
    				else if (dg.Controls[i].GetType() == typeof(DropDownList))
    				{
    					lit.Text = (dg.Controls[i] as DropDownList).SelectedItem.Text;
    					dg.Controls.Remove(dg.Controls[i]);
    					dg.Controls.AddAt(i, lit);
    				}  // End if.
    
    				if (dg.Controls[i].HasControls())
    				{
    					RemoveDatagridControls(dg.Controls[i]); 
    				} // End if.
    			} // End for.
    		}

  3. #3
    Join Date
    Apr 2007
    Location
    PJ
    Posts
    60
    Rep Power
    152

    Guide to share

    Please find more guides from here

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. WTA: adjust on Wordpress date alignment
    By calvynlee in forum Website Programming
    Replies: 3
    Last Post: 13-12-2008, 02:42 PM
  2. problem import data drpd ms excel ke MySQL
    By mys_az in forum Website Programming
    Replies: 6
    Last Post: 20-09-2007, 11:54 AM
  3. Export hugh data from MySQL
    By Kongtechnology in forum Website Programming
    Replies: 3
    Last Post: 16-07-2007, 04:35 PM
  4. mcmana nak export excel ke mysql
    By poshberry in forum Website Programming
    Replies: 15
    Last Post: 04-06-2004, 04:29 PM
  5. Camane nak export flash jadi movie clip ?
    By myravens in forum Website Design
    Replies: 0
    Last Post: 12-08-2003, 05:07 PM

Posting Permissions

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

Search Engine Optimization by vBSEO 3.5.0 RC1 PL1

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112