C#-Xuất dữ liệu SQL Server ra Excel để báo cáo



1
2
3
4
5
| private void btnTaiDuLieu_Click( object sender, EventArgs e) { CSDL_MAUDataContext context = new CSDL_MAUDataContext(); gvSanPham.DataSource = context.SanPhams.ToList(); } |



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
| 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 Excel = Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel; namespace TestReportExcel { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnTaiDuLieu_Click( object sender, EventArgs e) { CSDL_MAUDataContext context = new CSDL_MAUDataContext(); gvSanPham.DataSource = context.SanPhams.ToList(); } private void btnXuatExcel_Click( object sender, EventArgs e) { try { string saveExcelFile = @"f:\excel_report.xlsx" ; Excel.Application xlApp = new Excel.Application(); if (xlApp == null ) { MessageBox.Show( "Lỗi không thể sử dụng được thư viện EXCEL" ); return ; } xlApp.Visible = false ; object misValue = System.Reflection.Missing.Value; Workbook wb = xlApp.Workbooks.Add(misValue); Worksheet ws = (Worksheet)wb.Worksheets[1]; if (ws == null ) { MessageBox.Show( "Không thể tạo được WorkSheet" ); return ; } int row = 1; string fontName = "Times New Roman" ; int fontSizeTieuDe = 18; int fontSizeTenTruong = 14; int fontSizeNoiDung = 12; //Xuất dòng Tiêu đề của File báo cáo: Lưu ý Range row1_TieuDe_ThongKeSanPham = ws.get_Range( "A1" , "E1" ); row1_TieuDe_ThongKeSanPham.Merge(); row1_TieuDe_ThongKeSanPham.Font.Size = fontSizeTieuDe; row1_TieuDe_ThongKeSanPham.Font.Name = fontName; row1_TieuDe_ThongKeSanPham.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; row1_TieuDe_ThongKeSanPham.Value2 = "Thống kê sản phẩm" ; //Tạo Ô Số Thứ Tự (STT) Range row23_STT = ws.get_Range( "A2" , "A3" ); //Cột A dòng 2 và dòng 3 row23_STT.Merge(); row23_STT.Font.Size = fontSizeTenTruong; row23_STT.Font.Name = fontName; row23_STT.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter; row23_STT.Value2 = "STT" ; //Tạo Ô Mã Sản phẩm : Range row23_MaSP = ws.get_Range( "B2" , "B3" ); //Cột B dòng 2 và dòng 3 row23_MaSP.Merge(); row23_MaSP.Font.Size = fontSizeTenTruong; row23_MaSP.Font.Name = fontName; row23_MaSP.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter; row23_MaSP.Value2 = "Mã Sản Phẩm" ; row23_MaSP.ColumnWidth = 20; //Tạo Ô Tên Sản phẩm : Range row23_TenSP = ws.get_Range( "C2" , "C3" ); //Cột C dòng 2 và dòng 3 row23_TenSP.Merge(); row23_TenSP.Font.Size = fontSizeTenTruong; row23_TenSP.Font.Name = fontName; row23_TenSP.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter; row23_TenSP.ColumnWidth = 20; row23_TenSP.Value2 = "Tên Sản Phẩm" ; //Tạo Ô Giá Sản phẩm : Range row2_GiaSP = ws.get_Range( "D2" , "E2" ); //Cột D->E của dòng 2 row2_GiaSP.Merge(); row2_GiaSP.Font.Size = fontSizeTenTruong; row2_GiaSP.Font.Name = fontName; row2_GiaSP.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter; row2_GiaSP.Value2 = "Giá Sản Phẩm" ; //Tạo Ô Giá Nhập: Range row3_GiaNhap = ws.get_Range( "D3" , "D3" ); //Ô D3 row3_GiaNhap.Font.Size = fontSizeTenTruong; row3_GiaNhap.Font.Name = fontName; row3_GiaNhap.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; row3_GiaNhap.Value2 = "Giá Nhập" ; row3_GiaNhap.ColumnWidth = 20; //Tạo Ô Giá Xuất: Range row3_GiaXuat = ws.get_Range( "E3" , "E3" ); //Ô E3 row3_GiaXuat.Font.Size = fontSizeTenTruong; row3_GiaXuat.Font.Name = fontName; row3_GiaXuat.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; row3_GiaXuat.Value2 = "Giá Xuất" ; row3_GiaXuat.ColumnWidth = 20; //Tô nền vàng các cột tiêu đề: Range row23_CotTieuDe = ws.get_Range( "A2" , "E3" ); //nền vàng row23_CotTieuDe.Interior.Color = ColorTranslator.ToOle(System.Drawing.Color.Yellow); //in đậm row23_CotTieuDe.Font.Bold = true ; //chữ đen row23_CotTieuDe.Font.Color = ColorTranslator.ToOle(System.Drawing.Color.Black); int stt = 0; row = 3; //dữ liệu xuất bắt đầu từ dòng số 4 trong file Excel (khai báo 3 để vào vòng lặp nó ++ thành 4) CSDL_MAUDataContext context = new CSDL_MAUDataContext(); foreach (SanPham sp in context.SanPhams) { stt++; row++; dynamic []arr = { stt,sp.MaSP,sp.TenSP,sp.DonGiaNhap,sp.DonGiaXuat}; Range rowData = ws.get_Range( "A" +row, "E" +row); //Lấy dòng thứ row ra để đổ dữ liệu rowData.Font.Size = fontSizeNoiDung; rowData.Font.Name = fontName; rowData.Value2 = arr; } //Kẻ khung toàn bộ BorderAround(ws.get_Range( "A2" , "E" + row)); //Lưu file excel xuống Ổ cứng wb.SaveAs(saveExcelFile); //đóng file để hoàn tất quá trình lưu trữ wb.Close( true , misValue, misValue); //thoát và thu hồi bộ nhớ cho COM xlApp.Quit(); releaseObject(ws); releaseObject(wb); releaseObject(xlApp); //Mở File excel sau khi Xuất thành công System.Diagnostics.Process.Start(saveExcelFile); } catch (Exception ex) { MessageBox.Show(ex.Message); } } //Hàm kẻ khung cho Excel private void BorderAround(Range range) { Borders borders = range.Borders; borders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous; borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous; borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous; borders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous; borders.Color = Color.Black; borders[XlBordersIndex.xlInsideVertical].LineStyle = XlLineStyle.xlContinuous; borders[XlBordersIndex.xlInsideHorizontal].LineStyle = XlLineStyle.xlContinuous; borders[XlBordersIndex.xlDiagonalUp].LineStyle = XlLineStyle.xlLineStyleNone; borders[XlBordersIndex.xlDiagonalDown].LineStyle = XlLineStyle.xlLineStyleNone; } //Hàm thu hồi bộ nhớ cho COM Excel private static void releaseObject( object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null ; } catch (Exception ex) { Console.WriteLine(ex.Message); obj = null ; } finally { GC.Collect(); } } } } |