C#WForm 작업 EXCEL

2762 단어
1. dsoframer를 등록합니다.ocx, dsoframer 인용 추가
2. 창에 DSO Framer Control Object 추가
3. 다음 변수를 선언합니다
        private String path = AppDomain.CurrentDomain.BaseDirectory;
        Boolean bOpen = false;
        Object refmissing = System.Reflection.Missing.Value;
        private Workbook wbb;
        private Workbook wb;
        private Worksheet ws;
        private Microsoft.Office.Interop.Excel.Application oApp;

 4.excel 열기, 닫기
        // EXCEL
        public void openExcel(String filepath)
        {
            if (bOpen)
            {
                ExcelExit();
            }
            prevone = filepath;
            this.axFramerControl1.Close();
            this.axFramerControl1.Open(filepath);
            this.axFramerControl1.Activate();
            bOpen = true;
            this.axFramerControl1.Menubar = false;
            this.axFramerControl1.Titlebar = false;
            this.axFramerControl1.Toolbars = true;
            wbb = this.axFramerControl1.ActiveDocument as Workbook;
            oApp = wbb.Application as ApplicationClass;
            wb = oApp.Workbooks[oApp.Workbooks.Count];
        }
        // EXCEL
        public void ExcelExit()
        {
            if (bOpen)
            {
                wbb.Save();
                this.axFramerControl1.Save();
                NAR(wbb);
                wbb.Close(false, wbb.FullName, refmissing);
                this.axFramerControl1.Close();
                oApp.Quit();
                NAR(oApp);

            }
            bOpen = false;
            ws = null;
            wb = null;
            wbb = null;
            oApp = null;
        }
        private void NAR(Object o)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
            }
            catch
            {

            }
            finally
            {
                o = null;
            }
        }

 5.EXCEL의 가장 중요한 두 줄 닫기
                                this.axFramerControl1.Close();
                                wbb.Close();

 6.EXCEL 다음 행 선택
                Range range = ws.Rows[nowrownum] as Range;
                range.Select();

 7.EXCEL에 변경 선택 행 이벤트 추가
            oApp.SheetSelectionChange += new AppEvents_SheetSelectionChangeEventHandler(Workbook_SheetSelectionChange);

좋은 웹페이지 즐겨찾기