Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Satham.DataAccess1;

namespace exptoexl
{
    public partial class Form1 : Form
    {
        DataTable dt = new DataTable();
        DBAccess db = new DBAccess();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnload_Click(object sender, EventArgs e)
        {
           // DataRow dr = dt.NewRow();
           // dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;
            string sSql = "SELECT * FROM `fxwalkincustomer`";
            db.ExecuteNonQuery(sSql);
            dt = db.GetDataTable(sSql);
            dataGridView1.DataSource = dt;
            MessageBox.Show("Loading Complete");

        }

        private void btnexport_Click(object sender, EventArgs e)
        {
            load.InitialDirectory = "C:/Windows";
            load.Title = "save as ExcelFile";
            load.FileName = "";
            load.Filter = "Excel Files(2003)|*.xls|Excel Files(2007)|*.xlsx|Excel Workbook|*.xlsx";

            if (load.ShowDialog() != DialogResult.Cancel)
            {

                Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                app.Visible = false;
                worksheet = workbook.Sheets["Sheet1"];
                worksheet = workbook.ActiveSheet;
                app.Columns.ColumnWidth = 20;
                worksheet.Name = "Exported from dtaaGridView1";
                             

                for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
                {
                    worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                    }
                }
                app.ActiveWorkbook.SaveCopyAs(load.FileName.ToString());
                app.ActiveWorkbook.Saved = true;
                app.Quit();
            }
        }
    }
}
Posted
Updated 25-Jun-14 19:40pm
v2
Comments
Thanks7872 26-Jun-14 1:40am    
What is not working? How you come to know that?

Hi,

You can use code @ below link, it will solve your problem.

Export to Excel from GridView in C#[^]

Please give your feedback or rating when you get solution.
 
Share this answer
 
v2
Comments
Sarvesh Kumar Gupta 26-Jun-14 3:28am    
You have checked my link, it's working or not. I have used this code and working fine. If you have any confusion or doubt then you can ask me.
 
Share this answer
 
Comments
syshafarook 26-Jun-14 2:34am    
that coding not working showing error on Sqlconnection =cnn
syshafarook 26-Jun-14 2:35am    
how to rectify that error
Thanks7872 26-Jun-14 2:47am    
If something doesn't work for you,why did you accept it as solution? your question with accepted solutions get highlighted as green in the list which will prevent it from getting more answers.
syshafarook 26-Jun-14 3:16am    
just i trusted on ur coding but its not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900