FormReport.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. using Excel = Microsoft.Office.Interop.Excel;
  12. namespace СУБД_Агентство
  13. {
  14. public partial class FormReport : Form
  15. {
  16. public FormReport()
  17. {
  18. InitializeComponent();
  19. }
  20. private void FormReport_Load(object sender, EventArgs e)
  21. {
  22. // TODO: This line of code loads data into the 'dbAgentstvoDataSet1.Users' table. You can move, or remove it, as needed.
  23. this.usersTableAdapter.Fill(this.dbAgentstvoDataSet1.Users);
  24. SqlConnection con = new SqlConnection(Form1.txtcon); // подключение к БД
  25. con.Open(); // открыть подключение к базе данных
  26. // SQL-запрос
  27. string t = String.Format(@"
  28. select pok.fam + ' ' + pok.name as fiopok,
  29. prod.fam + ' ' + prod.name as fioprod,
  30. datesdelka
  31. from Sdelka, Potrebnost, predlozhenie,
  32. Users as pok, Users as prod
  33. where Sdelka.idpotrebnost = Potrebnost.idpotrebnost
  34. and Sdelka.idpredlozhenie = predlozhenie.idpredlozhenie
  35. and Potrebnost.idklient = pok.idusers
  36. and predlozhenie.idklient = prod.idusers
  37. and (Potrebnost.idrielter = {0} or predlozhenie.idrielter = {0})", lblIdRielter.Text);
  38. SqlCommand query1 = new SqlCommand(t, con);
  39. // выполнить SQL-запрос
  40. SqlDataReader rez = query1.ExecuteReader();
  41. // если есть строки в результате запроса
  42. if (rez.HasRows)
  43. // перебор строк в результате запроса
  44. while (rez.Read())
  45. // вывести данные в DataGridView
  46. dgvReport.Rows.Add(rez["fiopok"], rez["fioprod"], rez["datesdelka"]);
  47. con.Close(); // закрыть подключение к базе данных
  48. }
  49. private void btnExport_Click(object sender, EventArgs e)
  50. {
  51. // создать объект и запустить приложение Excel
  52. Excel.Application exapp = new Excel.Application();
  53. exapp.Visible = true; // сделать Excel видимым
  54. // открыть шаблон в режиме только для чтения
  55. exapp.Workbooks.Open(Application.StartupPath + "\\report1.xlsx", Type.Missing, true);
  56. // создать переменную для работы с первым листом рабочей книги
  57. Excel.Worksheet list = exapp.Worksheets.get_Item(1);
  58. int rowexcel = 4; // счетчик строк для Excel
  59. // перебор всех строк в DataGridView
  60. for(int i=0; i <= dgvReport.RowCount-1; i++)
  61. {
  62. // дата очередной i-й сделки
  63. DateTime datesdelki = Convert.ToDateTime(dgvReport.Rows[i].Cells[2].Value);
  64. // если i-я сделка была совершена в указанный диапазон дат
  65. if (datesdelki >= dtp1.Value.Date && datesdelki <= dtp2.Value.Date)
  66. {
  67. // порядковый номер
  68. list.Cells[rowexcel, 1] = rowexcel - 3;
  69. // ФИО покупателя
  70. list.Cells[rowexcel, 2] = dgvReport.Rows[i].Cells[0].Value;
  71. // ФИО продавца
  72. list.Cells[rowexcel, 3] = dgvReport.Rows[i].Cells[1].Value;
  73. // дата сделки
  74. list.Cells[rowexcel, 4] = datesdelki.ToString("dd.MM.yyyy");
  75. rowexcel++; // перейти к следующей строке в Excel
  76. }
  77. }
  78. rowexcel--;
  79. // нарисовать границы для таблицы
  80. list.get_Range("A4:D" + rowexcel).Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
  81. }
  82. }
  83. }