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 sheetName = SheetName.Get(context); string cellName_Begin = CellName_Begin.Get(context); int cellRow_Begin = CellRow_Begin.Get(context); int cellColumn_Begin = CellColumn_Begin.Get(context); System.Data.DataTable dt = DataTable.Get(context); Excel::_Worksheet sheet; if (sheetName != null) { sheet = excelApp.ActiveWorkbook.Sheets[sheetName]; } else { sheet = excelApp.ActiveSheet; } Excel::Range range = cellName_Begin == null ? sheet.Cells[cellRow_Begin, cellColumn_Begin] : sheet.Range[cellName_Begin]; int iRowBegin = range.Row; int iColBegin = range.Column; int i = 0; if (isTitle) { for (int j = 0; j <= dt.Columns.Count - 1; j++) { sheet.Cells[iRowBegin + i, j + iColBegin] = dt.Columns[j].ColumnName; } i++; } for (int a = 0; a <= dt.Rows.Count - 1; a++) { for (int j = 0; j <= dt.Columns.Count - 1; j++) { sheet.Cells[iRowBegin + i + a, j + iColBegin] = dt.Rows[a][j]; } } 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)); }
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]; } }
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)); }