internal static void GetRange(Excel.Application excelApp,
                                      AsyncCodeActivityContext context,
                                      InArgument <string> SheetName,
                                      InArgument <string> CellName_Begin,
                                      InArgument <string> CellName_End,
                                      InArgument <int> CellRow_Begin,
                                      InArgument <int> CellColumn_Begin,
                                      InArgument <int> CellRow_End,
                                      InArgument <int> CellColumn_End,
                                      out Excel::_Worksheet sheet,
                                      out Excel::Range rangeBegin,
                                      out Excel::Range rangeEnd)
        {
            string sheetName        = SheetName.Get(context);
            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);

            if (sheetName != null && sheetName.Length > 0)
            {
                sheet = excelApp.ActiveWorkbook.Sheets[sheetName];
            }
            else
            {
                sheet = excelApp.ActiveSheet;
            }

            if (cellRow_Begin == 0 && cellColumn_Begin == 0 && string.IsNullOrEmpty(cellName_Begin) &&
                cellRow_End == 0 && cellColumn_End == 0 && string.IsNullOrEmpty(cellName_End))
            {
                // Set UsedRange if every range related values are empty.
                cellRow_Begin    = sheet.UsedRange.Row;
                cellColumn_Begin = sheet.UsedRange.Column;
                cellRow_End      = cellRow_Begin + sheet.UsedRange.Rows.Count - 1;
                cellColumn_End   = cellColumn_Begin + sheet.UsedRange.Columns.Count - 1;
                rangeBegin       = sheet.Cells[cellRow_Begin, cellColumn_Begin];
                rangeEnd         = sheet.Cells[cellRow_End, cellColumn_End];
            }
            else
            {
                rangeBegin = cellName_Begin == null ? sheet.Cells[cellRow_Begin, cellColumn_Begin] : sheet.Range[cellName_Begin];
                rangeEnd   = cellName_End == null ? sheet.Cells[cellRow_End, cellColumn_End] : sheet.Range[cellName_End];
            }
        }
Exemple #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;

            try
            {
                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);
                double rowHeight        = RowHeight.Get(context);
                double colWidth         = ColWidth.Get(context);
                Int32  fontSize         = FontSize.Get(context);
                string sheetName        = SheetName.Get(context);

                Excel::_Worksheet sheet = null;
                if (sheetName == null)
                {
                    sheet = excelApp.ActiveSheet;
                }
                else
                {
                    sheet = excelApp.ActiveWorkbook.Sheets[sheetName];
                }

                Excel::Range range1, range2;
                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];
                Excel::Range range = sheet.Range[range1, range2];

                /*对齐设置*/
                if ((int)_AlignStyle != 0)
                {
                    range.HorizontalAlignment = (AlignEnum)_AlignStyle;
                }

                /*字体*/
                range.Font.Bold      = isBold;
                range.Font.Italic    = isItalic;
                range.Font.Underline = isUnderLine;
                if (Font != 0)
                {
                    range.Font.Name = ConvertFont(Font.ToString());
                }
                range.Font.Size = fontSize;

                if ((int)_FontColor != 0)
                {
                    range.Font.ColorIndex = (int)_FontColor;
                }

                /*填充色*/
                if ((int)_CellColor != 0)
                {
                    range.Interior.ColorIndex = (int)_CellColor;
                }

                /*行列宽度*/
                range.RowHeight   = rowHeight;
                range.ColumnWidth = colWidth;

                /*边框*/
                if ((int)_BorderStyle != 0)
                {
                    switch ((int)_BorderType)
                    {
                    case 0:
                    {
                        range.Borders.LineStyle = (int)_BorderStyle;
                        break;
                    }

                    case 1:
                    {
                        range.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = (int)_BorderStyle;
                        break;
                    }

                    case 2:
                    {
                        range.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = (int)_BorderStyle;
                        break;
                    }

                    case 3:
                    {
                        range.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = (int)_BorderStyle;
                        break;
                    }

                    case 4:
                    {
                        range.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = (int)_BorderStyle;
                        break;
                    }

                    default:
                        break;
                    }
                }
                System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
                sheet = null;
                range = 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));
        }
Exemple #3
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));
        }