日期:2014-05-16  浏览次数:20829 次

新手求助 读取excel数据
目的:点击按键读取指定excel表中的A列数据
private void button2_Click(object sender, EventArgs e)
        {

            Microsoft.Office.Interop.Excel.Application xapp = new Microsoft.Office.Interop.Excel.Application();   
            Microsoft.Office.Interop.Excel._Workbook xbk;
            Microsoft.Office.Interop.Excel._Worksheet xht;
            xapp.Visible=true;

            xbk = xapp.Workbooks.Open(textBox1.Text,Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, 
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, );
            xht = (Excel.Worksheet)xbk.Sheets[1];

            Microsoft.Office.Interop.Excel.Range rng=xht.get_Range("A1",Type.Missing);
           
        }
按了button2,无反应
------解决方案--------------------
无反应是什么意思?excel没打开?你看看任务管理器有没有excel进程
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
------解决方案--------------------
  换种方法,Dataset中去取 
 public DataSet ExcelToDS(OpenFileDialog openFileDialog1)
        {
            string strConn = "";
            if (openFileDialog1.FileName.EndsWith(".xls"))
            {
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + openFileDialog1.FileName + ";" + "Extended Properties=Excel 8.0;";
            }
            else if (openFileDialog1.FileName.EndsWith(".xlsx"))
            {
                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + openFileDialog1.FileName + ";" + ";Extended Properties=\"Excel 12.0; HDR=No;\"";

                //   strConn = "Provider=Microsoft.Jet.OLEDB.12.0;" + "Data Source=" + openFileDialog1.FileName + ";Extended Properties=\"Excel 12.0; HDR=No;\"";
           &nb