求:分页到处exl

解决方案 »

  1.   

            StringBuilder sb = new StringBuilder();
            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.   

    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.        }   
    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.}