Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            string firstPassword  = "";
            string secondPassword = "";

            do
            {
                if (firstPassword != secondPassword)
                {
                    Console.WriteLine("Passwords do not match!");
                }

                Console.Write("Enter your password: "******"Confirm your password: "******"The Password You entered is : " + firstPassword);
            Console.WriteLine("The Hash : " + HashUtil.Encrypt(firstPassword));
#endif
            try
            {
                // open config
                string configPath = Directory.GetCurrentDirectory() + @"\CatalogPrinter.config";
                if (!File.Exists(configPath))
                {
                    throw new Exception($"Config file " + configPath + " not found!");
                }
                ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
                configMap.ExeConfigFilename = configPath;
                Configuration config      = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
                var           appSettings = config.GetSection("appSettings") as AppSettingsSection;
                // get config values
                string oldHash       = appSettings.Settings["password"].Value;
                string masterCatalog = appSettings.Settings["masterCatalog"].Value;

                // encrypt new password
                string encryptedPassword = HashUtil.Encrypt(firstPassword);

                // try opening catalog with old password and change the password
                if (!File.Exists(masterCatalog))
                {
                    throw new Exception($"Workbook " + masterCatalog + " not found!");
                }
                Console.WriteLine("Changing the password...");
                string   oldPassword = HashUtil.Decrypt(oldHash);
                Workbook wb          = ExcelUtility.GetWorkbook(masterCatalog, oldPassword);
                if (wb == null)
                {
                    throw new Exception($"Wrong password for workbook " + masterCatalog + "!");
                }
                wb.Password = firstPassword;
                ExcelUtility.CloseWorkbook(wb, true);

                // write new encrypted password to config
                appSettings.Settings["password"].Value = encryptedPassword;
                config.Save(ConfigurationSaveMode.Modified);

                Console.WriteLine("Success! Press Enter to close the application.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Error! Could not change the password!");
            }

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        private void Print_Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // open config
                string configPath = Directory.GetCurrentDirectory() + @"\CatalogPrinter.config";
                if (!File.Exists(configPath))
                {
                    throw new Exception($"Config file " + configPath + " not found!");
                }
                ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
                configMap.ExeConfigFilename = configPath;
                Configuration config      = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
                var           appSettings = config.GetSection("appSettings") as AppSettingsSection;
                // get config values
                string hash          = appSettings.Settings["password"]?.Value;
                string masterCatalog = appSettings.Settings["masterCatalog"]?.Value;
                string clientCatalog = appSettings.Settings["clientCatalog"]?.Value;
                string outputPath    = appSettings.Settings["outputPath"]?.Value;

                if (hash == null)
                {
                    throw new Exception($"Could not find 'password' key in " + configPath + "!");
                }
                if (masterCatalog == null)
                {
                    throw new Exception($"Could not find 'masterCatalog' key in " + configPath + "!");
                }
                if (clientCatalog == null)
                {
                    throw new Exception($"Could not find 'clientCatalog' key in " + configPath + "!");
                }
                if (outputPath == null)
                {
                    throw new Exception($"Could not find 'outputPath' key in " + configPath + "!");
                }

                // check if files exists
                if (!File.Exists(masterCatalog))
                {
                    throw new Exception($"Workbook " + masterCatalog + " not found!");
                }
                if (!File.Exists(clientCatalog))
                {
                    throw new Exception($"Workbook " + clientCatalog + " not found!");
                }

                // open master workbook
                string password = HashUtil.Decrypt(hash);
                Workbook = ExcelUtility.GetWorkbook(masterCatalog, password);

                // get catalog type
                string catalogType = CatalogTypeComboBox.SelectedItem.ToString();

                // open temp workbook to which the sheets of interest are copied to
                Workbook2Print = ExcelUtility.XlApp.Workbooks.Add();
                if (!Directory.Exists(_tmpWorkbookDir))
                {
                    Directory.CreateDirectory(_tmpWorkbookDir);
                }
                Workbook2Print?.SaveAs(_tmpWorkbookDir + _tmpWokbookName);

                // get sheet order to print
                var sheetOrder = GetSheetOrder(catalogType, clientCatalog);

                string leftHeader = "null", centerHeader = "null", rightHeader = "null", leftFooter = "null", rightFooter = "null";

                // copy necessary sheets to temp workbook and put sheets in correct order
                foreach (var shName in sheetOrder)
                {
                    if (ExcelUtility.GetWorksheetByName(Workbook, shName) == null)
                    {
                        throw new Exception($"Sheet " + shName + " not found in workbook " + masterCatalog + "!" +
                                            "\nPlease check the sheet order input in " + clientCatalog + " for " + catalogType + ".");
                    }
                    // set catalog type
                    Workbook.Sheets[shName].Cells[11, 2] = catalogType;

                    leftHeader   = (Workbook.Sheets[shName].Cells[16, 2] as Range).Value as string ?? "null";
                    centerHeader = (Workbook.Sheets[shName].Cells[17, 2] as Range).Value as string ?? "null";
                    var rightHeaderDate = ((Workbook.Sheets[shName].Cells[18, 2] as Range).Value);
                    rightHeader = "null";
                    if (rightHeaderDate != null)
                    {
                        rightHeader = rightHeaderDate.ToString("dd/MM/yyyy");
                    }
                    leftFooter  = (Workbook.Sheets[shName].Cells[19, 2] as Range).Value as string ?? "null";
                    rightFooter = (Workbook.Sheets[shName].Cells[20, 2] as Range).Value as string ?? "null";

                    // copy sheet
                    if (catalogType == "Particulier")
                    {
                        //SetBtwField(Workbook.Sheets[shName], true);
                        Workbook.Sheets[shName].Copy(After: Workbook2Print.Sheets[Workbook2Print.Sheets.Count]);
                        Workbook2Print.Sheets[Workbook2Print.Sheets.Count].Cells[8, 2] = "ja";
                        //SetBtwField(Workbook.Sheets[shName], false);
                        Workbook.Sheets[shName].Copy(After: Workbook2Print.Sheets[Workbook2Print.Sheets.Count]);
                        Workbook2Print.Sheets[Workbook2Print.Sheets.Count].Cells[8, 2] = "neen";
                    }
                    else
                    {
                        Workbook.Sheets[shName].Copy(After: Workbook2Print.Sheets[Workbook2Print.Sheets.Count]);
                    }
                }
                // delete default first sheet on creation of workbook
                Workbook2Print.Activate();
                Workbook2Print.Worksheets[1].Delete();

                // format and print sheets
                string outputFile = outputPath + @"\catalog.pdf";
                if (ExcelUtility.IsFileInUse(outputFile))
                {
                    throw new Exception(outputFile + " is open, please close it and press 'Print' again.");
                }
                foreach (Worksheet sh in Workbook2Print.Worksheets)
                {
                    FormatSheet(sh, leftHeader, centerHeader, rightHeader, leftFooter, rightFooter);
                }
                Workbook2Print.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outputFile, OpenAfterPublish: true);

                MessageBox.Show("Done!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                ExcelUtility.CloseWorkbook(Workbook, false);
                ExcelUtility.CloseWorkbook(WorkbookSheetOrder, false);
                ExcelUtility.CloseWorkbook(Workbook2Print, true);
                File.Delete(_tmpWorkbookDir + _tmpWokbookName);

                ExcelUtility.CloseExcel();
            }
        }