求:分页到处exl
解决方案 »
- 如何实现数据库里有新暑假的时候弹出一个小窗口进行提醒?
- zengzhan 您下面的代码什么意思啊,为什么有两个function 最后一个文本框,回车自动触发按钮事件
- 有一个新手的困惑 关于asp
- 本机不报错,放上服务器报错
- 在asp.net中关于sql语句的问题???
- 我想在基类页中使用Page.FindControl(),应该怎么写?
- vs2005中的detailsview控件问题
- 我用Visual Web Developer 2005 Express Edition,如何发布网站?为何Bin文夹是空的?没法Release?
- 无法向会话状态服务器发出会话状态请求。
- 高手注意了:怎么让网页在2分钟内没有动作,自动返回到首页???
- COOKIE保存出错
- 请教浮动菜单设计
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw); Page page = new Page();
HtmlForm form = new HtmlForm(); MyGridView.EnableViewState = false;
page.EnableEventValidation = false;
page.DesignerInitialize();
page.Controls.Add(form);
form.Controls.Add(MyGridView);
page.RenderControl(htw);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName.Trim(), System.Text.Encoding.UTF8).ToString() + ".xls");
Response.Charset = "UTF-8";
Response.ContentEncoding = Encoding.Default;
Response.Write(sb.ToString());
Response.End();
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. }
56. private void button1_Click(object sender, EventArgs e) 57. {
58. this.ExportDataGridview(dataGridView1, true); 59. }
60. private void Form1_Load(object sender, EventArgs e) 61. {
62. SqlConnection con = new SqlConnection("Server=.;uid=sa;pwd=123;DataBase=accp;"); 63. SqlDataAdapter dap = new SqlDataAdapter("select * from userInfo", con); 64. DataSet ds = new DataSet(); 65. dap.Fill(ds);
66. dataGridView1.DataSource = ds.Tables[0].DefaultView; 67. }
68.
69. }
70.}