123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using Microsoft.Office.Interop.Excel;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using WpfApp1.Entities;
- using Excel = Microsoft.Office.Interop.Excel;
- namespace WpfApp1
- {
- /// <summary>
- /// Логика взаимодействия для DiagramPage.xaml
- /// </summary>
- public partial class DiagramPage : Page
- {
- private user20Entities _context = new user20Entities();
- //List<string> listStatus = new List<string>()
- //{
- // "запланирована", "выполнена", "отмена", "исполняется"
- //};
- public DiagramPage()
- {
- InitializeComponent();
- //ChartPayments.ChartAreas.Add(new ChartArea("Main"));
- //var currentSeries = new Series("Количество")
- //{
- // IsValueShowAsLabel = true
- //};
- //ChartPayments.Series.Add(currentSeries);
- //ComboExecutor.ItemsSource = listStatus;
- //ComboChartTypes.ItemsSource = Enum.GetValues(typeof(SeriesChartType));
- }
- private void BtnExportToExcel_Click(object sender, RoutedEventArgs e)
- {
- var allUsers = _context.User.ToList().OrderBy(p => p.FirstName).ToList();
- var application = new Excel.Application();
- application.SheetsInNewWorkbook = allUsers.Count();
- Excel.Workbook workbook = application.Workbooks.Add(Type.Missing);
- int startRowIndex = 1;
- for (int i = 0; i < allUsers.Count(); i++)
- {
- Excel.Worksheet worksheet = application.Worksheets.Item[i + 1];
- worksheet.Name = allUsers[i].FirstName;
- worksheet.Cells[1][startRowIndex] = "Дата платежа";
- worksheet.Cells[2][startRowIndex] = "Название";
- worksheet.Cells[3][startRowIndex] = "Стоимость";
- worksheet.Cells[4][startRowIndex] = "Колличество";
- worksheet.Cells[5][startRowIndex] = "Сумма";
- startRowIndex++;
- var usersCategories = allUsers[i].Payments.OrderBy(p=>p.Date).GroupBy(p=>p.Category).OrderBy(p=>p.Key.Name);
- foreach (var groupCategory in usersCategories)
- {
- Excel.Range headRange = worksheet.Range[worksheet.Cells[1][startRowIndex], worksheet.Cells[5][startRowIndex]];
- headRange.Merge();
- headRange.Value = groupCategory.Key.Name;
- headRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
- headRange.Font.Italic = true;
- startRowIndex++;
- foreach (var payment in groupCategory)
- {
- worksheet.Cells[1][startRowIndex] = payment.Date.ToString("dd.MM.yyyy HH:mm");
- worksheet.Cells[2][startRowIndex] = payment.Name;
- worksheet.Cells[3][startRowIndex] = payment.Price;
- worksheet.Cells[4][startRowIndex] = payment.Num;
- worksheet.Cells[5][startRowIndex].formula = $"+C{startRowIndex}*D{startRowIndex}";
- worksheet.Cells[3][startRowIndex].NumberFormat =
- worksheet.Cells[3][startRowIndex].NumberFormat = "#, ###.00";
- startRowIndex++;
- }
- Excel.Range sumRange = worksheet.Range[worksheet.Cells[1][startRowIndex], worksheet.Cells[4][startRowIndex]];
- sumRange.Merge();
- sumRange.Value = "Итого:";
- sumRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
- worksheet.Cells[5][startRowIndex].Formula = $"=SUM(E{startRowIndex = groupCategory.Count()}:" +
- $"{startRowIndex - 1})";
- sumRange.Font.Bold = worksheet.Cells[5][startRowIndex].Font.Bold = true;
- worksheet.Cells[5][startRowIndex].NumberFormat = "#, ###.00";
- startRowIndex++;
- Excel.Range rangeBorders = worksheet.Range[worksheet.Cells[1][1], worksheet.Cells[5][startRowIndex - 1]];
- rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =
- rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle =
- rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle =
- rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle =
- rangeBorders.Borders[Excel.XlBordersIndex.xlEdge].LineStyle =
- rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =
- }
- }
- }
- }
- }
|