Go Back   Webmaster Malaysia Forum » Website Design & Development » Website Programming

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 30-08-2006, 06:17 PM
genzy's Avatar
Nowhere Webmaster
 
Join Date: Aug 2006
Location: Malaysia
Posts: 1,581
Rep Power: 59
genzy is on a distinguished road
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();  
 }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-04-2007, 03:30 PM
genzy's Avatar
Nowhere Webmaster
 
Join Date: Aug 2006
Location: Malaysia
Posts: 1,581
Rep Power: 59
genzy is on a distinguished road
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.
		}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 15-04-2007, 11:21 AM
AsiaPartTime's Avatar
Novice Webmaster
 
Join Date: Apr 2007
Location: PJ
Posts: 60
Rep Power: 21
AsiaPartTime is on a distinguished road
Guide to share

Please find more guides from here
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
mcmana nak export excel ke mysql poshberry Website Programming 15 04-06-2004 04:29 PM
can excel VBA 97 be converted to XML? ymhy_80 Website Design 1 26-02-2004 01:15 PM
convert excel vba to webpage ymhy_80 Websites Review and Suggestion 1 20-02-2004 11:50 AM
Excel ke MySQL baddai Website Programming 2 01-11-2003 10:18 AM
Camane nak export flash jadi movie clip ? myravens Website Design 0 12-08-2003 05:07 PM


All times are GMT +8. The time now is 01:53 PM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0 vBulletin skin by ForumMonkeys.com.


WebmasterMalaysia.com is Proudly Hosted by Exabytes Semi Dedicated Server.

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