没有合适的资源?快使用搜索试试~ 我知道了~
C#实现将DataGridView中的数据导入到Excel
4星 · 超过85%的资源 需积分: 19 28 下载量 145 浏览量
2011-12-10
14:34:34
上传
评论 1
收藏 3KB TXT 举报
温馨提示
试读
2页
C#实现将DataGridView中的数据导入到Excel的详细代码!
资源推荐
资源详情
资源评论
1.using System;
2.using System.Collections.Generic;
3.using System.ComponentModel;
4.using System.Data;
5.using System.Drawing;
6.using System.Text;
7.using System.Windows.Forms;
8.using System.Data.Sql;
9.using System.Data.SqlClient;
10.namespace excelform
11.{
12. public partial class Form1 : Form 13. {
14. public Form1() 15. {
16. InitializeComponent();
17. }
18.
19. /// <summary> 20. /// 将DataGridView控件中数据导出到Excel 21. /// </summary> 22. /// <param name="gridView">DataGridView对象</param> 23. /// <param name="isShowExcle">是否显示Excel界面</param> 24. /// <returns></returns> 25. public bool ExportDataGridview(DataGridView gridView, bool isShowExcle) 26. {
27. if (gridView.Rows.Count == 0) 28. return false; 29. //建立Excel对象 30. Excel.Application excel = new Excel.Application(); 31. excel.Application.Workbooks.Add(true); 32. excel.Visible = isShowExcle;
33. //生成字段名称 34. for (int i = 0; i < gridView.ColumnCount; i++) 35. {
36. excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText; 37. }
38. //填充数据 39. for (int i = 0; i < gridView.RowCount - 1; i++) //循环行 40. {
41.
42. for (int j = 0; j < gridView.ColumnCount; j++) //循环列 43. {
44. if (gridView[j, i].ValueType == typeof(string)) //判断DataGirdView中数据的类型 45. {
46. excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString(); 47. }
48. else 49. {
50. excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString(); 51. }
52. }
53. }
54. return true; 55. }
2.using System.Collections.Generic;
3.using System.ComponentModel;
4.using System.Data;
5.using System.Drawing;
6.using System.Text;
7.using System.Windows.Forms;
8.using System.Data.Sql;
9.using System.Data.SqlClient;
10.namespace excelform
11.{
12. public partial class Form1 : Form 13. {
14. public Form1() 15. {
16. InitializeComponent();
17. }
18.
19. /// <summary> 20. /// 将DataGridView控件中数据导出到Excel 21. /// </summary> 22. /// <param name="gridView">DataGridView对象</param> 23. /// <param name="isShowExcle">是否显示Excel界面</param> 24. /// <returns></returns> 25. public bool ExportDataGridview(DataGridView gridView, bool isShowExcle) 26. {
27. if (gridView.Rows.Count == 0) 28. return false; 29. //建立Excel对象 30. Excel.Application excel = new Excel.Application(); 31. excel.Application.Workbooks.Add(true); 32. excel.Visible = isShowExcle;
33. //生成字段名称 34. for (int i = 0; i < gridView.ColumnCount; i++) 35. {
36. excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText; 37. }
38. //填充数据 39. for (int i = 0; i < gridView.RowCount - 1; i++) //循环行 40. {
41.
42. for (int j = 0; j < gridView.ColumnCount; j++) //循环列 43. {
44. if (gridView[j, i].ValueType == typeof(string)) //判断DataGirdView中数据的类型 45. {
46. excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString(); 47. }
48. else 49. {
50. excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString(); 51. }
52. }
53. }
54. return true; 55. }
资源评论
- zb2382502014-10-10挺好的,帮助很大!
SmilingKevin
- 粉丝: 9
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功