The Ext.NET team is thrilled to announce the official release of Ext.NET 4, including the new Spreadsheet Selection Model, Triton Theme and Advanced Color Picker!

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

  1. #1

    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

    	<script type="text/javascript">
    		var ExportYap = function () {
    			Ext.net.Mask.show();
    			Ext.net.Mask.hide.defer(500);
    		};
    	</script>
    <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>
    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
    Thanks for sharing!
    Geoffrey McGill
    Founder & CEO
  3. #3
    Thanks for sharing! Moving to the Examples and Extras forum.
    Daniil Veriga
    Developer & Support Expert
  4. #4
    Thanks for Sharing! Works perfect for me!
  5. #5
    Quote Originally Posted by equiman View Post
    Thanks for Sharing! Works perfect for me!
    Youre wellcome. Enjoy the EXT.Net
  6. #6
    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

Subscribe now to get the latest Ext.NET happenings in our newsletter.