请各位大大帮忙看看代码错在哪里? 环境 windows7  VS2010 office2007   提交excel文件后, 没有反馈。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;namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            //打开一个文件选择框
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Excel文件";
            ofd.FileName = "";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ofd.Filter = "Excel文件(*.xls)|*.xls";
            ofd.ValidateNames = true;     //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
            ofd.CheckFileExists = true;  //验证路径有效性
            ofd.CheckPathExists = true; //验证文件有效性            string strName = string.Empty;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                strName = ofd.FileName;
            }            if (strName == "")
            {
                MessageBox.Show("没有选择Excel文件!无法进行数据导入");
                return;
            }            //调用导入数据方法
            EcxelToDataGridView(strName, this.dataGridView1);
            //MessageBox.Show(strName);        }        public void EcxelToDataGridView(string filePath, DataGridView dgv)
        {
            //根据路径打开一个Excel文件并将数据填充到DataSet中
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            DataSet ds = null;
            strExcel = "select  * from   [sheet1$]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            ds = new DataSet();
            myCommand.Fill(ds, "table1");            //根据DataGridView的列构造一个新的DataTable
            DataTable tb = new DataTable();
            foreach (DataGridViewColumn dgvc in dgv.Columns)
            {
                if (dgvc.Visible && dgvc.CellType != typeof(DataGridViewCheckBoxCell))
                {
                    DataColumn dc = new DataColumn();
                    dc.ColumnName = dgvc.DataPropertyName;
                    //dc.DataType = dgvc.ValueType;//若需要限制导入时的数据类型则取消注释,前提是DataGridView必须先绑定一个数据源那怕是空的DataTable
                    tb.Columns.Add(dc);
                }
            }            //根据Excel的行逐一对上面构造的DataTable的列进行赋值
            foreach (DataRow excelRow in ds.Tables[0].Rows)
            {
                int i = 0;
                DataRow dr = tb.NewRow();
                foreach (DataColumn dc in tb.Columns)
                {
                    dr[dc] = excelRow[i];
                    i++;
                } 
                tb.Rows.Add(dr);
            }
            //在DataGridView中显示导入的数据
            dgv.DataSource = tb;
            
        }        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            //dt = gettable();
            dataGridView1.DataSource = dt;
            //dataGridView1.DataBindings;
        }
    }
}

解决方案 »

  1.   

    DataSet ds = new DataSet();
    string strCon = "Provider= Microsoft.Ace.OleDB.12.0;Data Source=c:\a.xlsx;Extended Properties=Excel 12.0;";
    using (OleDbConnection conn= new OleDbConnection(strCon))
    {
      OleDbDataAdapter OleDat = new OleDbDataAdapter("select * from [Sheet1$]", conn);
      OleDat.Fill(ds);
    }
      

  2.   

    连接字符串不正确,参考:http://www.connectionstrings.com/excel-2007
      

  3.   

           public void EcxelToDataGridView(string filePath, DataGridView dgv)
            {
                //根据路径打开一个Excel文件并将数据填充到DataSet中
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "";
                OleDbDataAdapter myCommand = null;
                DataSet ds = null;
                strExcel = "select * from [V1$]";
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                ds = new DataSet();
                myCommand.Fill(ds, "table1");            //根据DataGridView的列构造一个新的DataTable
                DataTable tb = new DataTable();
                tb=ds.Tables[0];
                dgv.DataSource = tb;
                System.IO.File.Exists(@"F:\Sheet1.xls");
            }方法这么写试试
      

  4.   


    OleDbConnection conn = new OleDbConnection(strCon)   从索引 49 处开始,初始化字符串的格式不符合规范。
      

  5.   

    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
      

  6.   

    public void EcxelToDataGridView(string filePath, DataGridView dgv)
      {
      //根据路径打开一个Excel文件并将数据填充到DataSet中
      string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
      OleDbConnection conn = new OleDbConnection(strConn);
      conn.Open();
      string strExcel = "";
      OleDbDataAdapter myCommand = null;
      DataSet ds = null;
      strExcel = "select * from [V1$]";
      myCommand = new OleDbDataAdapter(strExcel, strConn);
      ds = new DataSet();
      myCommand.Fill(ds, "table1");  //根据DataGridView的列构造一个新的DataTable
      DataTable tb = new DataTable();
      tb=ds.Tables[0];
      dgv.DataSource = tb;
      System.IO.File.Exists(@"F:\Sheet1.xls");
      }你把方法改成这个  可以读出来啊 
      

  7.   


    兄弟你的方法可以, 多谢了,成功导入, 下附 完整代码。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;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                //打开一个文件选择框
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "Excel文件";
                ofd.FileName = "";
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                ofd.Filter = "Excel文件(*.xls)|*.xls";
                ofd.ValidateNames = true;     //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
                ofd.CheckFileExists = true;  //验证路径有效性
                ofd.CheckPathExists = true; //验证文件有效性            string strName = string.Empty;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    strName = ofd.FileName;
                }            if (strName == "")
                {
                    MessageBox.Show("没有选择Excel文件!无法进行数据导入");
                    return;
                }            //调用导入数据方法
                EcxelToDataGridView(strName, this.dataGridView1);
                //MessageBox.Show(strName);        }        public void EcxelToDataGridView(string filePath, DataGridView dgv)
            {
                //根据路径打开一个Excel文件并将数据填充到DataSet中
                //string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "";
                OleDbDataAdapter myCommand = null;
                DataSet ds = null;
                strExcel = "select  * from   [sheet1$]";
                //strExcel = "select * from [V1$]";
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                ds = new DataSet();
                myCommand.Fill(ds, "table1");            //根据DataGridView的列构造一个新的DataTable
                DataTable tb = new DataTable();
                tb = ds.Tables[0];
                dgv.DataSource = tb;
                System.IO.File.Exists(@filePath);            //在DataGridView中显示导入的数据
                dgv.DataSource = tb;
                
            }        private void Form1_Load(object sender, EventArgs e)
            {            
            }
        }
    }
      

  8.   

    是啊  运行了你的代码 
    不知道为什么你要   根据DataGridView的列构造一个新的DataTable
    直接用dataset的table就行  最好自己一步步调试  调试很关键啊