Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. read data from excel

read data from excel

Scheduled Pinned Locked Moved C#
csharpxmlhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TAFIN
    wrote on last edited by
    #1

    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.

    A 1 Reply Last reply
    0
    • T TAFIN

      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.

      A Offline
      A Offline
      amaankhan
      wrote on last edited by
      #2

      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();

      T 1 Reply Last reply
      0
      • A amaankhan

        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();

        T Offline
        T Offline
        TAFIN
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • T TAFIN

          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

          A Offline
          A Offline
          amaankhan
          wrote on last edited by
          #4

          your welcome........best of luck... happy coding

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups