{"id":2299,"date":"2011-07-29T18:48:14","date_gmt":"2011-07-29T13:18:14","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2299"},"modified":"2011-07-29T18:48:14","modified_gmt":"2011-07-29T13:18:14","slug":"tutorial-read-and-export-excel-file-in-asp-net-using-c","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/tutorial-read-and-export-excel-file-in-asp-net-using-c\/","title":{"rendered":"Tutorial &#8211; Read and export excel file in ASP.Net  using C#"},"content":{"rendered":"<p>Hi Readers,<br \/>\nIn this article, i am going to show you that how to read excel file in C# and exporting it into ASP.Net. You might need this type of code when you want to read the file from server and export to the client.<br \/>\nFollowing will be the application look like :<\/p>\n<p><figure id=\"attachment_2301\" aria-describedby=\"caption-attachment-2301\" style=\"width: 465px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/07\/Read-And-Export-Excel-in-ASP.Net_.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2301\" title=\"Read And Export Excel in ASP.Net\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/07\/Read-And-Export-Excel-in-ASP.Net_.png?resize=465%2C339&#038;ssl=1\" alt=\"Read And Export Excel in ASP.Net\" width=\"465\" height=\"339\" \/><\/a><figcaption id=\"caption-attachment-2301\" class=\"wp-caption-text\">Read And Export Excel in ASP.Net<\/figcaption><\/figure><br \/>\n<!--more--><br \/>\n<strong>Asp.Net Code:<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot;  CodeFile=&quot;Default.aspx.cs&quot; Inherits=&quot;_Default&quot; %&gt;\n\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot; &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&gt;\n\n&lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot; &gt;\n&lt;head runat=&quot;server&quot;&gt;\n    &lt;title&gt;Untitled Page&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;\n    &lt;div&gt;\n        &lt;asp:Button ID=&quot;btnExport&quot; Text=&quot;Click To Export&quot; runat=&quot;server&quot; OnClick=&quot;btnExport_Click&quot; \/&gt;\n        &lt;br \/&gt;\n        &lt;strong&gt;&lt;span style=&quot;text-decoration: underline&quot;&gt;\n        Note :&lt;\/span&gt;&lt;\/strong&gt; Excel path is picked up from web.Config\n    &lt;\/div&gt;\n    &lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p><strong>C# Code :<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Data;\nusing System.Configuration;\nusing System.Web;\nusing System.Web.Security;\nusing System.Web.UI;\nusing System.Web.UI.WebControls;\nusing System.Web.UI.WebControls.WebParts;\nusing System.Web.UI.HtmlControls;\nusing System.IO;\n\npublic partial class _Default : System.Web.UI.Page\n{\n    protected void Page_Load(object sender, EventArgs e)\n    {\n\n    }\n    protected void btnExport_Click(object sender, EventArgs e)\n    {\n        try\n        {\n            string filePath = string.Empty;\n            if (ConfigurationManager.AppSettings&#x5B;&quot;FilePath&quot;] != null)\n            {\n                filePath = ConfigurationManager.AppSettings&#x5B;&quot;FilePath&quot;].ToString();\n            }\n\n            byte&#x5B;] document = this.StreamFile(filePath);\n            Response.Clear();\n            Response.AddHeader(&quot;content-disposition&quot;, &quot;attachment;filename=TestExcel&quot; + Convert.ToString(DateTime.Now.ToFileTimeUtc()) + &quot;.xls&quot;);\n            Response.Charset = &quot;&quot;;\n            Response.Cache.SetNoServerCaching();\n            Response.ContentType = &quot;application\/ms-excel&quot;;\n            Response.BinaryWrite(document);\n            Response.End();\n        }\n        catch (Exception ex)\n        {\n            Page.RegisterStartupScript(&quot;error&quot;, &quot;&lt;script&gt;alert('Some error occured. Please contact admin. ');&lt;\/script&gt;&quot;);\n        }\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Streams the file.\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=&quot;filename&quot;&gt;The filename.&lt;\/param&gt;\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n    \/\/\/ &lt;remarks&gt;&lt;\/remarks&gt;\n    private byte&#x5B;] StreamFile(string filename)\n    {\n        byte&#x5B;] ImageData = new byte&#x5B;0];\n        FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);\n        try\n        {\n            \/\/ Create a byte array of file stream length\n            ImageData = new byte&#x5B;fs.Length];\n            \/\/Read block of bytes from stream into the byte array\n            fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));\n            \/\/Close the File Stream\n\n        }\n        catch (Exception ex)\n        {\n            throw ex;\n        }\n        finally\n        {\n            if (fs != null)\n            {\n                fs.Close();\n            }\n        }\n        return ImageData;\n        \/\/return the byte data\n    }\n}\n<\/pre>\n<p><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/07\/ReadAndExportExcel.zip\">Download source code of Read and Export to excel<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial &#8211; Read and export excel file in ASP.Net  using C#<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[21,22],"tags":[43,51],"class_list":["post-2299","post","type-post","status-publish","format-standard","hentry","category-net","category-csharp","tag-asp-net","tag-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":827,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/add-css-programatically-in-asp-net\/","url_meta":{"origin":2299,"position":0},"title":"Add CSS Programatically in ASP.NET","author":"Jitendra","date":"September 3, 2010","format":false,"excerpt":"Tutorial on adding the CSS file dynamically in ASP.NET and C# \/ VB.Net","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":132,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/upload-files-in-asp-net-at-production-server\/","url_meta":{"origin":2299,"position":1},"title":"Upload Files in ASP.NET at Production server","author":"Jitendra","date":"May 13, 2010","format":false,"excerpt":"In this article, i am going to demonstrate that how to upload the file in ASP.NET production server. Most of the case, a developer created a code to upload the file and test it on his local machine. program runs smoothly, but as he\u00a0forward\u00a0the same code on production. He\u00a0stuck\u00a0in the\u2026","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/WebShare-Folder.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":15,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/app_offline-htm-in-asp-net\/","url_meta":{"origin":2299,"position":2},"title":"App_Offline.htm in ASP.NET","author":"Jitendra","date":"May 5, 2010","format":false,"excerpt":"If you place a file with the name App_Offline.htm in the root of a web application directory, ASP.NET 2.0 will shut-down the application, unload the application domain from the server, and stop processing any new incoming requests for that application. ASP.NET will also then respond to all requests for dynamic\u2026","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":150,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/handlers-url-mapping-in-asp-net\/","url_meta":{"origin":2299,"position":3},"title":"Handlers in ASP.NET","author":"Jitendra","date":"May 14, 2010","format":false,"excerpt":"HTTP Handlers is\u00a0a new technique presented in ASP.NET that was not present in the \"Classic ASP\".\u00a0HTTP Handlers are components that implement the System.Web.IHttpHandler interface. Unlike ASP.NET pages\u00a0Handlers dont have HTML-markup file, no events and other supporting. All they have is a code-file (written in any .NET-compatible language) that writes some\u2026","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Step 1","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/AddProject.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":608,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/selecting-and-sorting-in-datatable-asp-net-c\/","url_meta":{"origin":2299,"position":4},"title":"Selecting and Sorting in DataTable &#8211; ASP.net C#","author":"Jitendra","date":"July 7, 2010","format":false,"excerpt":"Demonstrates that how to select and sort the Datatable in ASP.Net , C#","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Sorting and Selecting DataTable in ASP","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Sorting-and-Selecting-DataTable-in-ASP-300x223.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1138,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/how-to-read-value-from-configuration-file-app-config-or-web-config\/","url_meta":{"origin":2299,"position":5},"title":"How to read value from Configuration file &#8211; ( app.config or web.config )","author":"Jitendra","date":"September 19, 2010","format":false,"excerpt":"How to read value from Configuration file - app.config or web.config in C# application or web application","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"add reference of System.Configuration in .NET","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/System.Configuration-300x250.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2299"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2299\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}