码迷,mamicode.com
首页 > Windows程序 > 详细

C#winform导出数据到Excel的类

时间:2016-03-08 21:35:50      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

  1         /// <summary>
  2         /// 构造函数
  3         /// </summary>
  4         public ExportData()
  5         {
  6 
  7         }
  8         /// <summary>
  9         /// 保存文件名
 10         /// </summary>
 11         private string savefilename = "";
 12         /// <summary>
 13         /// 准备导出的数据容器
 14         /// </summary>
 15         private System.Windows.Forms.DataGridView datagv;
 16         /// <summary>
 17         /// 导出excel文档
 18         /// </summary>
 19         /// <param name="d">数据所在控件</param>
 20         public void OutDataToExcelByDataSource(System.Windows.Forms.DataGridView dgview)
 21         {
 22             try
 23             {
 24                 if (dgview.Columns.Count < 1)
 25                 {
 26                     MessageBox.Show("没有导出数据。");
 27                     return;
 28                 }
 29                 SaveFileDialog sfd = new SaveFileDialog();
 30                 sfd.ValidateNames = true;
 31                 sfd.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx|Access文件(*.mdb)|*.mdb|DBF文件(*.dbf)|*.dbf";
 32                 if (sfd.ShowDialog() != DialogResult.OK)
 33                 {
 34                     return;
 35                 }
 36                 this.savefilename = sfd.FileName;
 37                 //检测文件名是否合法
 38                 string tryfile = "";
 39                 tryfile = savefilename.Substring(savefilename.LastIndexOf("\\") + 1);
 40                 tryfile = tryfile.Substring(0, tryfile.LastIndexOf("."));
 41                 foreach (char c in tryfile)
 42                 {
 43                     if ("<>?:[]|*".Contains(c.ToString()))
 44                     {
 45                         MessageBox.Show("文件名包含非法字符.");
 46                         return;
 47                     }
 48                 }
 49                 this.datagv = dgview;
 50                 if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")) == ".xlsx" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")) == ".xls")
 51                 {
 52                     outToExcelByDataSource();
 53                 }
 54                 else if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")) == ".mdb" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")) == ".dbf")
 55                 {
 56                     outToAccessByDataSource();
 57                 }
 58             }
 59             catch (Exception W)
 60             {
 61                 MessageBox.Show(W.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
 62             }
 63         }
 64         /// <summary>
 65         /// 导出excel文档
 66         /// </summary>
 67         /// <param name="d">数据所在控件</param>
 68         public void OutDataToExcelByDataTable(System.Windows.Forms.DataGridView dgview)
 69         {
 70             try
 71             {
 72                 if (dgview.Columns.Count < 1)
 73                 {
 74                     MessageBox.Show("没有导出数据。");
 75                     return;
 76                 }
 77                 SaveFileDialog sfd = new SaveFileDialog();
 78                 sfd.ValidateNames = true;
 79                 sfd.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx|Access文件(*.mdb)|*.mdb|DBF文件(*.dbf)|*.dbf"; ;
 80                 if (sfd.ShowDialog() != DialogResult.OK)
 81                 {
 82                     return;
 83                 }
 84                 this.savefilename = sfd.FileName;
 85                 //检测文件名是否合法
 86                 string tryfile = "";
 87                 tryfile = savefilename.Substring(savefilename.LastIndexOf("\\") + 1);
 88                 tryfile = tryfile.Substring(0, tryfile.LastIndexOf("."));
 89                 foreach (char c in tryfile)
 90                 {
 91                     if ("<>?:[]|*".Contains(c.ToString()))
 92                     {
 93                         MessageBox.Show("文件名包含非法字符.");
 94                         return;
 95                     }
 96                 }
 97                 this.datagv = dgview;
 98                 if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".xlsx" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".xls")
 99                 {
100                     outToExcelByDataTable();
101                 }
102                 else if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".mdb" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".dbf")
103                 {
104                     outToAccessByDataTable();
105                 }
106             }
107             catch (Exception W)
108             {
109                 MessageBox.Show(W.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
110             }
111         }
112         /// <summary>
113         /// 导出文档
114         /// </summary>
115         public void OutDataByDataTable(DataTable tab)
116         {
117             try
118             {
119                 if (tab.Columns.Count < 1)
120                 {
121                     MessageBox.Show("没有导出数据。");
122                     return;
123                 }
124                 SaveFileDialog sfd = new SaveFileDialog();
125                 sfd.ValidateNames = true;
126                 sfd.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx|Access文件(*.mdb)|*.mdb|DBF文件(*.dbf)|*.dbf";
127                 if (sfd.ShowDialog() != DialogResult.OK)
128                 {
129                     return;
130                 }
131                 this.savefilename = sfd.FileName;
132                 //检测文件名是否合法
133                 string tryfile = "";
134                 tryfile = savefilename.Substring(savefilename.LastIndexOf("\\") + 1);
135                 tryfile = tryfile.Substring(0, tryfile.LastIndexOf("."));
136                 foreach (char c in tryfile)
137                 {
138                     if ("<>?:[]|*".Contains(c.ToString()))
139                     {
140                         MessageBox.Show("文件名包含非法字符.");
141                         return;
142                     }
143                 }
144                 if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".xlsx" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".xls")
145                 {
146                     ExportExcelForm of = new ExportExcelForm();
147                     of.mytable = tab;
148                     if (of.mytable == null)
149                     {
150                         MessageBox.Show("没有导出数据。");
151                         return;
152                     }
153                     of.savefimename = this.savefilename;
154                     of.Show();
155                 }
156                 else if (sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".mdb" || sfd.FileName.Substring(sfd.FileName.LastIndexOf(".")).ToLower() == ".dbf")
157                 {
158                     ExportAccessForm of = new ExportAccessForm();
159                     of.mytable = tab;
160                     if (of.mytable == null)
161                     {
162                         MessageBox.Show("没有导出数据。");
163                         return;
164                     }
165                     of.savefimename = this.savefilename;
166                     of.Show();
167                 }
168             }
169             catch (Exception W)
170             {
171                 MessageBox.Show(W.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
172             }
173         }
174         /// <summary>
175         /// 弹出导出窗口
176         /// </summary>
177         private void outToExcelByDataSource()
178         {
179             ExportExcelForm of = new ExportExcelForm();
180             of.mytable = (DataTable)this.datagv.DataSource;
181             if (of.mytable == null)
182             {
183                 MessageBox.Show("没有导出数据。");
184                 return;
185             }
186             of.savefimename = this.savefilename;
187             of.Show();
188         }
189         /// <summary>
190         /// 弹出导出窗口
191         /// </summary>
192         private void outToExcelByDataTable()
193         {
194             ExportExcelForm of = new ExportExcelForm();
195 
196             DataTable dt = new DataTable();
197             for (int i = 0; i < this.datagv.ColumnCount; i++)
198             {
199                 dt.Columns.Add(this.datagv.Columns[i].HeaderText);
200             }
201             DataRow dr;
202             for (int i = 0; i < this.datagv.RowCount; i++)
203             {
204                 dr = dt.NewRow();
205                 for (int j = 0; j < this.datagv.ColumnCount; j++)
206                 {
207                     if (this.datagv.Columns[j].HeaderText == "sqlstring")
208                     {
209                         dr[j] = "";
210                     }
211                     else
212                     {
213                         if (this.datagv.Rows[i].Cells[j].Value == null)
214                         {
215                             dr[j] = "";
216                         }
217                         else
218                         {
219                             dr[j] = this.datagv.Rows[i].Cells[j].Value.ToString();
220                         }
221                     }
222                 }
223                 dt.Rows.Add(dr);
224             }
225             of.mytable = dt;
226             if (of.mytable == null)
227             {
228                 MessageBox.Show("没有导出数据。");
229                 return;
230             }
231             of.savefimename = this.savefilename;
232             of.Show();
233         }
234         /// <summary>
235         /// 弹出导出窗口
236         /// </summary>
237         private void outToAccessByDataSource()
238         {
239             if (this.savefilename.Substring(this.savefilename.LastIndexOf(".")).ToLower() == ".mdb")
240             {
241                 ExportAccessForm of = new ExportAccessForm();
242                 of.mytable = (DataTable)this.datagv.DataSource;
243                 if (of.mytable == null)
244                 {
245                     MessageBox.Show("没有导出数据。");
246                     return;
247                 }
248                 of.savefimename = this.savefilename;
249                 of.Show();
250             }
251             else if (this.savefilename.Substring(this.savefilename.LastIndexOf(".")).ToLower() == ".dbf")
252             {
253                 ExportDBFForm of = new ExportDBFForm();
254                 of.mytable = (DataTable)this.datagv.DataSource;
255                 if (of.mytable == null)
256                 {
257                     MessageBox.Show("没有导出数据。");
258                     return;
259                 }
260                 of.savefimename = this.savefilename;
261                 of.Show();
262             }
263         }
264         /// <summary>
265         /// 弹出导出窗口
266         /// </summary>
267         private void outToAccessByDataTable()
268         {
269             try
270             {
271                 if (this.savefilename.Substring(this.savefilename.LastIndexOf(".")).ToLower() == ".mdb")
272                 {
273                     ExportAccessForm of = new ExportAccessForm();
274                     DataTable dt = new DataTable();
275                     for (int i = 0; i < this.datagv.ColumnCount; i++)
276                     {
277                         dt.Columns.Add(this.datagv.Columns[i].HeaderText);
278                     }
279                     DataRow dr;
280                     for (int i = 0; i < this.datagv.RowCount; i++)
281                     {
282                         dr = dt.NewRow();
283                         for (int j = 0; j < this.datagv.ColumnCount; j++)
284                         {
285                             if (this.datagv.Columns[j].HeaderText == "sqlstring")
286                             {
287                                 dr[j] = "";
288                             }
289                             else
290                             {
291                                 if (this.datagv.Rows[i].Cells[j].Value == null)
292                                 {
293                                     dr[j] = "";
294                                 }
295                                 else
296                                 {
297                                     dr[j] = this.datagv.Rows[i].Cells[j].Value.ToString();
298                                 }
299                             }
300                         }
301                         dt.Rows.Add(dr);
302                     }
303                     of.mytable = dt;
304                     if (of.mytable == null)
305                     {
306                         MessageBox.Show("没有导出数据。");
307                         return;
308                     }
309                     of.savefimename = this.savefilename; 
310                     of.Show();
311                 }
312                 else if (this.savefilename.Substring(this.savefilename.LastIndexOf(".")).ToLower() == ".dbf")
313                 {
314                     ExportDBFForm of = new ExportDBFForm();
315                     DataTable dt = new DataTable();
316                     for (int i = 0; i < this.datagv.ColumnCount; i++)
317                     {
318                         dt.Columns.Add(this.datagv.Columns[i].HeaderText);
319                     }
320                     DataRow dr;
321                     for (int i = 0; i < this.datagv.RowCount; i++)
322                     {
323                         dr = dt.NewRow();
324                         for (int j = 0; j < this.datagv.ColumnCount; j++)
325                         {
326                             if (this.datagv.Columns[j].HeaderText == "sqlstring")
327                             {
328                                 dr[j] = "";
329                             }
330                             else
331                             {
332                                 if (this.datagv.Rows[i].Cells[j].Value == null)
333                                 {
334                                     dr[j] = "";
335                                 }
336                                 else
337                                 {
338                                     dr[j] = this.datagv.Rows[i].Cells[j].Value.ToString();
339                                 }
340                             }
341                         }
342                         dt.Rows.Add(dr);
343                     }
344                     of.mytable = dt;
345                     if (of.mytable == null)
346                     {
347                         MessageBox.Show("没有导出数据。");
348                         return;
349                     }
350                     of.savefimename = this.savefilename;
351                     of.Show();
352                 }
353             }
354             catch
355             {
356 
357             }
358         }

 

C#winform导出数据到Excel的类

标签:

原文地址:http://www.cnblogs.com/luoxiaozhao/p/5255470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!