Как получить отчет Excel с другим полем на экране WPF в MVVM? - PullRequest
0 голосов
/ 03 апреля 2020

Я должен взять Incentive Reports с приложением WPF в шаблоне проектирования MVVM. У меня есть отчет о стимулировании. Он имеет EmployeeID Location EmployeeName против preorties. Я могу воспользоваться отчетом по мотивам для selectedmonth в выбранном году, и у меня есть кнопка «Подробно», которую я хочу, когда я нажимаю подробную кнопку, будет отчет по Excel за все дни в selectedmonth в выбранном году и различные предварительные требования из стимулирующего отчета. Мои предпочтительные отчеты: EmployeeID, EmployeeName, Location

Мои Excel Excel Preorties (я хочу их) День (1-31 или 1-30 в зависимости от выбранного месяца)

-EmployeeID (employeeID) -EmployeeName (Имя сотрудника HasWeekend (в соответствии с субботой или воскресеньем HasWeekend в файле Excel имеет значение true, в противном случае - false) HasPublicHoliday (в Publi c День праздника - значение true, в противном случае - false.) Вот так

My IncentiveDay.cs / Model Класс в шаблоне проектирования mvvm:

using Digi.DAL.Common;
using Digi.DAL.Entities;
using Digi.UI.Common.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Digi.UI.Models
{


    public class IncentiveDay : Bindable
    {
        private Employee _employee;
        public Employee Employee
        {
            get { return _employee; }
            set { Set(ref _employee, value, EmployeeChanged); }
        }

        public bool IsEmployeeWorking
        {
            //get { return _employee.Status == 1 || _day < _employee.LeaveDate; }
            get { return _day >= _employee.StartDate && _day <= _employee.LeaveDate; }
        }

        public bool HasEmployeeLeft
        {
            get { return _employee.LeaveDate <= _day; }
        }

        public bool IsException
        {
            get { return _exception != null; }
        }

        private IncentiveReportException _exception;
        public IncentiveReportException Exception
        {
            get { return _exception; }
            set { Set(ref _exception, value, () => OnPropertyChanged(nameof(IsException))); }
        }

        private double _workingHoursForOrigLocation;
        public double WorkedHoursAtOrigLocation
        {
            get { return _workingHoursForOrigLocation; }
            set { Set(ref _workingHoursForOrigLocation, value); }
        }

        private double _workingHoursForOtherLocation;
        public double WorkedHoursAtOtherLocation
        {
            get { return _workingHoursForOtherLocation; }
            set { Set(ref _workingHoursForOtherLocation, value); }
        }

        private DateTime _day;
        public DateTime Day
        {
            get { return _day; }
            set { Set(ref _day, value, DayChanged); }
        }

        public bool IsWeekend
        {
            get { return _day.IsWeekend(); }
        }

        public bool HasWeekend
        {
            get
            {
                if (IsException) return false;
                if (_employee.IsHalfTime) return false;
                if (_employee.IsPartialTime) return false;
                if (_employee.IsSupportStaff) return IsWeekend;
                if (_employee.IsFullTime) return IsWeekend;
                throw new ArgumentException("Invalid employee type.");
            }
        }

        private bool _isPublicHoliday;
        public bool IsPublicHoliday
        {
            get { return _isPublicHoliday; }
            set { Set(ref _isPublicHoliday, value); }
        }

        public bool HasPublicHoliday
        {
            get
            {
                if (IsException) return false;
                if (_employee.IsHalfTime) return false;
                if (_employee.IsPartialTime) return false;
                if (_employee.IsSupportStaff) return IsWeekend == false && _isPublicHoliday;
                if (_employee.IsFullTime) return IsWeekend == false && _isPublicHoliday;
                throw new ArgumentException("Invalid employee type.");
            }
        }

        private bool _isVacation;
        public bool IsVacation
        {
            get { return _isVacation; }
            set { Set(ref _isVacation, value); }
        }

        public bool HasVacation
        {
            get
            {
                if (IsException) return false;
                if (_employee.IsHalfTime) return false;
                if (_employee.IsPartialTime) return false;
                if (_employee.IsSupportStaff) return _isVacation;
                if (_employee.IsFullTime) return IsWeekend == false && IsPublicHoliday == false && _isVacation;
                throw new ArgumentException("Invalid employee type.");
            }
        }

        private ProjectTravel _projectTravel;
        public ProjectTravel ProjectTravel
        {
            get { return _projectTravel; }
            set { Set(ref _projectTravel, value, ProjectTravelChanged); }
        }

        public bool IsProjectTravel
        {
            get { return ProjectTravel != null && ProjectTravel.ReasonCode.InRangeOrEquals(1, 3); }
        }

        public bool HasProjectTravel
        {
            get
            {
                if (IsException) return false;
                if (_employee.IsHalfTime) return IsProjectTravel;
                if (_employee.IsPartialTime) return IsProjectTravel;
                if (_employee.IsSupportStaff) return false;
                if (_employee.IsFullTime) return IsWeekend == false && IsPublicHoliday == false && IsVacation == false && IsProjectTravel;
                throw new ArgumentException("Invalid employee type.");
            }
        }

        private bool _isTraining;
        public bool IsTraining
        {
            get { return _isTraining; }
            set { Set(ref _isTraining, value); }
        }

        public bool HasTraining
        {
            get
            {
                if (IsException) return false;
                if (_employee.IsHalfTime) return _isTraining && IsProjectTravel == false;
                if (_employee.IsPartialTime) return _isTraining && IsProjectTravel == false;
                if (_employee.IsSupportStaff) return false;
                if (_employee.IsFullTime) return IsWeekend == false && IsPublicHoliday == false && IsVacation == false && IsProjectTravel == false && _isTraining;
                throw new ArgumentException("Invalid employee type.");
            }
        }

        private bool _isEducationLeave;
        public bool IsEducationLeave
        {
            get { return _isEducationLeave; }
            set { Set(ref _isEducationLeave, value); }
        }

        public bool HasEducationLeave
        {
            get
            {
                if (IsException) return false;
                if (_employee.IsHalfTime) return false;
                if (_employee.IsPartialTime) return false;
                if (_employee.IsSupportStaff) return false;
                if (_employee.IsFullTime) return IsWeekend == false && IsPublicHoliday == false && IsVacation == false && IsProjectTravel == false && _isTraining == false && _isEducationLeave;
                throw new ArgumentException("Invalid employee type.");
            }
        }

        private EducationLeave _educationLeave;
        private int selectedYear;
        private int selectedMonth;

        public EducationLeave EducationLeave
        {
            get { return _educationLeave; }
            set { Set(ref _educationLeave, value, () => OnPropertyChanged(nameof(IsEducationLeave))); }
        }

        public bool IsTaxFree
        {
            get
            {
                if (IsException) return false;
                if (_employee.IsSupportStaff) return HasWeekend || HasPublicHoliday || HasVacation;
                return HasWeekend || HasPublicHoliday || HasVacation || HasProjectTravel || HasTraining;
            }
        }

        public IncentiveDay(Employee employee, DateTime day)
        {
            Employee = employee;
            Day = day;
        }

        public IncentiveDay(Employee employee, int selectedYear, int selectedMonth)
        {
            Employee = employee;
            this.selectedYear = selectedYear;
            this.selectedMonth = selectedMonth;
        }

и это моя кнопка «Подробно» (для создания отчета Excel)

#region MonthlyDetailReportCommand
        private DelegateCommand _monthlyDetailReportCommand;
        public DelegateCommand MonthlyDetailReportCommand
        {
            get { return _monthlyDetailReportCommand ?? (_monthlyDetailReportCommand = new DelegateCommand(CanMonthlyDetailReport, MonthlyDetailReport)); }
        }

        private bool CanMonthlyDetailReport()
        {
            return IncentiveMonths != null;
        }

        private void MonthlyDetailReport()
        {
            var app = new Application();
            var workbook = app.Workbooks.Add();
            var detailSheet = workbook.Sheets[1] as Worksheet;
            if (detailSheet != null) detailSheet.Name = "Detail Monthly Report";

            var detailSheetHeaders = new string[] {"Day", "Emp.ID", "Emp.Name", "HasWeekend", "HasVacation", "HasProjectTravel", "HasTraining", "HasPublicHoliday", "HasEducationLeave"};
            detailSheet.Range["A1"].Resize[1, detailSheetHeaders.Count()].Value = detailSheetHeaders;

            var rowCount = IncentiveDays.Count();
            var colCount = detailSheetHeaders.Count();
            var DataArray = new object[rowCount + 1, colCount + 1];

            for (int row = 0, col = 0; row < rowCount; row++)
            {
                DataArray[row, col] = IncentiveDays[row].Day;
                DataArray[row, col + 1] = IncentiveDays[row].Employee.Id;
                DataArray[row, col + 2] = IncentiveDays[row].Employee.FullName;
                DataArray[row, col + 3] = IncentiveDays[row].HasWeekend;
                DataArray[row, col + 4] = IncentiveDays[row].HasVacation;
                DataArray[row, col + 5] = IncentiveDays[row].HasProjectTravel;
                DataArray[row, col + 6] = IncentiveDays[row].HasTraining;
                DataArray[row, col + 7] = IncentiveDays[row].HasPublicHoliday;
                DataArray[row, col + 8] = IncentiveDays[row].HasEducationLeave;

            }

            detailSheet.Range["A2"].Resize[rowCount+1,colCount+1].Value = DataArray;
            detailSheet.Range["A1:Z1"].Font.FontStyle = "Bold";
            detailSheet.Range["A1:Z1"].Font.Color = Color.White;
            detailSheet.Range["A1:Z1"].Interior.Color = Color.RoyalBlue;
            detailSheet.Columns.HorizontalAlignment = XlHAlign.xlHAlignCenter;
            detailSheet.Columns.VerticalAlignment = XlVAlign.xlVAlignCenter;
            detailSheet.Columns["F"].NumberFormat = ExcelDefinitions.DateFormat;
            detailSheet.Columns["G"].NumberFormat = ExcelDefinitions.DateFormat;
              detailSheet.Columns.AutoFit();

            app.Visible = true;
        }
        #endregion
    }
}

Как получить подробный отчет в Excel с различными полями (как вы видите в кнопочной команде)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...