Ejemplo n.º 1
0
        private void EditPerson(FormMode formMode, Employee person, List <Employee> personList)
        {
            Employee newPerson;
            var      indexToFind = personList.IndexOf(person);

            if (formMode == FormMode.EditFixPrice)
            {
                newPerson = new FixedTimeEmployee(ConvertToDecimal(this.Salary.Text))
                {
                    FirstName  = person.FirstName,
                    LastName   = person.LastName,
                    EmployeeId = person.EmployeeId
                };
                personList[indexToFind] = newPerson;
            }
            else
            if (formMode == FormMode.EditCalcPrice)
            {
                newPerson = new ByTimeEmployee(ConvertToDecimal(this.Salary.Text), person.FirstName, person.LastName,
                                               person.EmployeeId);
                personList[indexToFind] = newPerson;
            }
        }
Ejemplo n.º 2
0
        private void ImportFromExcell()
        {
            Microsoft.Office.Interop.Excel.Range excellRange;
            ExcellSctructure excelStructure = new ExcellSctructure();
            OpenFileDialog   excellImport   = new OpenFileDialog();
            DataTable        dTable         = new DataTable("dataTableExcell");
            DataSet          dsSource       = new DataSet("dataSetExcell");

            dTable.Reset();
            try
            {
                Employees.Clear();
                excelStructure.SetElements();

                excellImport.Title            = "Select file excell";
                excellImport.Filter           = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
                excellImport.FilterIndex      = 1;
                excellImport.RestoreDirectory = true;

                if (excellImport.ShowDialog() == DialogResult.OK)
                {
                    excelStructure.XlWorkBook = excelStructure.XlExcel.Workbooks.Open(excellImport.FileName, excelStructure.MisValue, excelStructure.MisValue,
                                                                                      excelStructure.MisValue, excelStructure.MisValue, excelStructure.MisValue, excelStructure.MisValue, excelStructure.MisValue, excelStructure.MisValue,
                                                                                      excelStructure.MisValue, excelStructure.MisValue, excelStructure.MisValue, excelStructure.MisValue, excelStructure.MisValue, excelStructure.MisValue);
                    excelStructure.XlWorkSheet = excelStructure.XlWorkBook.Sheets.get_Item(1);
                    excellRange = excelStructure.XlWorkSheet.UsedRange;
                    var employeeProperties = typeof(Employee).GetProperties();
                    var fiedCountEmployee  = employeeProperties.Length;
                    var columnCountExcell  = excellRange.Columns.Count;

                    List <string> columnList = new List <string>();
                    for (int columnNum = 1; columnNum <= excellRange.Columns.Count; columnNum++)
                    {
                        dTable.Columns.Add(new DataColumn((excellRange.Cells[1, columnNum] as Microsoft.Office.Interop.Excel.Range).Value2.ToString()));
                    }
                    dTable.AcceptChanges();

                    for (int i = 0; i < dTable.Columns.Count; i++)
                    {
                        columnList.Add(dTable.Columns[i].ColumnName);
                    }

                    //Check format
                    try
                    {
                        bool wrongFormat = false;

                        wrongFormat = fiedCountEmployee != columnCountExcell;
                        foreach (var item in columnList)
                        {
                            if (employeeProperties.FirstOrDefault(zx => zx.Name == item) == null)
                            {
                                wrongFormat = false;
                                break;
                            }
                        }

                        if (wrongFormat)
                        {
                            throw new Exception("We have the wrong format of a file");
                        }
                        ;
                    }
                    catch
                    {
                        throw;
                    }

                    for (int rowNumber = 2; rowNumber <= excellRange.Rows.Count; rowNumber++)
                    {
                        DataRow dRow = dTable.NewRow();
                        for (int columnNumber = 1; columnNumber <= excellRange.Columns.Count; columnNumber++)
                        {
                            if ((excellRange.Cells[rowNumber, columnNumber] as Microsoft.Office.Interop.Excel.Range).Value2 != null)
                            {
                                dRow[columnNumber - 1] = (excellRange.Cells[rowNumber, columnNumber] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();
                            }
                        }
                        dTable.Rows.Add(dRow);
                        dTable.AcceptChanges();
                    }
                    excelStructure.XlWorkBook.Close(true, excelStructure.MisValue, excelStructure.MisValue);
                    excelStructure.XlExcel.Quit();
                    for (int i = 0; i < dTable.Rows.Count; i++)
                    {
                        var tableRow           = dTable.Rows[i];
                        FixedTimeEmployee fixT = new FixedTimeEmployee();
                        for (int j = 0; j < tableRow.ItemArray.Length; j++)
                        {
                            var currentValue = tableRow.ItemArray[j];
                            var columnNAme   = columnList[j];

                            if (employeeProperties.FirstOrDefault(zx => zx.Name == columnNAme).GetValue(fixT) is decimal)
                            {
                                employeeProperties.FirstOrDefault(zx => zx.Name == columnNAme).SetValue(fixT, Decimal.Parse(currentValue.ToString()));
                            }
                            else
                            {
                                employeeProperties.FirstOrDefault(zx => zx.Name == columnNAme).SetValue(fixT, currentValue);
                            }
                        }
                        Employees.Add(fixT);
                    }
                }
                else
                {
                    releaseObject(excellImport);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                releaseObject(excelStructure.XlWorkSheet);
                releaseObject(excelStructure.XlWorkBook);
                releaseObject(excelStructure.XlExcel);
                releaseObject(excellImport);
            }
        }