How to export to excel in windows app C#?

export datatable to excel c# windows application
export datagridview to excel in c# windows application
c# export bulk data to excel
c# fastest way to export datatable to excel
export list to excel c#
c# export to excel xlsx
export data from sql server to excel using c# windows application
export datatable to excel using c# with formatting styles

I am new in Windows Application development, Trying to write code for exporting dataset to excel. I have read several examples on the internet regarding this but all are exporting the file to a location. I want to export file like in web (as an open asking to save as file). Below is my code for the web app, what changes we need to run it on a window?

DataGridView grid_records = new DataGridView();
grid_records.DataSource = dset;
//grid_records.DataBind();

Response.Clear();

Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "UIDAI" + DateTime.Now + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
grid_records.GridLines = GridLines.Both;
grid_records.HeaderStyle.Font.Bold = true;
grid_records.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
//Response.End();

Response.Flush();
Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();

Thanks for so many hints , Below is my final code after converting to window app hope it will help others

   private void btn_export_Click(object sender, EventArgs e)
    {
        try
        {
            SaveFileDialog savefile = new SaveFileDialog();
            savefile.FileName = "Response.xls";
            savefile.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
            if (dset.Tables[0].Rows.Count > 0)
            {
                if (savefile.ShowDialog() == DialogResult.OK)
                {
                    //using (StreamWriter sw = new StreamWriter(savefile.FileName))
                    //    sw.WriteLine("Hello World!");
                    StreamWriter wr = new StreamWriter(savefile.FileName);
                    for (int i = 0; i < dset.Tables[0].Columns.Count; i++)
                    {
                        wr.Write(dset.Tables[0].Columns[i].ToString().ToUpper() + "\t");
                    }

                    wr.WriteLine();

                    //write rows to excel file
                    for (int i = 0; i < (dset.Tables[0].Rows.Count); i++)
                    {
                        for (int j = 0; j < dset.Tables[0].Columns.Count; j++)
                        {
                            if (dset.Tables[0].Rows[i][j] != null)
                            {
                                wr.Write(Convert.ToString(dset.Tables[0].Rows[i][j]) + "\t");
                            }
                            else
                            {
                                wr.Write("\t");
                            }
                        }
                        //go to next line
                        wr.WriteLine();
                    }
                    //close file
                    wr.Close();
                    MetroMessageBox.Show(this, "Data saved in Excel format at location "+ savefile.FileName , "Successfully Saved", MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
            }
            else
            {
                MetroMessageBox.Show(this,"Zero record to export , perform a operation first","Can't export file",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        catch (Exception ex)
        {
            MetroMessageBox.Show(this, v1.PrintExceptionDetails(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            el.LogError(ex);
        }
        finally
        {

        }
    }

How to export to excel in windows app C#?, Thanks for so many hints , Below is my final code after converting to window app hope it will help others private void btn_export_Click(object sender, EventArgs  However, to get started, first create a reference to the Excel library. To add a reference, click Project from the top menu list in your .Net IDE and select Add Reference… option. This will open the Add Reference window. Select the COM tab in the window, find and select Microsoft Excel 12.0 Object Library from the list. Click OK.


This example will tell you How to export Dataset to Excel, if you see the 2nd answer on this link, you'll find out how would you allow user to save the document at any location in their pc. You can open Excel file by following this link

Always search before asking a question because everything is available here whatever you'd asked.

Hope it helps!

Windows Forms App, A simple Windows Forms Application that will allow you to Export all your data to a excel sheet with autoformat technique in C# using Microsoft.Office.Interop. This C#.net tutorial, we will discuss how to Export Data Of DataGridview To Excel using C#.Net Windows Application. Below is the full code to Export Data Of DataGridview To Excel using C#.Net Windows Application.


While searching for windows desktop application related answers use keywords "Winforms","C#".

You need to select a file using SaveFileDialog class and use FileStream to write content to it.

You can find detailed samples msdn, here and here.

6 Easy Ways to Export Data to Excel in C#, Exporting data to Excel helps in visualizing the data in a more understandable fashion. to Excel; Microsoft Grid controls to Excel; Array to Excel; CSV to Excel Excel application, you can export data from a database to an Excel table. export data with database to view on excel with add ins using c#?. Here I will explain how to export datatable or datagridview to excel in windows forms application using c# vb.net or export datagridview or datatable data to excel in windows application using c#, vb.net or export datagridview / datatable to excel in windows forms using c#, vb.net.


How to export DataGridView to excel file, Windows.Forms; using System.Data; using System.Data.SqlClient; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsApplication1 { public partial  Here I will explain how to export data from datagridview to excel in c# windows application in vb.net or export datagridview data to excel in windows application using c#, vb.net or export datagridview to excel in windows forms using c#, vb.net.


[Solved] C# winform export to excel, Use the debugger to inspect the variables and to see what your code is doing. There is a tool that allow you to see what your code is doing,  The following site demonstrates how to export a DataSet (or DataTable or List<>) into a "genuine" Excel 2007 .xlsx file.It uses the OpenXML libraries, so you don't need to have Excel installed on your server.


How to Import and Export Excel Spreadsheets with C# and WPF , Net Framework) desktop application project. I named my project StockTrends.​WPF. Building a WPF Application. Once the solution has completed  Here Mudassar Ahmed Khan has explained how to export DataGridView data to Excel file in Windows Forms (WinForms) Applications using OpenXml and ClosedXml Excel conversion libraries, C# and VB.Net. DataGridView cannot be exported directly to Excel file and hence need to generate a DataTable and export the DataTable to Excel file.