当前位置: 代码迷 >> C# >> 新手 c# Excel WorkBooks.Open 提示不能访问XX.xls
  详细解决方案

新手 c# Excel WorkBooks.Open 提示不能访问XX.xls

热度:28   发布时间:2016-05-05 04:45:02.0
新手求助 c# Excel WorkBooks.Open 提示不能访问XX.xls
  private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string oleDBString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + @"/bfs.mdb";
                OleDbConnection conn = new OleDbConnection(oleDBString); 
                String serverPath =  openFileDialog1.FileName;
                       openFileDialog1.InitialDirectory = "serverPath";
                conn.Open();
                string sql = "select *  from jh";
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataReader sdr = cmd.ExecuteReader(); //执行查询

                int j = 1;
               while (sdr.Read())
                {
                    string a = sdr.GetString(1);
                    string b = sdr.GetString(2);
                 
                    DataSet ds = ExecleDs(serverPath, openFileDialog1.FileName, a.Trim());
                    DataRow[] dr = ds.Tables[0].Select();                        //定义一个DataRow数组        
                    int rowsnum = ds.Tables[0].Rows.Count;
                                double w = 0;
                    int i = 6;
                    while(i < rowsnum - 4)
                    {
                        if (dr[i][0].ToString().Trim() == b.Trim())

                        {
                           
                            w = Convert.ToDouble(dr[i][9]);
                        }

                       // double c = Convert.ToDouble(dr[i][9]);
                       // w = w + c;
                        i = i + 1;
                    }
                    rq[j] = dr[1][6].ToString();
                    jh[j] = a.ToString();
                    sl[j] = w.ToString(); 
                Thread.Sleep(100);
            
                j = j + 1;
               
            }
            }

            //输出报表execl部分
            string filename = System.Windows.Forms.Application.StartupPath + @"\c.xls";
            string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + filename + ";Extended Properties='Excel 8.0;HDR=Yes'";//这个链接字符串是excel2003的     
            OleDbConnection oleConn = new OleDbConnection(connstr);

            try
            {
                oleConn.Open();
                string sqlStr;
                System.Data.DataTable dt = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null);
                                 objExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                    objExcelWorkBooks = objExcelApp.Workbooks;
                    objExcelWorkbook = objExcelWorkBooks.Open(filename);
到这里就提示  无法访问
------解决思路----------------------
建议直接使用com接口来看控制,不过也挺麻烦的.
------解决思路----------------------
文件被这个OleDbConnection 给独占方式打开了,造成下边的 objExcelWorkBooks.Open(filename); 无法OPEN了。
  相关解决方案