用oledb C#

时间:2017-09-15 11:49:03

标签: c# excel

当我尝试关闭excel文件时出现问题。我正在使用OleDb打开excel文件然后关闭它。但它不起作用。

OleDbConnection ole = new OleDbConnection(connString);
ole.Open();
comboBox1.DataSource = GetExcelSheetNames(filePath);
OleDbCommand cmd = new OleDbCommand("Select * from [" + comboBox1.SelectedValue.ToString() + "]", ole);            
OleDbDataAdapter oledata = new OleDbDataAdapter();
oledata.SelectCommand = cmd;
DataSet ds = new DataSet();
oledata.Fill(ds);
DataTable datatable = ds.Tables[0];
StripEmptyRows(datatable);
dataGridView3.DataSource = datatable;
oledata.Dispose();
ole.Close();
ole = null;
ds.Dispose();

enter image description here

P / s:这是一个GetExcelSheetName

 private String[] GetExcelSheetNames(string excelFile)
    {
        OleDbConnection objConn = null;
        System.Data.DataTable dt = null;
        String connString = "";
        string[] fileSpit = filePath.Split('.');

        try
        {
            if (filePath.Length > 1 && fileSpit[1] == "xls")
            {
                connString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0";

            }
            else
            {
                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0";
            }
            objConn = new OleDbConnection(connString);
            objConn.Open();
            dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            if (dt == null)
            {
                return null;
            }
            String[] excelSheets = new String[dt.Rows.Count];
            int i = 0;
            foreach (DataRow row in dt.Rows)
            {
                excelSheets[i] = row["TABLE_NAME"].ToString();
                i++;
            }
            return excelSheets;
        }
        catch (System.Exception ex)
        {
            return null;
        }
        finally
        {
            if (objConn != null)
            {
                objConn.Close();
                objConn.Dispose();
            }
            if (dt != null)
            {
                dt.Dispose();
            }
        }
    }

更新:我使用代码,但它有效但不好。

 private void ImportDialogBox_FormClosing(object sender, FormClosingEventArgs e)
    {
        HideImportDialogBox();
        System.Windows.Forms.Application.Exit();
        try
        {
            if (txtExcelFile.Text != null)
            {
                xlApp.Quit();
                xlWorkbook.Close(true);
            }
        }

        catch { }

        finally //kill excel.exe
        {
            try
            {
                Process[] excelProcsNew = Process.GetProcessesByName("EXCEL");
                foreach (Process procNew in excelProcsNew)
                {
                    int exist = 0;
                    foreach (Process procOld in excelProcsOld)
                    {
                        if (procNew.Id == procOld.Id)
                        {
                            exist++;
                        }
                    }
                    if (exist == 0)
                    {
                        procNew.Kill();
                    }
                }
            }
            catch { }
        }

    }

0 个答案:

没有答案