Отчет для зарплат.

This commit is contained in:
maksim 2024-12-08 17:52:25 +04:00
parent c36d12c9dd
commit e61e11a192
3 changed files with 105 additions and 4 deletions

View File

@ -114,6 +114,7 @@ namespace EmployeeManagmentView.Employee.Salary
}
}
private void TelephoneTextBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
e.Handled = !char.IsDigit(e.Text, 0);

View File

@ -48,6 +48,7 @@
Width="150" Height="40"
Margin="0,0,0,20"
Background="#004890"
Click="ExportToExcelButton_Click"
Foreground="White"
Style="{StaticResource RoundedButtonStyle}" />
</Grid>

View File

@ -1,4 +1,7 @@
using EmployeeManagmentContracts.BusinessLogicContracts;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
using EmployeeManagmentContracts.BusinessLogicContracts;
using EmployeeManagmentContracts.ViewModels;
using System;
using System.Collections.Generic;
@ -13,7 +16,8 @@ using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
namespace EmployeeManagmentView.Employee.Salary
{
@ -40,6 +44,100 @@ namespace EmployeeManagmentView.Employee.Salary
SalariesDataGrid.ItemsSource = _allSalaries;
}
private void ExportToExcelButton_Click(object sender, RoutedEventArgs e)
{
try
{
// Получаем данные о зарплатах
var salaries = _salaryLogic.GetFullList();
if (salaries == null || !salaries.Any())
{
MessageBox.Show("Нет данных для экспорта.", "Внимание", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
// Генерация Excel-файла
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Отчет_зарплаты.xlsx");
GenerateExcelReport(filePath, salaries);
MessageBox.Show($"Отчет успешно сохранен: {filePath}", "Успех", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка экспорта: {ex.Message}", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void GenerateExcelReport(string filePath, List<SalaryViewModel> salaries)
{
using (SpreadsheetDocument document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
{
// Создаем Workbook
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
// Создаем Worksheet
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Добавляем лист в Workbook
Sheets sheets = document.WorkbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet()
{
Id = document.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "Зарплаты"
};
sheets.Append(sheet);
// Получаем SheetData
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
// Заполняем заголовки
Row headerRow = new Row();
headerRow.Append(
CreateTextCell("A", "ID"),
CreateTextCell("B", "Сотрудник"),
CreateTextCell("C", "Часы"),
CreateTextCell("D", "Ставка"),
CreateTextCell("E", "Премия"),
CreateTextCell("F", "Дата"),
CreateTextCell("G", "Статус оплаты") // Добавлен заголовок для Passed
);
sheetData.AppendChild(headerRow);
// Заполняем данные
foreach (var salary in salaries)
{
Row row = new Row();
row.Append(
CreateTextCell("A", salary.Id.ToString()),
CreateTextCell("B", salary.EmployeeName),
CreateTextCell("C", salary.CountHours.ToString()),
CreateTextCell("D", salary.PriceHour.ToString("F2")),
CreateTextCell("E", salary.Premium?.ToString("F2") ?? "0"),
CreateTextCell("F", salary.Date.Value.ToString("dd.MM.yyyy")),
CreateTextCell("G", salary.Passed ? "Заплачено" : "Не заплачено") // Используем поле Passed
);
sheetData.AppendChild(row);
}
workbookPart.Workbook.Save();
}
}
// Метод для создания текстовой ячейки
private Cell CreateTextCell(string columnName, string text)
{
return new Cell
{
DataType = CellValues.String,
CellReference = columnName,
CellValue = new CellValue(text)
};
}
private void SearchTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
@ -57,13 +155,14 @@ namespace EmployeeManagmentView.Employee.Salary
(sal.EmployeeName?.ToLower().Contains(query) ?? false) ||
sal.CountHours.ToString().Contains(query) ||
sal.PriceHour.ToString().Contains(query) ||
sal.Passed.ToString().ToLower().Contains(query) ||
(sal.Premium.HasValue && sal.Premium.Value.ToString().Contains(query)) ||
(sal.Date.HasValue && sal.Date.Value.ToString("dd.MM.yyyy").Contains(query))
(sal.Date.HasValue && sal.Date.Value.ToString("dd.MM.yyyy").Contains(query)) ||
(sal.Passed ? "заплачено" : "не заплачено").Contains(query) // Добавлен фильтр для Passed
).ToList();
SalariesDataGrid.ItemsSource = filteredList;
}
}
}
}