读取EXCEL的简单方式

        private void OpenExcel(string strFileName)
        {
            object missing = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//启动Excel应用程序

            if (excel == null)
            {
                MessageBox.Show("Can't access excel");
            }
            else
            {
                excel.Visible = false;//对象是否可见
                excel.UserControl = true;//如果应用程序可见或者由用户创建或启动,则为true。 如果您使用CreateObject或GetObject函数以编程方式创建或启动应用程序,并且隐藏应用程序,则为False。

                //只读方式打开EXCEL文件
                Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing, missing, missing, 
                                                                missing, true, missing, missing, missing, missing, missing);

             
                //取得第1个工作薄
                Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);

               Range value1 = ws.Cells[2][5];//单元格:B-5
               Range value2 = ws.Cells[1][2];//单元格:A-2
              
               this.textBox1.Text =value2.Text;
            }

            excel.Quit(); excel = null;//退出Microsoft Excel

            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("excel");

            foreach (Process pro in procs)
            {
                pro.Kill();//没有更好的方法,只有杀掉进程
            }
            GC.Collect();
        }

 

posted @ 2017-11-09 18:10  巫居树  阅读(346)  评论(0编辑  收藏  举报