problem when export data from datagridview to Excel

Mar 10 2011 4:31 PM
Dear sir/madam,
  This is vshnuvardhan.i face problem when i m trying to export data from datagridview to Excel.Here i attach my project wjth database file.Could you please help any one to resolve those errors.My code is as follows...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;

namespace datagridview
{
    public partial class Form1 : Form
    {
        int cntr = 0;

        OleDbConnection con =new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=E:\\datagridview\\db1.mdb");
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db1DataSet.tokendetails' table. You can move, or remove it, as needed.
            this.tokendetailsTableAdapter.Fill(this.db1DataSet.tokendetails);

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            //string st = "select * from tokendetails where Scope='" + comboBox1.Text + "'";
            //OleDbCommand cmd = new OleDbCommand(st, con);
            ////con.Open();
            ////cmd.ExecuteReader();
            ////con.Close();
            //OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            //DataSet ds = new DataSet();
            //da.Fill(ds, "tokendetails");
            //DataView dv = new DataView(ds.Tables["tokendetails"]);
          
           

        }
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (cntr % 2 == 0)
                dataGridView1.Sort(dataGridView1.Columns[e.ColumnIndex], ListSortDirection.Ascending);
            else
                dataGridView1.Sort(dataGridView1.Columns[e.ColumnIndex], ListSortDirection.Descending);
            cntr++;
        }
       
        private void button4_Click(object sender, EventArgs e)
        {
            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 = true;

            worksheet = workbook.Sheets["sheet1"];
            worksheet = workbook.ActiveSheet;

            worksheet.Name = "Export from gridview";

            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 - 1; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
            }


            workbook.SaveAs("D:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            app.Quit();

        }

    }
}


i got error at  thre lines.

worksheet = workbook.Sheets["sheet1"];
  worksheet = workbook.ActiveSheet; and workbook saveas line.

please check.




Attachment: rar.rar

Answers (2)