DiagramPage.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Microsoft.Office.Interop.Excel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using WpfApp1.Entities;
  17. using Excel = Microsoft.Office.Interop.Excel;
  18. namespace WpfApp1
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для DiagramPage.xaml
  22. /// </summary>
  23. public partial class DiagramPage : Page
  24. {
  25. private user20Entities _context = new user20Entities();
  26. //List<string> listStatus = new List<string>()
  27. //{
  28. // "запланирована", "выполнена", "отмена", "исполняется"
  29. //};
  30. public DiagramPage()
  31. {
  32. InitializeComponent();
  33. //ChartPayments.ChartAreas.Add(new ChartArea("Main"));
  34. //var currentSeries = new Series("Количество")
  35. //{
  36. // IsValueShowAsLabel = true
  37. //};
  38. //ChartPayments.Series.Add(currentSeries);
  39. //ComboExecutor.ItemsSource = listStatus;
  40. //ComboChartTypes.ItemsSource = Enum.GetValues(typeof(SeriesChartType));
  41. }
  42. private void BtnExportToExcel_Click(object sender, RoutedEventArgs e)
  43. {
  44. var allUsers = _context.User.ToList().OrderBy(p => p.FirstName).ToList();
  45. var application = new Excel.Application();
  46. application.SheetsInNewWorkbook = allUsers.Count();
  47. Excel.Workbook workbook = application.Workbooks.Add(Type.Missing);
  48. int startRowIndex = 1;
  49. for (int i = 0; i < allUsers.Count(); i++)
  50. {
  51. Excel.Worksheet worksheet = application.Worksheets.Item[i + 1];
  52. worksheet.Name = allUsers[i].FirstName;
  53. worksheet.Cells[1][startRowIndex] = "Дата платежа";
  54. worksheet.Cells[2][startRowIndex] = "Название";
  55. worksheet.Cells[3][startRowIndex] = "Стоимость";
  56. worksheet.Cells[4][startRowIndex] = "Колличество";
  57. worksheet.Cells[5][startRowIndex] = "Сумма";
  58. startRowIndex++;
  59. var usersCategories = allUsers[i].Payments.OrderBy(p=>p.Date).GroupBy(p=>p.Category).OrderBy(p=>p.Key.Name);
  60. foreach (var groupCategory in usersCategories)
  61. {
  62. Excel.Range headRange = worksheet.Range[worksheet.Cells[1][startRowIndex], worksheet.Cells[5][startRowIndex]];
  63. headRange.Merge();
  64. headRange.Value = groupCategory.Key.Name;
  65. headRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
  66. headRange.Font.Italic = true;
  67. startRowIndex++;
  68. foreach (var payment in groupCategory)
  69. {
  70. worksheet.Cells[1][startRowIndex] = payment.Date.ToString("dd.MM.yyyy HH:mm");
  71. worksheet.Cells[2][startRowIndex] = payment.Name;
  72. worksheet.Cells[3][startRowIndex] = payment.Price;
  73. worksheet.Cells[4][startRowIndex] = payment.Num;
  74. worksheet.Cells[5][startRowIndex].formula = $"+C{startRowIndex}*D{startRowIndex}";
  75. worksheet.Cells[3][startRowIndex].NumberFormat =
  76. worksheet.Cells[3][startRowIndex].NumberFormat = "#, ###.00";
  77. startRowIndex++;
  78. }
  79. Excel.Range sumRange = worksheet.Range[worksheet.Cells[1][startRowIndex], worksheet.Cells[4][startRowIndex]];
  80. sumRange.Merge();
  81. sumRange.Value = "Итого:";
  82. sumRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
  83. worksheet.Cells[5][startRowIndex].Formula = $"=SUM(E{startRowIndex = groupCategory.Count()}:" +
  84. $"{startRowIndex - 1})";
  85. sumRange.Font.Bold = worksheet.Cells[5][startRowIndex].Font.Bold = true;
  86. worksheet.Cells[5][startRowIndex].NumberFormat = "#, ###.00";
  87. startRowIndex++;
  88. Excel.Range rangeBorders = worksheet.Range[worksheet.Cells[1][1], worksheet.Cells[5][startRowIndex - 1]];
  89. rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =
  90. rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle =
  91. rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle =
  92. rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle =
  93. rangeBorders.Borders[Excel.XlBordersIndex.xlEdge].LineStyle =
  94. rangeBorders.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =
  95. }
  96. }
  97. }
  98. }
  99. }