Results 1 to 6 of 6

Thread: Exporting Grid Data for Excel, CSV, XML, HTML, PDF [EXAMPLE Codes]

  1. #1
    Member
    Join Date
    Mar 2011
    Location
    Istanbul/TURKIYE
    Posts
    31

    Exporting Grid Data for Excel, CSV, XML, HTML, PDF [EXAMPLE Codes]

    I have referenced this page; http://examples1.ext.net/#/GridPanel...ort_Data_Ajax/

    and iTextSharp; http://sourceforge.net/projects/itextsharp/

    and this page; http://forums.ext.net/showthread.php...EventMask-Hide

    Code:
    	<script type="text/javascript">
    		var ExportYap = function () {
    			Ext.net.Mask.show();
    			Ext.net.Mask.hide.defer(500);
    		};
    	</script>
    Code:
    <TopBar>
    							<ext:Toolbar ID="Toolbar1" runat="server">
    								<Items>
    									<ext:ToolbarFill ID="ToolbarFill1" runat="server" />
    									<ext:Button ID="ButtonXLS" runat="server" Text="XLS" Icon="PageWhiteExcel">
    										<DirectEvents>
    											<Click OnEvent="ExportEt" IsUpload="true" Before="ExportYap()">
    												<ExtraParams>
    													<ext:Parameter Name="data" Value="#{GridPanel1}.getRowsValues()" Mode="Raw" Encode="true" />
    													<ext:Parameter Name="format" Value="xls" Mode="Value" />
    												</ExtraParams>
    											</Click>
    										</DirectEvents>
    									</ext:Button>
    									<ext:Button ID="ButtonDOC" runat="server" Text="DOC" Icon="PageWhiteWord">
    										<DirectEvents>
    											<Click OnEvent="ExportEt" IsUpload="true" Before="ExportYap()">
    												<ExtraParams>
    													<ext:Parameter Name="data" Value="#{GridPanel1}.getRowsValues()" Mode="Raw" Encode="true" />
    													<ext:Parameter Name="format" Value="doc" Mode="Value" />
    												</ExtraParams>
    											</Click>
    										</DirectEvents>
    									</ext:Button>
    									<ext:Button ID="ButtonCSV" runat="server" Text="CSV" Icon="PageAttach">
    										<DirectEvents>
    											<Click OnEvent="ExportEt" IsUpload="true" Before="ExportYap()">
    												<ExtraParams>
    													<ext:Parameter Name="data" Value="#{GridPanel1}.getRowsValues()" Mode="Raw" Encode="true" />
    													<ext:Parameter Name="format" Value="csv" Mode="Value" />
    												</ExtraParams>
    											</Click>
    										</DirectEvents>
    									</ext:Button>
    									<ext:Button ID="ButtonXML" runat="server" Text="XML" Icon="PageWhiteCode">
    										<DirectEvents>
    											<Click OnEvent="ExportEt" IsUpload="true" Before="ExportYap()">
    												<ExtraParams>
    													<ext:Parameter Name="data" Value="#{GridPanel1}.getRowsValues()" Mode="Raw" Encode="true" />
    													<ext:Parameter Name="format" Value="xml" Mode="Value" />
    												</ExtraParams>
    											</Click>
    										</DirectEvents>
    									</ext:Button>
    									<ext:Button ID="ButtonHTML" runat="server" Text="HTML" Icon="PageWhiteH">
    										<DirectEvents>
    											<Click OnEvent="ExportEt" IsUpload="true" Before="ExportYap()">
    												<ExtraParams>
    													<ext:Parameter Name="data" Value="#{GridPanel1}.getRowsValues()" Mode="Raw" Encode="true" />
    													<ext:Parameter Name="format" Value="html" Mode="Value" />
    												</ExtraParams>
    											</Click>
    										</DirectEvents>
    									</ext:Button>
    									<ext:Button ID="ButtonPDF" runat="server" Text="PDF" Icon="PageWhiteAcrobat">
    										<DirectEvents>
    											<Click OnEvent="ExportEt" IsUpload="true" Before="ExportYap()">
    												<ExtraParams>
    													<ext:Parameter Name="data" Value="#{GridPanel1}.getRowsValues()" Mode="Raw" Encode="true" />
    													<ext:Parameter Name="format" Value="pdf" Mode="Value" />
    												</ExtraParams>
    											</Click>
    										</DirectEvents>
    									</ext:Button>
    								</Items>
    							</ext:Toolbar>
    						</TopBar>
    Code:
    protected void ExportEt(object sender, DirectEventArgs e)
    	{
    		try
    		{
    			#region Convert Data JSON
    			object o = JSON.Deserialize<object>(e.ExtraParams["data"]); 
    			#endregion
    
    			#region JSON for use XML
    			StringBuilder SB = new StringBuilder();
    			SB.Append("{");
    			SB.Append("\"?xml\":");
    			SB.Append("{");
    			SB.Append("\"@version\": \"1.0\",");
    			SB.Append("\"@standalone\": \"no\"");
    			SB.Append("},");
    			SB.Append("\"records\":");
    			SB.Append("{");
    			SB.Append("\"record\":");
    			SB.Append(o.ToString());
    			SB.Append("}}"); 
    			#endregion
    
    			#region Convert JSON to XML
    			XmlDocument XD = (XmlDocument)JsonConvert.DeserializeXmlNode(@SB.ToString());
    			XmlNode XN = XD as XmlNode; 
    			#endregion
    
    			#region Clear Buffer
    			Response.Clear(); 
    			#endregion
    
    			switch (e.ExtraParams["format"].ToString())
    			{
    				#region Document Type XML
    				case "xml":
    					string strXml = XN.OuterXml;
    					Response.AddHeader("Content-Disposition", "attachment; filename=" + SayfaAdi + ".xml");
    					Response.Cache.SetCacheability(HttpCacheability.NoCache);
    					Response.AddHeader("Content-Length", strXml.Length.ToString());
    					Response.ContentType = "application/xml";
    					Response.Write(strXml);
    					break; 
    				#endregion
    
    				#region Document Type XLS
    				case "xls":
    					Response.ContentType = "application/vnd.ms-excel";
    					Response.AddHeader("Content-Disposition", "attachment; filename=" + SayfaAdi + ".xls");
    					Response.Cache.SetCacheability(HttpCacheability.NoCache);
    					XslCompiledTransform XCT1 = new XslCompiledTransform();
    					XCT1.Load(Server.MapPath("xsl/excel.xsl"));
    					XCT1.Transform(XN, null, Response.OutputStream);
    					break; 
    				#endregion
    
    				#region Document Type CSV
    				case "csv":
    					Response.ContentType = "application/octet-stream";
    					Response.AddHeader("Content-Disposition", "attachment; filename=" + SayfaAdi + ".csv");
    					Response.Cache.SetCacheability(HttpCacheability.NoCache);
    					XslCompiledTransform XCT2 = new XslCompiledTransform();
    					XCT2.Load(Server.MapPath("xsl/csv.xsl"));
    					XCT2.Transform(XN, null, Response.OutputStream);
    					break; 
    				#endregion
    
    				#region Document Type PDF
    				case "pdf":
    					using (XmlNodeReader XNR = new XmlNodeReader(XN))
    					{
    						using (DataSet DS = new DataSet())
    						{
    							DS.ReadXml(XNR);
    
    							using (System.Web.UI.WebControls.GridView GridView1 = new System.Web.UI.WebControls.GridView())
    							{
    								GridView1.AllowPaging = false;
    								GridView1.DataSource = DS.Tables[0];
    								GridView1.DataBind();
    
    								Response.ContentType = "application/pdf";
    								Response.AddHeader("content-disposition", "attachment;filename=" + SayfaAdi + ".pdf");
    								Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
    								using (StringWriter SW = new StringWriter())
    								{
    									using (HtmlTextWriter hw = new HtmlTextWriter(SW))
    									{
    										GridView1.RenderControl(hw);
    
    										using (StringReader sr = new StringReader(SW.ToString()))
    										{
    											using (Document D = new Document(PageSize.A4, 10f, 10f, 10f, 0f))
    											{
    												using (HTMLWorker HW = new HTMLWorker(D))
    												{
    													using (PdfWriter.GetInstance(D, Response.OutputStream))
    													{
    														D.Open();
    														HW.Parse(sr);
    														D.Close();
    														Response.Write(D);
    													}
    												};
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    					break; 
    				#endregion
    
    				#region Document Type DOC
    				case "doc":
    					using (XmlNodeReader XNR = new XmlNodeReader(XN))
    					{
    						using (DataSet DS = new DataSet())
    						{
    							DS.ReadXml(XNR);
    
    							using (System.Web.UI.WebControls.GridView GridView1 = new System.Web.UI.WebControls.GridView())
    							{
    								GridView1.AllowPaging = false;
    								GridView1.DataSource = DS.Tables[0];
    								GridView1.DataBind();
    
    								Response.ContentType = "application/vnd.ms-word";
    								Response.AddHeader("content-disposition", "attachment;filename=" + SayfaAdi + ".doc");
    								Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
    								using (StringWriter SW = new StringWriter())
    								{
    									using (HtmlTextWriter HW = new HtmlTextWriter(SW))
    									{
    										GridView1.RenderControl(HW);
    										Response.Output.Write(SW.ToString());
    									}
    								}
    							}
    						}
    					}
    					break; 
    				#endregion
    
    				#region Document Type HTML
    				case "html":
    					using (XmlNodeReader XNR = new XmlNodeReader(XN))
    					{
    						using (DataSet DS = new DataSet())
    						{
    							DS.ReadXml(XNR);
    
    							using (System.Web.UI.WebControls.GridView GridView1 = new System.Web.UI.WebControls.GridView())
    							{
    								GridView1.AllowPaging = false;
    								GridView1.DataSource = DS.Tables[0];
    								GridView1.DataBind();
    
    								Response.ContentType = "text/html";
    								Response.AddHeader("content-disposition", "attachment;filename=" + SayfaAdi + ".html");
    								Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
    								using (StringWriter SW = new StringWriter())
    								{
    									using (HtmlTextWriter HW = new HtmlTextWriter(SW))
    									{
    										GridView1.RenderControl(HW);
    										Response.Output.Write(SW.ToString());
    									}
    								}
    							}
    						}
    					}
    					break; 
    				#endregion
    			}
    
    			#region Close Buffer
    			Response.End(); 
    			#endregion
    		}
    		catch
    		{
    		}
    	}
    I love EXT.Net! I love C#
    Thanks EXT.Net
    Last edited by fatihunal; Jan 17, 2012 at 11:50 PM.

  2. #2
    Ext.NET - Dev Team geoffrey.mcgill's Avatar
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    8,661
    Thanks for sharing!
    Geoffrey McGill
    Ext.NET, Inc.
    Founder, and Dev Team

    Ext.NET Examples | Ext.NET API Docs | ExtJS API Docs | @extnet | Jobs

  3. #3
    Ext.NET - Support Team Daniil's Avatar
    Join Date
    Jun 2010
    Location
    Saint-Petersburg, Russia
    Posts
    27,499
    Thanks for sharing! Moving to the Examples and Extras forum.
    Daniil
    Ext.NET, Inc.
    Support Team

    Ext.NET Examples | Ext.NET API Docs | ExtJS API Docs | Twitter | Jobs

  4. #4
    Member equiman's Avatar
    Join Date
    Nov 2011
    Location
    Medellin - Colombia
    Posts
    137
    Thanks for Sharing! Works perfect for me!

  5. #5
    Member
    Join Date
    Mar 2011
    Location
    Istanbul/TURKIYE
    Posts
    31
    Quote Originally Posted by equiman View Post
    Thanks for Sharing! Works perfect for me!
    Youre wellcome. Enjoy the EXT.Net

  6. #6
    Member inaltec's Avatar
    Join Date
    Jan 2012
    Location
    Medellin - Colombia
    Posts
    66
    Hellow!!! i'm trying to use this code... but generating buttons dinamically. Maybe can you help me with this:

    http://forums.ext.net/showthread.php...nts-usin-ExtJS

    My problem is that don't now how can I use the DirectEvent when I create the Button.

    Thanks in advance!!

Similar Threads

  1. [CLOSED] hide a field while exporting data to Excel
    By Fahd in forum 1.x Premium Help
    Replies: 4
    Last Post: Jun 20, 2012, 4:46 PM
  2. exporting to excel with gridfilters on
    By norphos in forum 1.x Help
    Replies: 0
    Last Post: Oct 18, 2011, 1:14 PM
  3. Replies: 0
    Last Post: Sep 05, 2011, 10:01 AM
  4. Exporting data from the grid
    By emon in forum 1.x Help
    Replies: 0
    Last Post: Mar 05, 2011, 3:40 AM
  5. Replies: 4
    Last Post: Jan 17, 2011, 3:35 PM

Tags for this Thread

Posting Permissions