protected override void Execute(CodeActivityContext context) { base.LoadVariables(context); StrNewSheetName = NewSheetName.Get(context); StrNewFilePath = NewFilePath.Get(context); StrNewFilePassword = NewFilePassword.Get(context); IsAutoCreate = AutoFileCreation.Get(context); this.DoCopyToFile(); }
protected override void Execute(NativeActivityContext context) { try { String workbookFullName = FilePath.Get(context); String newFileName = NewFileName.Get(context); string newfilePath = NewFilePath.Get(context); string fileFormat = ToDescriptionString(FileFormatType); String[] fileFormatTypeStr = FileFormatType.ToString().TrimStart().Split('_'); String extension = String.Concat(".", fileFormatTypeStr[0]); string newFileNameWithExtension = String.Concat(newFileName, extension); string workbookName = string.Empty; dynamic xlWorkbook = null; bool excelFileVisible = false; if (true == NeedToOpen) { excelFileVisible = true; } // Combine 2 path parts. string workbooknameas = Path.Combine(newfilePath, newFileName); string workbooknameasWithExtension = Path.Combine(newfilePath, newFileNameWithExtension); if (File.Exists(workbookFullName)) { ExcelHelper.Shared.Close_OpenedFile(workbookFullName); workbookName = Path.GetFileName(workbookFullName); ExcelHelper.Shared.GetApp(excelFileVisible).DisplayAlerts = false; xlWorkbook = ExcelHelper.Shared.GetApp(excelFileVisible).Workbooks.Open(workbookFullName); if (File.Exists(workbooknameasWithExtension)) { if (true == IsOverride) { // xlWorkbook.SaveAs(workbooknameas, FileFormatType, Missing.Value, Missing.Value, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value); XlFileFormat fm = (XlFileFormat)Enum.Parse(typeof(XlFileFormat), fileFormat); xlWorkbook.SaveAs(workbooknameas, fm, Missing.Value, Missing.Value, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value); } else { Log.Logger.LogData("The file \"" + newFileName + "\" is already exists in activity Excel_Workbook_SaveAs", LogLevel.Error); if (!ContinueOnError) { context.Abort(); } } } else { // xlWorkbook.SaveAs(workbooknameas, XlFileFormat.xlWorkbookDefault, Missing.Value, Missing.Value, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value); //xlWorkbook.SaveAs(workbooknameas, FileFormatType, Missing.Value,Missing.Value, false, false, XlSaveAsAccessMode.xlNoChange,XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value); XlFileFormat fm = (XlFileFormat)Enum.Parse(typeof(XlFileFormat), fileFormat); xlWorkbook.SaveAs(workbooknameas, fm, Missing.Value, Missing.Value, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value); } if (true == NeedToClose) { xlWorkbook.Close(); } if (false == NeedToClose && false == NeedToOpen) { xlWorkbook.Close(); } if (false == NeedToClose && true == NeedToOpen) { xlWorkbook.Close(); ExcelHelper.Shared.GetApp(excelFileVisible).Workbooks.Open(workbookFullName); } if (ExcelHelper.Shared.GetApp(excelFileVisible).Workbooks.Count == 0) { ExcelHelper.Shared.Dispose(); } } else { Log.Logger.LogData("Excel file does not exist:\"" + workbookFullName + "\" in activity Excel_Workbook_SaveAs", LogLevel.Error); if (!ContinueOnError) { context.Abort(); } } } catch (Exception ex) { Log.Logger.LogData(ex.Message + " in activity Excel_Workbook_SaveAs", LogLevel.Error); if (!ContinueOnError) { context.Abort(); } } }