get_Address()格式不正确

时间:2018-02-19 22:32:06

标签: c# excel .net-3.5

我在excel workbook中使用小型c#应用程序执行搜索。 现在,当匹配发现时,我正在尝试获取完整的行详细信息并且绑定到datagridview,如下所示,但我的问题是

get_Address(true, true, Excel.XlReferenceStyle.xlR1C1, missing, missing) 

未在select statement

中以正确格式显示
  SELECT * FROM [Sheet1$B14:I24] //correct format 
  SELECT * FROM [sheet$B952] 

所以我收到错误消息说

  

Microsoft Access数据库引擎找不到该对象   '片$ B952&#39 ;.确保对象存在并拼写其名称   和路径名称正确。如果'表$ B952'不是本地对象,   检查您的网络连接或联系服务器管理员。

我尝试使用get_AddressLocalxlA1样式,但这对我没用 获得正确的格式

如果有人可以提供帮助或为我提供更好的解决方案或方案

非常感谢

    private void B_General_Search_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook oWB;
        Microsoft.Office.Interop.Excel.Range currentFind = null;
        Microsoft.Office.Interop.Excel.Range firstFind = null;

        string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DefaultDirectory + @"\new29.xlsx" + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";

        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

        if (string.IsNullOrEmpty(TB_Search_Text.Text))
        {
            MessageBox.Show("enter text for search");
            return;
        }

        object missing = System.Reflection.Missing.Value;

        oWB = oXL.Workbooks.Open(DefaultDirectory + @"\new29.xlsx",   //---Filename OR FilePath
                                 0,          //---object UpdateLinks
                                 true,       //---object ReadOnly
                                 missing,    //5//---object Format
                                 "",         //---object Password
                                 "",         //---object WriteResPassword
                                 false,      //---object ReadOnlyRecommend
                                 Excel.XlPlatform.xlWindows,     //---object Origin
                                 "",         //---object Delimiter
                                 true,       //---object Editable
                                 false,      //---object Notify
                                 0,          //---object Converter
                                 true,       //---object AddToMru
                                 false,      //---object Local
                                 false);     //---object CorruptLoad;

        //loop to search witin all excel sheets (workbook)
        foreach (Excel.Worksheet SheetID in oWB.Worksheets)
        {
            Excel.Range oRng = oXL.get_Range("A1", "XFD1048576");

            currentFind = oRng.Find(TB_Search_Text.Text,
                                    missing,
                                    Excel.XlFindLookIn.xlValues,
                                    Excel.XlLookAt.xlPart,
                                    Excel.XlSearchOrder.xlByRows,
                                    Excel.XlSearchDirection.xlNext,
                                    false,
                                    missing,
                                    missing);

            while (currentFind != null)
            {
                //Keep track of the first range you find.
                if (firstFind == null)
                {
                    firstFind = currentFind;
                }
                else if (currentFind.get_Address(true, true, Excel.XlReferenceStyle.xlR1C1, missing, missing) == firstFind.get_Address(true, true, Excel.XlReferenceStyle.xlR1C1, missing, missing))
                {
                    break;
                }
                //currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                //currentFind.Font.Bold = true;

                currentFind = oRng.FindNext(currentFind);

                //SELECT * FROM [Sheet1$B14:I24] correct format 
                //SELECT * FROM [sheet$B952]   XXXXX
                string cmdtxt = @"select * from [" + SheetID.Name + "$" + currentFind.get_Address(true, true, Excel.XlReferenceStyle.xlR1C1, missing, missing) + "]";
                MessageBox.Show(cmdtxt);
                using (OleDbConnection conn = new OleDbConnection(ConnStr))
                {
                    OleDbDataAdapter DA = new OleDbDataAdapter(cmdtxt, conn);
                    DA.Fill(dt);
                    DGV_Data.DataSource = dt;
                    conn.Close();
                }                
            }
        }

        oWB.Close(false, missing, missing);

        //oSheet = null;
        oWB = null;
        oXL.Quit();
    }

0 个答案:

没有答案