首页 新闻 赞助 找找看

操作一个已经打开的EXCEL

0
悬赏园豆:50 [已关闭问题] 关闭于 2015-11-10 14:37

我用C# 读取EXCEL里面的数据,以前都是自动打开一个EXCEL

这一次是要在已经打开的EXCEL 中读取数据。我怎么才能获取电脑当前打开的这个EXCEL 对象呢。

我倒是在进程中能获取这个EXCEL 对象 但是怎么转化为excel对象呢

暗夜中的精灵的主页 暗夜中的精灵 | 初学一级 | 园豆:77
提问于:2015-10-14 09:10
< >
分享
所有回答(3)
0

可否换一个思路,用组件的方式GetObject(, "Excel.Application") '判断Excel是否打开,返回结果?

稳稳的河 | 园豆:4216 (老鸟四级) | 2015-10-14 09:59
1

直接用下面的代码 就能取出打开的EXCEL在进程中所有的文件了,包含能取到的路径和内容,
只要取到  Microsoft.Office.Interop.Excel.Application a = o as Microsoft.Office.Interop.Excel.Application; 这个对像,你就能为所欲为了,怎么操作EXCEL都成。剩下的就是你要去了解这个对像的操作EXCEL的方式。

[DllImport("ole32.dll")]
        public static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable prot);

        [DllImport("ole32.dll")]
        public static extern int CreateBindCtx(int reserved, out IBindCtx ppbc);


        private Hashtable GetRunningObjectTable()
        {
            Hashtable result = new Hashtable();

            IntPtr numFetched = IntPtr.Zero;
            IRunningObjectTable runningObjectTable;
            IEnumMoniker monikerEnumerator;
            IMoniker[] monikers = new IMoniker[1];
            GetRunningObjectTable(0, out runningObjectTable);
            runningObjectTable.EnumRunning(out monikerEnumerator);
            monikerEnumerator.Reset();
            while (monikerEnumerator.Next(1, monikers, numFetched) == 0)
            {
                IBindCtx ctx;
                CreateBindCtx(0, out ctx);
                string runningObjectName;monikers[0].GetDisplayName(ctx, null, out runningObjectName);
                object runningObjectVal;
                runningObjectTable.GetObject(monikers[0], outrunningObjectVal);
                result[runningObjectName] = runningObjectVal;
            }
            return result;
        }

        private bool XlsIsOpen(string fileName)
        {
            bool XlsIsOpen = false;
            Hashtable rot = GetRunningObjectTable();
            bool isOk = false;
            foreach (object o in rot.Values)
            {
                if (isOk)
                {
                    break;
                }
                Microsoft.Office.Interop.Excel.Application a = o as Microsoft.Office.Interop.Excel.Application;
                if (a != null)
                {
                    Microsoft.Office.Interop.Excel.Workbooks wbooks = a.Workbooks;
                    foreach (Microsoft.Office.Interop.Excel.Workbook wb in wbooks)
                    {
                       
                        if (wb.Name == fileName)
                        {
                            Microsoft.Office.Interop.Excel.Worksheet sheet = wb.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;


                            int iRowCount = sheet.UsedRange.Rows.Count;
                            int iColCount = sheet.UsedRange.Columns.Count;
                            Microsoft.Office.Interop.Excel.Range range;

                            for (int iRow = 1; iRow <= iRowCount; iRow++)
                            {


                                for (int iCol = 1; iCol <= iColCount; iCol++)
                                {

                                    Microsoft.Office.Interop.Excel.Range ss = (Microsoft.Office.Interop.Excel.Range)sheet.Cells[iRow, iCol];
                                    Console.WriteLine(ss.Text);
                                }
                            }
                            XlsIsOpen = true;
                            isOk = true;
                            break;
                        }
                    }
                    //a.DisplayAlerts = false;
                    //a.Quit();
                }
            }
            return XlsIsOpen;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            XlsIsOpen(@"XX.xls");
        }

三人之行,必有我师 | 园豆:291 (菜鸟二级) | 2015-10-14 10:45
0

http://www.myexception.cn/c-sharp/1838673.html

自己在这个页面找到答案 自己修改了 里面代码在2010里有错误 需要修改

暗夜中的精灵 | 园豆:77 (初学一级) | 2015-10-14 13:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册