Пример #1
0
        private void exportContractorsToCSV()
        {
            try
            {
                //first step is too check if temp file exists and if it does delete it
                //to start again
                if (File.Exists(Application.StartupPath + @"\database\temp\contractorlists.csv"))
                {
                    File.Delete(Application.StartupPath + @"\database\temp\contractorlists.csv");
                }
                //now we get the value of all input and selection fields and convert them to appropriate data type
                exportToCSV CSV = new exportToCSV();
                CSV.createContractorListsCSVDocument();
                string year = textBoxSaveToCSVEndYear.Text;

                try
                {
                    if (File.Exists(Application.StartupPath + @"\database\" + year + @"\contractors\lists\contractors.xml"))
                    {

                        CSV.generateContractorListsCSVDocument(Application.StartupPath + @"\database\" + year + @"\contractors\lists\contractors.xml",
                        DateTime.Now.ToString("d/MM/yyyy"), "contractor");
                    }

                }
                catch (Exception err)
                {
                    debugTerminal terminal = new debugTerminal();
                    terminal.output(err.ToString());
                }

                //now we let the user save the temp excel file
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "Excel|*.CSV";
                save.FileName = "myContractorList.csv";
                progressBar1.Value = 50;

                save.ShowDialog();
                File.Copy(Application.StartupPath + @"\database\temp\contractorlists.csv", save.FileName);
                //exportCSVValidate.Text = "File Saved";
                //delete temp file
                File.Delete(Application.StartupPath + @"\database\temp\contractorlists.csv");
                progressBar1.Value = 100;

            }
            catch (Exception err)
            {

                exportCSVValidate.Text = "File was not saved";
                progressBar1.Value = 0;
                if (File.Exists(Application.StartupPath + @"\database\temp\contractorlists.csv"))
                {
                    File.Delete(Application.StartupPath + @"\database\temp\contractorlists.csv");
                    debugTerminal terminal = new debugTerminal();
                    terminal.output(err.ToString());
                }

            }
        }