├── ExportToExcel ├── ExportToExcel.suo ├── bin │ └── Debug │ │ ├── Excel.png │ │ ├── ExportToExcel.exe │ │ ├── ExportToExcel.pdb │ │ ├── ExportToExcel.vshost.exe │ │ └── ExportToExcel.vshost.exe.manifest ├── obj │ └── x86 │ │ └── Debug │ │ ├── ExportToExcel.exe │ │ ├── ExportToExcel.pdb │ │ ├── ExportToExcel.frmExcelExport.resources │ │ ├── DesignTimeResolveAssemblyReferences.cache │ │ ├── ExportToExcel.csproj.GenerateResource.Cache │ │ ├── TempPE │ │ └── Properties.Resources.Designer.cs.dll │ │ ├── ExportToExcel.Properties.Resources.resources │ │ ├── DesignTimeResolveAssemblyReferencesInput.cache │ │ ├── ExportToExcel.csprojResolveAssemblyReference.cache │ │ └── ExportToExcel.csproj.FileListAbsolute.txt ├── Properties │ ├── Settings.settings │ ├── Settings.Designer.cs │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ └── Resources.resx ├── Program.cs ├── ExportToExcel.sln ├── frmMain.Designer.cs ├── ExportToExcel.csproj ├── frmMain.resx └── frmMain.cs └── README.md /ExportToExcel/ExportToExcel.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/ExportToExcel.suo -------------------------------------------------------------------------------- /ExportToExcel/bin/Debug/Excel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/bin/Debug/Excel.png -------------------------------------------------------------------------------- /ExportToExcel/bin/Debug/ExportToExcel.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/bin/Debug/ExportToExcel.exe -------------------------------------------------------------------------------- /ExportToExcel/bin/Debug/ExportToExcel.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/bin/Debug/ExportToExcel.pdb -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/ExportToExcel.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/obj/x86/Debug/ExportToExcel.exe -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/ExportToExcel.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/obj/x86/Debug/ExportToExcel.pdb -------------------------------------------------------------------------------- /ExportToExcel/bin/Debug/ExportToExcel.vshost.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/bin/Debug/ExportToExcel.vshost.exe -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/ExportToExcel.frmExcelExport.resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/obj/x86/Debug/ExportToExcel.frmExcelExport.resources -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/ExportToExcel.csproj.GenerateResource.Cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/obj/x86/Debug/ExportToExcel.csproj.GenerateResource.Cache -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/TempPE/Properties.Resources.Designer.cs.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/obj/x86/Debug/TempPE/Properties.Resources.Designer.cs.dll -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/ExportToExcel.Properties.Resources.resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/obj/x86/Debug/ExportToExcel.Properties.Resources.resources -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/ExportToExcel.csprojResolveAssemblyReference.cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/How-to-Export-Excel-in-C-/master/ExportToExcel/obj/x86/Debug/ExportToExcel.csprojResolveAssemblyReference.cache -------------------------------------------------------------------------------- /ExportToExcel/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ExportToExcel/bin/Debug/ExportToExcel.vshost.exe.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /ExportToExcel/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Windows.Forms; 5 | 6 | namespace ExportToExcel 7 | { 8 | static class Program 9 | { 10 | /// 11 | /// The main entry point for the application. 12 | /// 13 | [STAThread] 14 | static void Main() 15 | { 16 | Application.EnableVisualStyles(); 17 | Application.SetCompatibleTextRenderingDefault(false); 18 | Application.Run(new frmExcelExport()); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ExportToExcel/ExportToExcel.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExportToExcel", "ExportToExcel.csproj", "{ECD4934A-501E-4724-B70C-FA5569B1F2AD}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|x86 = Debug|x86 9 | Release|x86 = Release|x86 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {ECD4934A-501E-4724-B70C-FA5569B1F2AD}.Debug|x86.ActiveCfg = Debug|x86 13 | {ECD4934A-501E-4724-B70C-FA5569B1F2AD}.Debug|x86.Build.0 = Debug|x86 14 | {ECD4934A-501E-4724-B70C-FA5569B1F2AD}.Release|x86.ActiveCfg = Release|x86 15 | {ECD4934A-501E-4724-B70C-FA5569B1F2AD}.Release|x86.Build.0 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /ExportToExcel/obj/x86/Debug/ExportToExcel.csproj.FileListAbsolute.txt: -------------------------------------------------------------------------------- 1 | E:\Youtube Channel\Dotnet Mob\C# tutorials\Export toExcel\ExportToExcel\bin\Debug\ExportToExcel.exe 2 | E:\Youtube Channel\Dotnet Mob\C# tutorials\Export toExcel\ExportToExcel\bin\Debug\ExportToExcel.pdb 3 | E:\Youtube Channel\Dotnet Mob\C# tutorials\Export toExcel\ExportToExcel\obj\x86\Debug\ExportToExcel.frmExcelExport.resources 4 | E:\Youtube Channel\Dotnet Mob\C# tutorials\Export toExcel\ExportToExcel\obj\x86\Debug\ExportToExcel.Properties.Resources.resources 5 | E:\Youtube Channel\Dotnet Mob\C# tutorials\Export toExcel\ExportToExcel\obj\x86\Debug\ExportToExcel.csproj.GenerateResource.Cache 6 | E:\Youtube Channel\Dotnet Mob\C# tutorials\Export toExcel\ExportToExcel\obj\x86\Debug\ExportToExcel.exe 7 | E:\Youtube Channel\Dotnet Mob\C# tutorials\Export toExcel\ExportToExcel\obj\x86\Debug\ExportToExcel.pdb 8 | E:\Youtube Channel\Dotnet Mob\C# tutorials\Export toExcel\ExportToExcel\obj\x86\Debug\ExportToExcel.csprojResolveAssemblyReference.cache 9 | -------------------------------------------------------------------------------- /ExportToExcel/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.34003 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace ExportToExcel.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ExportToExcel/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ExportToExcel")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ExportToExcel")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("edab0c0b-4231-4d0b-9d9d-ff79133dd6d7")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to Export Excel in C# 2 | C# Demo project to demonstrate how to export excel in C# applications. 3 | 4 | 5 | ## Get the Code 6 | 7 | ``` 8 | $ git clone https://github.com/CodAffection/Angular-5-Quiz-App-With-Web-API.git 9 | ``` 10 | 11 | ## How it works ? 12 | 13 | :tv: Video tutorial on this same topic 14 | Url : https://youtu.be/yX-JOqV_ZR0 15 | 16 | Video Tutorial for Export Excel in C# 19 | 20 | 21 | | :bar_chart: | List of Tutorials | | :moneybag: | Support Us | 22 | |--------------------------:|:---------------------|---|---------------------:|:-------------------------------------| 23 | | Angular |http://bit.ly/2KQN9xF | |Paypal | https://goo.gl/bPcyXW | 24 | | Asp.Net Core |http://bit.ly/30fPDMg | |Amazon Affiliate | https://geni.us/JDzpE | 25 | | React |http://bit.ly/325temF | | 26 | | Python |http://bit.ly/2ws4utg | | :point_right: | Follow Us | 27 | | Node.js |https://goo.gl/viJcFs | |Website |http://www.codaffection.com | 28 | | Asp.Net MVC |https://goo.gl/gvjUJ7 | |YouTube |https://www.youtube.com/codaffection | 29 | | Flutter |https://bit.ly/3ggmmJz| |Facebook |https://www.facebook.com/codaffection | 30 | | Web API |https://goo.gl/itVayJ | |Twitter |https://twitter.com/CodAffection | 31 | | MEAN Stack |https://goo.gl/YJPPAH | | 32 | | C# Tutorial |https://goo.gl/s1zJxo | | 33 | | Asp.Net WebForm |https://goo.gl/GXC2aJ | | 34 | | C# WinForm |https://goo.gl/vHS9Hd | | 35 | | MS SQL |https://goo.gl/MLYS9e | | 36 | | Crystal Report |https://goo.gl/5Vou7t | | 37 | | CG Exercises in C Program |https://goo.gl/qEWJCs | | 38 | -------------------------------------------------------------------------------- /ExportToExcel/frmMain.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace ExportToExcel 2 | { 3 | partial class frmExcelExport 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows Form Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.btnExport = new System.Windows.Forms.Button(); 32 | this.SuspendLayout(); 33 | // 34 | // btnExport 35 | // 36 | this.btnExport.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 37 | this.btnExport.Image = global::ExportToExcel.Properties.Resources.Excel; 38 | this.btnExport.ImageAlign = System.Drawing.ContentAlignment.MiddleRight; 39 | this.btnExport.Location = new System.Drawing.Point(109, 42); 40 | this.btnExport.Name = "btnExport"; 41 | this.btnExport.Size = new System.Drawing.Size(210, 82); 42 | this.btnExport.TabIndex = 0; 43 | this.btnExport.Text = "Export To"; 44 | this.btnExport.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 45 | this.btnExport.UseVisualStyleBackColor = true; 46 | this.btnExport.Click += new System.EventHandler(this.btnExport_Click); 47 | // 48 | // frmExcelExport 49 | // 50 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 51 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 52 | this.ClientSize = new System.Drawing.Size(398, 165); 53 | this.Controls.Add(this.btnExport); 54 | this.Name = "frmExcelExport"; 55 | this.ShowIcon = false; 56 | this.Text = "Export To Excel"; 57 | this.ResumeLayout(false); 58 | 59 | } 60 | 61 | #endregion 62 | 63 | private System.Windows.Forms.Button btnExport; 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /ExportToExcel/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.34003 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace ExportToExcel.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// Returns the cached ResourceManager instance used by this class. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ExportToExcel.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// Overrides the current thread's CurrentUICulture property for all 51 | /// resource lookups using this strongly typed resource class. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | 63 | /// 64 | /// Looks up a localized resource of type System.Drawing.Bitmap. 65 | /// 66 | internal static System.Drawing.Bitmap Excel { 67 | get { 68 | object obj = ResourceManager.GetObject("Excel", resourceCulture); 69 | return ((System.Drawing.Bitmap)(obj)); 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /ExportToExcel/ExportToExcel.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {ECD4934A-501E-4724-B70C-FA5569B1F2AD} 9 | WinExe 10 | Properties 11 | ExportToExcel 12 | ExportToExcel 13 | v4.0 14 | Client 15 | 512 16 | 17 | 18 | x86 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | x86 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | True 39 | 40 | 41 | True 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | Form 57 | 58 | 59 | frmMain.cs 60 | 61 | 62 | 63 | 64 | frmMain.cs 65 | 66 | 67 | ResXFileCodeGenerator 68 | Resources.Designer.cs 69 | Designer 70 | 71 | 72 | True 73 | Resources.resx 74 | True 75 | 76 | 77 | SettingsSingleFileGenerator 78 | Settings.Designer.cs 79 | 80 | 81 | True 82 | Settings.settings 83 | True 84 | 85 | 86 | 87 | 88 | 89 | 90 | 97 | -------------------------------------------------------------------------------- /ExportToExcel/frmMain.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 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 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /ExportToExcel/frmMain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using System.Windows.Forms; 4 | using OfficeExcel = Microsoft.Office.Interop.Excel; 5 | 6 | namespace ExportToExcel 7 | { 8 | public partial class frmExcelExport : Form 9 | { 10 | /// 11 | /// Returns a dataset already populated with test data 12 | /// 13 | /// 14 | private DataSet GetDataSet() 15 | { 16 | DataSet ds = new DataSet(); 17 | DataTable dtbl = new DataTable(); 18 | dtbl.Columns.Add("Sl No");//Define Columns 19 | dtbl.Columns.Add("Novel Name"); 20 | dtbl.Columns.Add("Author"); 21 | dtbl.Columns.Add("Genres"); 22 | dtbl.Columns.Add("Published Date"); 23 | dtbl.Columns.Add("Price"); 24 | dtbl.Columns.Add("Rating"); 25 | 26 | dtbl.Rows.Add("1", "In Search of Lost Time", "Marcel Proust", "Literary modernism", "01-01-1913", "348", "4.3");//Adding Rows 27 | dtbl.Rows.Add("2", "Ulysses", "James Joyce", "Modernism", "22-02-1922", "58", "3.7"); 28 | dtbl.Rows.Add("3", "Moby Dick", "Herman Melville", "Adventure fiction", "18-10-1851", "131", "3.4"); 29 | dtbl.Rows.Add("4", "Hamlet", "William Shakespeare", "Tragedy", "01-01-1603", "225", "3.9"); 30 | dtbl.Rows.Add("5", "War and Peace", "Leo Tolstoy", "Historical fiction", "01-01-1869", "133.95", "4.1"); 31 | dtbl.TableName = "Table1"; 32 | ds.Tables.Add(dtbl); 33 | 34 | DataTable dtbl2 = dtbl.Copy();//Created copies of first table 35 | dtbl2.TableName = "Table2"; 36 | ds.Tables.Add(dtbl2); 37 | DataTable dtbl3 = dtbl.Copy();//Created copies of first table 38 | dtbl3.TableName = "Table3"; 39 | ds.Tables.Add(dtbl3); 40 | 41 | return ds; 42 | } 43 | 44 | /// 45 | /// Fuction to export dataset to excel 46 | /// 47 | /// 48 | private void ExportDataSetToExcel(DataSet ds, string strPath) 49 | { 50 | int inHeaderLength = 3, inColumn = 0, inRow = 0; 51 | System.Reflection.Missing Default = System.Reflection.Missing.Value; 52 | //Create Excel File 53 | strPath += @"\Excel" + DateTime.Now.ToString().Replace(':', '-') + ".xlsx"; 54 | OfficeExcel.Application excelApp = new OfficeExcel.Application(); 55 | OfficeExcel.Workbook excelWorkBook = excelApp.Workbooks.Add(1); 56 | foreach (DataTable dtbl in ds.Tables) 57 | { 58 | //Create Excel WorkSheet 59 | OfficeExcel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add(Default, excelWorkBook.Sheets[excelWorkBook.Sheets.Count], 1, Default); 60 | excelWorkSheet.Name = dtbl.TableName;//Name worksheet 61 | 62 | //Write Column Name 63 | for (int i = 0; i < dtbl.Columns.Count; i++) 64 | excelWorkSheet.Cells[inHeaderLength + 1, i + 1] = dtbl.Columns[i].ColumnName.ToUpper(); 65 | 66 | //Write Rows 67 | for (int m = 0; m < dtbl.Rows.Count; m++) 68 | { 69 | for (int n = 0; n < dtbl.Columns.Count; n++) 70 | { 71 | inColumn = n + 1; 72 | inRow = inHeaderLength + 2 + m; 73 | excelWorkSheet.Cells[inRow, inColumn] = dtbl.Rows[m].ItemArray[n].ToString(); 74 | if (m % 2 == 0) 75 | excelWorkSheet.get_Range("A" + inRow.ToString(), "G" + inRow.ToString()).Interior.Color = System.Drawing.ColorTranslator.FromHtml("#FCE4D6"); 76 | } 77 | } 78 | 79 | //Excel Header 80 | OfficeExcel.Range cellRang = excelWorkSheet.get_Range("A1", "G3"); 81 | cellRang.Merge(false); 82 | cellRang.Interior.Color = System.Drawing.Color.White; 83 | cellRang.Font.Color = System.Drawing.Color.Gray; 84 | cellRang.HorizontalAlignment = OfficeExcel.XlHAlign.xlHAlignCenter; 85 | cellRang.VerticalAlignment = OfficeExcel.XlVAlign.xlVAlignCenter; 86 | cellRang.Font.Size = 26; 87 | excelWorkSheet.Cells[1, 1] = "Greate Novels Of All Time"; 88 | 89 | //Style table column names 90 | cellRang = excelWorkSheet.get_Range("A4","G4"); 91 | cellRang.Font.Bold = true; 92 | cellRang.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White); 93 | cellRang.Interior.Color = System.Drawing.ColorTranslator.FromHtml("#ED7D31"); 94 | excelWorkSheet.get_Range("F4").EntireColumn.HorizontalAlignment = OfficeExcel.XlHAlign.xlHAlignRight; 95 | //Formate price column 96 | excelWorkSheet.get_Range("F5").EntireColumn.NumberFormat = "0.00"; 97 | //Auto fit columns 98 | excelWorkSheet.Columns.AutoFit(); 99 | } 100 | 101 | //Delete First Page 102 | excelApp.DisplayAlerts = false; 103 | Microsoft.Office.Interop.Excel.Worksheet lastWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkBook.Worksheets[1]; 104 | lastWorkSheet.Delete(); 105 | excelApp.DisplayAlerts = true; 106 | 107 | //Set Defualt Page 108 | (excelWorkBook.Sheets[1] as OfficeExcel._Worksheet).Activate(); 109 | 110 | excelWorkBook.SaveAs(strPath, Default, Default, Default, false, Default, OfficeExcel.XlSaveAsAccessMode.xlNoChange, Default, Default, Default, Default, Default); 111 | excelWorkBook.Close(); 112 | excelApp.Quit(); 113 | 114 | MessageBox.Show("Excel generated successfully \n As "+strPath); 115 | } 116 | public frmExcelExport() 117 | { 118 | InitializeComponent(); 119 | } 120 | private void btnExport_Click(object sender, EventArgs e) 121 | { 122 | DataSet dsData = GetDataSet(); 123 | ExportDataSetToExcel(dsData, Application.StartupPath); 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /ExportToExcel/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 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 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 122 | ..\bin\Debug\Excel.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 123 | 124 | --------------------------------------------------------------------------------