read data from excel
-
hellow I am TAFIN from Bangladesh. I am new in C#. i need to read data from an excel file(.xsl). plz help. code segment will be very appreciable. Thanks in advance.
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 System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { static OleDbConnection con; //static OleDbCommand cmd; public Form1() { InitializeComponent(); Connect(); } DataSet ds = new DataSet(); DataTable dt = new DataTable(); private bool Connect() { string DB_Path= @"E:\Office Project\salary project\AttendanceSummary.xls"; string Con_Str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_Path + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";"; con = new OleDbConnection(Con_Str); con.Open(); //string query = "SELECT SNO, ECODE, NAME, PRESENT, ABSENT, OFF, LEAVE, LWP, OTHOURS FROM [AttendanceSummary$]"; string query = "SELECT * FROM [AttendanceSummary$]"; OleDbDataAdapter oda = new OleDbDataAdapter(query, con); DataSet ds = new DataSet(); DataTable dt = new DataTable(); oda.Fill(ds); ds.Tables.Add(dt); string n = ds.Tables[0].Columns[0].ToString(); MessageBox.Show(n); for (int i = 1; i < ds.Tables [0].Rows .Count ; i++) { string result = (ds.Tables[0].Rows[i][ds.Tables [0].Columns [0].ToString ()]).ToString(); comboBox1.Items.Add(result); } //DataSet ds = new DataSet(); // DataTable dt = new DataTable(); dataGridView1.DataSource = ds.Tables[0]; dataGridView1.Update(); if (con.State == ConnectionState.Open) { MessageBox.Show("Connected"); return true; } else return false; //// oda.Dispose(); // con.Close(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { //ds.Tables.Add(dt); int index = comboBox1.SelectedIndex +1 ; textBox1 .Text = (ds.Tables[0].Rows[index][ds.Tables[0].Columns[2].ToString()]).ToString();
-
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 System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { static OleDbConnection con; //static OleDbCommand cmd; public Form1() { InitializeComponent(); Connect(); } DataSet ds = new DataSet(); DataTable dt = new DataTable(); private bool Connect() { string DB_Path= @"E:\Office Project\salary project\AttendanceSummary.xls"; string Con_Str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_Path + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";"; con = new OleDbConnection(Con_Str); con.Open(); //string query = "SELECT SNO, ECODE, NAME, PRESENT, ABSENT, OFF, LEAVE, LWP, OTHOURS FROM [AttendanceSummary$]"; string query = "SELECT * FROM [AttendanceSummary$]"; OleDbDataAdapter oda = new OleDbDataAdapter(query, con); DataSet ds = new DataSet(); DataTable dt = new DataTable(); oda.Fill(ds); ds.Tables.Add(dt); string n = ds.Tables[0].Columns[0].ToString(); MessageBox.Show(n); for (int i = 1; i < ds.Tables [0].Rows .Count ; i++) { string result = (ds.Tables[0].Rows[i][ds.Tables [0].Columns [0].ToString ()]).ToString(); comboBox1.Items.Add(result); } //DataSet ds = new DataSet(); // DataTable dt = new DataTable(); dataGridView1.DataSource = ds.Tables[0]; dataGridView1.Update(); if (con.State == ConnectionState.Open) { MessageBox.Show("Connected"); return true; } else return false; //// oda.Dispose(); // con.Close(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { //ds.Tables.Add(dt); int index = comboBox1.SelectedIndex +1 ; textBox1 .Text = (ds.Tables[0].Rows[index][ds.Tables[0].Columns[2].ToString()]).ToString();
Thanks Amaan. the code was really helpful. i am greatful to U. Thanks!!!:thumbsup: Tanim Tafin mahbub.rabbani.84@gmail.com tanim_csesust.2003@yahoo.com
-
Thanks Amaan. the code was really helpful. i am greatful to U. Thanks!!!:thumbsup: Tanim Tafin mahbub.rabbani.84@gmail.com tanim_csesust.2003@yahoo.com