| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using Excel = Microsoft.Office.Interop.Excel;
- namespace СУБД_Агентство
- {
- public partial class FormReport : Form
- {
- public FormReport()
- {
- InitializeComponent();
- }
- private void FormReport_Load(object sender, EventArgs e)
- {
- // TODO: This line of code loads data into the 'dbAgentstvoDataSet1.Users' table. You can move, or remove it, as needed.
- this.usersTableAdapter.Fill(this.dbAgentstvoDataSet1.Users);
- SqlConnection con = new SqlConnection(Form1.txtcon); // подключение к БД
- con.Open(); // открыть подключение к базе данных
- // SQL-запрос
- string t = String.Format(@"
- select pok.fam + ' ' + pok.name as fiopok,
- prod.fam + ' ' + prod.name as fioprod,
- datesdelka
- from Sdelka, Potrebnost, predlozhenie,
- Users as pok, Users as prod
- where Sdelka.idpotrebnost = Potrebnost.idpotrebnost
- and Sdelka.idpredlozhenie = predlozhenie.idpredlozhenie
- and Potrebnost.idklient = pok.idusers
- and predlozhenie.idklient = prod.idusers
- and (Potrebnost.idrielter = {0} or predlozhenie.idrielter = {0})", lblIdRielter.Text);
- SqlCommand query1 = new SqlCommand(t, con);
- // выполнить SQL-запрос
- SqlDataReader rez = query1.ExecuteReader();
- // если есть строки в результате запроса
- if (rez.HasRows)
- // перебор строк в результате запроса
- while (rez.Read())
- // вывести данные в DataGridView
- dgvReport.Rows.Add(rez["fiopok"], rez["fioprod"], rez["datesdelka"]);
- con.Close(); // закрыть подключение к базе данных
- }
- private void btnExport_Click(object sender, EventArgs e)
- {
- // создать объект и запустить приложение Excel
- Excel.Application exapp = new Excel.Application();
- exapp.Visible = true; // сделать Excel видимым
- // открыть шаблон в режиме только для чтения
- exapp.Workbooks.Open(Application.StartupPath + "\\report1.xlsx", Type.Missing, true);
- // создать переменную для работы с первым листом рабочей книги
- Excel.Worksheet list = exapp.Worksheets.get_Item(1);
- int rowexcel = 4; // счетчик строк для Excel
- // перебор всех строк в DataGridView
- for(int i=0; i <= dgvReport.RowCount-1; i++)
- {
- // дата очередной i-й сделки
- DateTime datesdelki = Convert.ToDateTime(dgvReport.Rows[i].Cells[2].Value);
- // если i-я сделка была совершена в указанный диапазон дат
- if (datesdelki >= dtp1.Value.Date && datesdelki <= dtp2.Value.Date)
- {
- // порядковый номер
- list.Cells[rowexcel, 1] = rowexcel - 3;
- // ФИО покупателя
- list.Cells[rowexcel, 2] = dgvReport.Rows[i].Cells[0].Value;
- // ФИО продавца
- list.Cells[rowexcel, 3] = dgvReport.Rows[i].Cells[1].Value;
- // дата сделки
- list.Cells[rowexcel, 4] = datesdelki.ToString("dd.MM.yyyy");
- rowexcel++; // перейти к следующей строке в Excel
- }
- }
- rowexcel--;
- // нарисовать границы для таблицы
- list.get_Range("A4:D" + rowexcel).Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
- }
- }
- }
|