C#:怎样直接从excel中提取数据

个人日记

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=e:\\Book1.xls;" + "Extended Properties=Excel 8.0;";
            OleDbConnection con = new OleDbConnection(strConn);
            OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
    }
}



--------------------------------------------
string  strConn ="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=e:\\Book1.xls;" + "Extended Properties=Excel 8.0;";
         con=new OleDbConnection(strConn);
         da=new OleDbDataAdapter("select * from [Sheet1$]",con);
         DataSet ds=new DataSet();
         da.Fill(ds);
         DataGrid1.DataSource=ds.Tables[0];
         DataGrid1.DataBind();

文章评论