部分代码如下:    1.(根据用户选择下拉列表框里的值 填充数据集) 2.DotNetBar.ComboBoxItem  第3方控件就是combobox 
 public void OpenFile(DevComponents.DotNetBar.ComboBoxItem cmb)//打开Excel文件,得到里面的表名,填充到 下拉列表框 里
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开(Open)";
            ofd.FileName = "";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ofd.Filter = "文本文件(*.xls)|*.xls";
            ofd.ValidateNames = true;
            ofd.CheckFileExists = true;  //验证文件是否存在
            ofd.CheckPathExists = true; // 验证文件路径是否存在
            ofd.Multiselect = false;
            ofd.RestoreDirectory = false;
            try
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (ofd.FileName.Split('.')[1].ToString().Equals("xls")) //判断文件的后缀名是否为xls
                    {
                        object missing = System.Reflection.Missing.Value;
                        Excel.Application excelApp = new Excel.Application();
                        excelApp.Workbooks.Open(ofd.FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                        cmb.Items.Clear();
                        for (int i = 0; i < excelApp.Workbooks[1].Worksheets.Count; i++)
                        {
                            Excel.Worksheet ws = (Excel.Worksheet)excelApp.Workbooks[1].Worksheets[i + 1];
                            string sSheetName = ws.Name;
                            cmb.Items.Add(sSheetName);
                        }
                        cmb.SelectedIndex = 0;
                    }
                    else
                    {
                        MessageBox.Show("请选择Excel文件!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }        public DataSet ExcelToDataSet(string opnFileName, string tableName) //把Excel转换成DataSet
        {
            OleDbConnection objcon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+opnFileName+";" + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");
            OleDbDataAdapter objdataAdapter = new OleDbDataAdapter("select * from [" + tableName + "$]", objcon);
            DataSet ds = new DataSet();
            objdataAdapter.Fill(ds);     -----------到了这里 程序阻塞了
            return ds;
}在线等!

解决方案 »

  1.   

    补充一下   我读别的Excel文件都没问题   唯独某一个excel 出现阻塞问题
      

  2.   

    问题也许出在这里:你的OpenFile方法里打开了Excel文件,却没有关闭.读完工作表名,把Excel文档关闭,看是否能解决你的问题.Office类的文档,都是独占操作.若有进程在用,其他进程往往不能再操作该文件.所以你才会看到"阻塞".