excel.dll

 private void OpenExcel(string strFileName)
        {
            object missing = System.Reflection.Missing.Value;
            Application excel = new Application();
            //lauch excel application    
            if (excel == null)
            {
                Response.Write("<script>alert('连接excel失败')</script>");
            }
            else
            {
                excel.Visible = false;
                excel.UserControl = true;
                // 以只读的形式打开EXCEL文件
                Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing);
                //取得第一个工作薄
                Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);
                //取得总记录行数   (包括标题列)
                int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
                int columnsint = ws.UsedRange.Cells.Columns.Count;//得到列数
                //取得数据范围区域  (不包括标题列)
                for (int i = 2; i < rowsint + 1; i++)
                {
                    for (int j = 1; j < columnsint; j++)
                    {
                        Range r = (Range)ws.Cells[i, j];
                        Response.Write(r.Value2 + "--");
                    }
                    Response.Write("<br/>");
                }

            }
            excel.Quit();
            excel = null;
            Process[] procs = Process.GetProcessesByName("excel");
            foreach (Process pro in procs)
            {
                pro.Kill();//没有更好的方法,只有杀掉进程   
            }
            GC.Collect();
        }

posted @ 2010-07-08 15:35  3.mu  阅读(213)  评论(0编辑  收藏  举报