1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
| Object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook;
Microsoft.Office.Interop.Excel.Worksheet ExcelWorkSheet;
//Книга.
ExcelWorkBook = ExcelApp.Workbooks.Add(System.Reflection.Missing.Value);
//Таблица.
ExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelWorkBook.Worksheets.get_Item(1);
ExcelWorkSheet.Cells[1, 1] = "Поликлиника";
ExcelWorkSheet.Cells[2, 1] = "Пациент";
ExcelWorkSheet.Cells[3, 1] = "Код пациента";
ExcelWorkSheet.Cells[3, 2] = "ФИО";
ExcelWorkSheet.Cells[3, 3] = "Пол";
ExcelWorkSheet.Cells[3, 4] = "Дата рождения";
ExcelWorkSheet.Cells[3, 5] = "Адрес";
ExcelWorkSheet.Cells[3, 6] = "Паспорт";
ExcelWorkSheet.Cells[3, 7] = "Телефон";
ExcelWorkSheet.Cells[3, 8] = "Номер ОМС";
ExcelWorkSheet.Cells[3, 9] = "Снилс";
for (int i = 4; i < dataGridView2.Rows.Count; i++)
{
for (int j = 0; j < dataGridView2.ColumnCount; j++)
{
ExcelApp.Cells[i + 1, j + 1] = dataGridView2.Rows[i].Cells[j].Value;
}
}
(ExcelWorkSheet.Cells[1, 1] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[1, 1] as Microsoft.Office.Interop.Excel.Range).Font.Size = 12;
(ExcelWorkSheet.Cells[2, 1] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[2, 1] as Microsoft.Office.Interop.Excel.Range).Font.Size = 12;
(ExcelWorkSheet.Cells[3, 1] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[3, 2] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[3, 3] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[3, 4] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[3, 5] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[3, 6] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[3, 7] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[3, 8] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
(ExcelWorkSheet.Cells[3, 9] as Microsoft.Office.Interop.Excel.Range).Font.Bold = true;
Microsoft.Office.Interop.Excel.Range oRange;
oRange = ExcelWorkSheet.Range[ExcelWorkSheet.Cells[2, 1], ExcelWorkSheet.Cells[2, 3]];
oRange.Merge(Type.Missing);
Microsoft.Office.Interop.Excel.Range oRange1;
oRange1 = ExcelWorkSheet.Range[ExcelWorkSheet.Cells[1, 1], ExcelWorkSheet.Cells[1, 3]];
oRange1.Merge(Type.Missing);
(ExcelWorkSheet.Cells[3, 2] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[3, 3] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[3, 4] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[3, 5] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[3, 6] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[3, 7] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[3, 8] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[3, 9] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[4, 1] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[4, 2] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[4, 3] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[4, 4] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[4, 5] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[4, 6] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[4, 7] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[4, 8] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[4, 9] as Microsoft.Office.Interop.Excel.Range).EntireColumn.AutoFit();
(ExcelWorkSheet.Cells[1, 1] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[1, 2] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[1, 3] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[1, 3] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[1, 1] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[1, 1] as Microsoft.Office.Interop.Excel.Range).HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
(ExcelWorkSheet.Cells[2, 1] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 2] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 3] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 4] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 5] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 6] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 7] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 8] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 9] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 3] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 1] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[2, 1] as Microsoft.Office.Interop.Excel.Range).HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
(ExcelWorkSheet.Cells[3, 1] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 2] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 3] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 4] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 5] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 6] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 7] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 8] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 9] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 1] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 9] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 1] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 2] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 3] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 4] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 5] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 6] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 7] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 8] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
(ExcelWorkSheet.Cells[3, 9] as Microsoft.Office.Interop.Excel.Range).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
//Вызываем нашу созданную эксельку.
ExcelApp.Visible = true;
ExcelApp.UserControl = true; |