// Returns true if the creation succeed, if not returns false public static bool CreateExcelFile(string name, string customUI = null) { // Validate overriding if (File.Exists(name)) { Console.Write("There is an excel file already created with this name and path. \nWould you like to override? (y/n) --> "); ConsoleKeyInfo key; int x = 0x0; int pos = Console.CursorLeft; bool canceled = true; while (x != 0x1B) { key = Console.ReadKey(); x = key.KeyChar; if (!(x == 0x59 || x == 0x79 || x == 0x4E || x == 0x6E)) { if (x != 0x1B) { if (x != 0x8) { Console.Write("\b \b"); } else { Console.Write(" "); } Console.CursorLeft = pos; } else { Console.Write("xn"); } } else { canceled = x == 0x4E || x == 0x6E; break; } } if (canceled) { Console.WriteLine("\nProccess Canceled"); return(false); } else { File.Delete(name); Console.WriteLine("\nOverriding file..."); } } // Gets the current office version Excel.Application xlApp; if (version == null) { xlApp = new Excel.Application(); version = xlApp.Version; xlApp.Quit(); while (Marshal.ReleaseComObject(xlApp) != 0) { } } DisableTrustCenterSecurity(); xlApp = new Excel.Application(); Excel.Workbooks xlWbks = xlApp.Workbooks; Excel.Workbook xlWbk = xlWbks.Add(); Excel.XlFileFormat xlFileFormat = Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled; // Creating file AddModule(xlWbk, Executable.Files.VBE.Modules.Callbacks); name = Regex.Replace(name, "[/]", "\\"); xlWbk.SaveAs(name, xlFileFormat); xlWbk.Close(true); while (Marshal.ReleaseComObject(xlWbk) != 0) { } xlWbks.Close(); while (Marshal.ReleaseComObject(xlWbks) != 0) { } xlApp.Quit(); while (Marshal.ReleaseComObject(xlApp) != 0) { } Console.WriteLine(@$ "Excel file successfully created: '{name}'"); bool added = CustomUI.AddCustomUI(name, customUI); EnableTrustCenterSecurity(); return(added); }
public static bool AddCustomUI(bool exe, bool ignore, string excel, string excelPath, string customUI, string customUIPath) { if (exe || ignore) { excel ??= Excel.DefaultExcelFileName; customUI ??= CustomUI.FileDefaultName; excelPath ??= Project.Paths.Base; customUIPath ??= exe && !ignore ? Executable.Paths.VBE.CustomUI : Project.Paths.VBE.CustomUI; Excel.AddMacroEnabledExtension(ref excel); if (!Verify.Name(ref excel, ref excelPath)) { return(false); } } else { customUIPath ??= Paths.VBE.CustomUI; excelPath ??= Paths.Base; if (Configuration.FileExists()) { Configuration.LoadFile(); if (excel == null) { if (!Configuration.GetProjectName(ref excel)) { return(false); } } Excel.AddMacroEnabledExtension(ref excel); if (customUI == null) { if (!Configuration.GetCustomUIDefaultName(ref customUI)) { return(false); } } if (!Verify.Name(ref excel, ref excelPath)) { return(false); } Directory.CreateDirectory(excelPath); } else { customUI ??= CustomUI.FileDefaultName; if (!Configuration.SetCustomUIDefaultName(customUI)) { return(false); } if (excel == null) { if (!Configuration.SetProjectNameByConsole()) { return(false); } if (!Configuration.GetProjectName(ref excel)) { return(false); } } else { if (!Configuration.SetProjectName(excel)) { return(false); } } Excel.AddMacroEnabledExtension(ref excel); if (!Verify.Name(ref excel, ref excelPath)) { return(false); } Paths.Base = excelPath; Directory.CreateDirectory(excelPath); if (!Configuration.CreateConfigurationFile(excel)) { return(false); } Configuration.SaveFile(); } } if (customUI != null) { CustomUI.AddXmlExtension(ref customUI); } if (!Verify.Name(ref customUI, ref customUIPath)) { return(false); } if (!CustomUI.FileExists($"{customUIPath}\\{customUI}")) { return(false); } if (!Excel.FileExists($"{excelPath}\\{excel}")) { return(false); } if (!CustomUI.AddCustomUI($"{excelPath}\\{excel}", $"{customUIPath}\\{customUI}")) { return(false); } return(true); }