This is a mirror of official site: http://jasper-net.blogspot.com/

Import Data from Excel to DataGridView in C#

| Wednesday, July 21, 2010
First need to add the reference "Microsoft ADO Ext. 2.8". You can easily add it from COM components.

Add an open Dialog box control on form

Put the following code on Browser button click events…..

       private void button1_Click_1(object sender, EventArgs e)
       {
           OpenFileDialog fdlg = new OpenFileDialog();
           fdlg.Title = "Select file";
           fdlg.InitialDirectory = @"c:\";
           fdlg.FileName = txtFileName.Text;
           fdlg.Filter = "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
           fdlg.FilterIndex = 1;
           fdlg.RestoreDirectory = true;
           if (fdlg.ShowDialog() == DialogResult.OK)
           {
               txtFileName.Text = fdlg.FileName;
               Import();
               Application.DoEvents();
           }
       }

This will filter only Excel file from your Machine.

This Excel file can contains more than one Sheet. You need to add another form to all excel sheets name so that user can select any one excel sheet which he want to import.

Write the following code on Page Load even of this form

       private void Select_Tables_Load(object sender, EventArgs e)
       {
           if (!DataTables)
           {
               if (Tables != null)
               {
                   for (int tables = 0; tables < Tables.Length; tables++)
                   {
                       try
                       {
                           ListViewItem lv = new ListViewItem();
                           lv.Text = Tables[tables].ToString();
                           lv.Tag = tables;
                           lstViewTables.Items.Add(lv);
                       }
                       catch (Exception ex)
                       { }
                   }
               }
           }
           else

Read more: C# Corner

Posted via email from .NET Info

0 comments: