1 /// <summary>
2 /// DataGridView导出EXCEL
3 /// </summary>
4 /// <param name="fileNameString">要保存的文件路径</param>
5 /// <param name="dgvCAPA">DataGridView</param>
6 private void dgvToExcel(string fileNameString, DataGridView dgvCAPA)
7 {
8 //定义表格内数据的行数和列数
9 int rowscount = dgvCAPA.Rows.Count;
10 int colscount = dgvCAPA.Columns.Count;
11 //行数必须大于0
12 if (rowscount <= 0)
13 {
14 MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
15 return;
16 }
17 //列数必须大于0
18 if (colscount <= 0)
19 {
20 MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
21 return;
22 }
23 //行数不可以大于65536
24 if (rowscount > 65536)
25 {
26 MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
27 return;
28 }
29 //列数不可以大于255
30 if (colscount > 255)
31 {
32 MessageBox.Show("数据记录行数太多,不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
33 return;
34 }
35 //验证以fileNameString命名的文件是否存在,如果存在删除它
36 FileInfo file = new FileInfo(fileNameString);
37 if (file.Exists)
38 {
39 try
40 {
41 file.Delete();
42 }
43 catch (Exception error)
44 {
45 MessageBox.Show(error.Message, "删除失败 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
46 return;
47 }
48 }
49 Microsoft.Office.Interop.Excel.Application objExcel = null;
50 Microsoft.Office.Interop.Excel.Workbook objWorkbook = null;
51 Microsoft.Office.Interop.Excel.Worksheet objsheet = null;
52 try
53 {
54 //申明对象
55 objExcel = new Microsoft.Office.Interop.Excel.Application();
56 objWorkbook = objExcel.Workbooks.Add(Missing.Value);
57 objsheet = (Microsoft.Office.Interop.Excel.Worksheet)objWorkbook.ActiveSheet;
58 //设置EXCEL不可见
59 objExcel.Visible = false;
60
61 //向Excel中写入表格的表头
62 int displayColumnsCount = 1;
63 for (int i = 0; i <= dgvCAPA.ColumnCount - 1; i++)
64 {
65 if (dgvCAPA.Columns[i].Visible == true)
66 {
67 objExcel.Cells[1, displayColumnsCount] = dgvCAPA.Columns[i].HeaderText.Trim();
68 displayColumnsCount++;
69 }
70 }
71
72 //向Excel中逐行逐列写入表格中的数据
73 for (int row = 0; row <= dgvCAPA.RowCount - 1; row++)
74 {
75 //tempProgressBar.PerformStep();
76 displayColumnsCount = 1;
77 for (int col = 0; col < colscount; col++)
78 {
79 if (dgvCAPA.Columns[col].Visible == true)
80 {
81 try
82 {
83
84 objExcel.Cells[row + 2, displayColumnsCount] = dgvCAPA.Rows[row].Cells[col].Value.ToString().Trim();
85 displayColumnsCount++;
86 }
87 catch (Exception)
88 {
89 }
90 }
91 }
92 }
93
94
95 //保存文件
96 objWorkbook.SaveAs(fileNameString, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
97 Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,
98 Missing.Value, Missing.Value);
99 }
100 catch (Exception error)
101 {
102 MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
103 return;
104 }
105 finally
106 {
107 //关闭Excel应用
108 if (objWorkbook != null) objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
109 if (objExcel.Workbooks != null) objExcel.Workbooks.Close();
110 if (objExcel != null) objExcel.Quit();
111
112 objsheet = null;
113 objWorkbook = null;
114 objExcel = null;
115 GC.Collect();
116 }
117 }
请发表评论