Beispiel #1
0
 public void SetVariables()
 {
     findText    = textBox1.Text;
     replaceText = textBox2.Text;
     lookat      = (comboBox1.SelectedIndex == 0) ? Excel.XlLookAt.xlPart : Excel.XlLookAt.xlWhole;
     searchOrder = (comboBox2.SelectedIndex == 0) ? Excel.XlSearchOrder.xlByRows : Excel.XlSearchOrder.xlByColumns;
     matchCase   = checkBox1.Checked;
 }
Beispiel #2
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;
            string             cellName_Begin  = CellName_Begin.Get(context);
            string             cellName_End    = CellName_End.Get(context);
            int               cellRow_Begin    = CellRow_Begin.Get(context);
            int               cellColumn_Begin = CellColumn_Begin.Get(context);
            int               cellRow_End      = CellRow_End.Get(context);
            int               cellColumn_End   = CellColumn_End.Get(context);
            string            findData         = FindData.Get(context);
            string            replaceData      = ReplaceData.Get(context);
            object            obj = null;
            Excel::Range      range1, range2;
            Excel::_Worksheet sheet     = null;
            Excel::_Worksheet currSheet = null;

            Excel.XlLookAt lookAt = IsCellMatch ? Excel.XlLookAt.xlWhole : Excel.XlLookAt.xlPart;
            if (SheetName.Get(context) != null)
            {
                sheet = excelApp.ActiveWorkbook.Sheets[SheetName];
            }
            else
            {
                sheet = excelApp.ActiveSheet;
            }

            try
            {
                if (AllSheets)  //全部工作表
                {
                    int sheetCount = excelApp.ActiveWorkbook.Worksheets.Count;
                    for (int i = 1; i < sheetCount + 1; i++)
                    {
                        if (AllRange)
                        {
                            excelApp.ActiveWorkbook.Worksheets[i].Range["A1", "IV65535"].Replace(obj, replaceData, lookAt, Type.Missing, IsTextTransform, isSemiFull);
                        }
                        else
                        {
                            currSheet = excelApp.ActiveWorkbook.Worksheets[i];
                            range1    = cellName_Begin == null ? currSheet.Cells[cellRow_Begin, cellColumn_Begin] : currSheet.Range[cellName_Begin];
                            range2    = cellName_End == null ? currSheet.Cells[cellRow_End, cellColumn_End] : currSheet.Range[cellName_End];
                            currSheet.Range[range1, range2].Replace(findData, replaceData, lookAt, Type.Missing, IsTextTransform, isSemiFull);
                        }
                    }
                }
                else
                {
                    if (AllRange)    //全部区域
                    {
                        sheet.Range["A1", "IV65535"].Replace(findData, replaceData, lookAt, Type.Missing, IsTextTransform, isSemiFull);
                    }
                    else
                    {
                        range1 = cellName_Begin == null ? sheet.Cells[cellRow_Begin, cellColumn_Begin] : sheet.Range[cellName_Begin];
                        range2 = cellName_End == null ? sheet.Cells[cellRow_End, cellColumn_End] : sheet.Range[cellName_End];
                        sheet.Range[range1, range2].Replace(findData, replaceData, lookAt, Type.Missing, IsTextTransform, isSemiFull);
                    }
                }

                /* 资源回收释放 */
                System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                sheet = null;
                if (currSheet != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(currSheet);
                    currSheet = 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));
        }