Exemplo n.º 1
0
        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {
            PropertyDescriptor property = context.DataContext.GetProperties()[ExcelCreate.GetExcelAppTag];
            Excel::Application excelApp = property.GetValue(context.DataContext) as Excel::Application;

            try
            {
                Int32             cellColumn = CellColumn.Get(context);
                Int32             cellRow    = CellRow.Get(context);
                string            cellName   = CellName.Get(context);
                string            sheetName  = SheetName.Get(context);
                Excel::_Worksheet sheet;

                if (sheetName == null)
                {
                    sheet = excelApp.ActiveSheet;
                }
                else
                {
                    sheet = excelApp.ActiveWorkbook.Sheets[sheetName];
                }
                object cellContent = null;
                if (cellName == null)
                {
                    cellContent = sheet.Cells[cellRow, cellColumn].Value2;
                }
                else
                {
                    cellContent = sheet.Range[cellName].Value2;
                }
                CellContent.Set(context, cellContent);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                sheet = null;
                GC.Collect();
            }
            catch (Exception e)
            {
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, "EXCEL获取单元格内容出错", e.Message);
                new CommonVariable().realaseProcessExit(excelApp);
            }
            m_Delegate = new runDelegate(Run);
            return(m_Delegate.BeginInvoke(callback, state));
        }