求用上传控件把把EXECL导入到GridView的代码,要详细,确实好用的,急~谢~

解决方案 »

  1.   


    private const string strConn = "自己写连接字符串";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataToGridView();
            }
        }    /// <summary>
        /// 绑定
        /// </summary>
        private void BindDataToGridView()
        {
            SqlConnection conn = new SqlConnection(strConn);
            string strSQL = "select * from [User]";
            SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "[User]");
            this.GridView1.DataSource = ds;
            this.GridView1.DataKeyNames = new string[] { "UserId" };
            this.GridView1.DataBind();
        }    /// <summary>
        /// 导出DataSet到Execl
        /// </summary>
        private void OutExecl()
        {
            Excel.Application myExcel = new Excel.Application();
            myExcel.Visible = true;        if (myExcel == null)
            {
                Page.RegisterStartupScript("", "<script>alert('EXCEL无法启动');</script>");
            }        Workbook work = myExcel.Application.Workbooks.Add(Type.Missing);
            Worksheet sheet = (Worksheet)work.Worksheets[1];        int rowCount = 0; 
            int columnCount = 0;         columnCount = this.GridView1.Columns.Count; 
            rowCount = this.GridView1.Rows.Count;          rowCount--;        for (int m = 1; m < columnCount; m++)   
            {
                sheet.Cells[1, m] = this.GridView1.Columns[m].HeaderText;//得到列标题文本
            }        for (int i = 0; i <= rowCount; i++)  //二维表填充从每一行开始
            {
                for (int j = 1; j < columnCount; j++)      //填充每一行第j列单元格,循环添加行列数据
                {
                    sheet.Cells[i + 2, j] = this.GridView1.Rows[i].Cells[j].Text; 
                }
            }
            Page.RegisterStartupScript("", "<script>alert('成功导出!');</script>");
        }    /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOut_Click(object sender, EventArgs e)
        {
            OutExecl();
        }    /// <summary>
        /// 导入Excel到DataSet
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private DataSet InExecl(string filePath)
        {
            DataSet ds = new DataSet();
            string connStr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";        OleDbConnection myConn = new OleDbConnection(connStr);
            string strSQL = " SELECT * FROM [Sheet1$]";
            myConn.Open();
            OleDbDataAdapter myCommand = new OleDbDataAdapter(strSQL, myConn);
            DataSet myDataSet = new DataSet();
            myCommand.Fill(myDataSet, "[Sheet1$]");
            myConn.Close();        return myDataSet;
        }
        
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnIn_Click(object sender, EventArgs e)
        {
            DataSet ds = InExecl(this.File1.PostedFile.FileName);
            this.GridView2.DataSource = ds;
            this.GridView2.DataBind();
        }
    }这个是导入到dataset里然后你绑定到GridView就行
      

  2.   

     private static DataSet NewMethod(string ExcelName, string Sheet) 
            { 
                DataSet dsE = new DataSet(); 
                string MyConnectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @System.Web.HttpContext.Current.Server.MapPath("ExcelTemp") + @"\" + ExcelName+ ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'"; 
                string str = "SELECT * FROM [" + Sheet + "$]"; 
                OleDbConnection myconn = new OleDbConnection(MyConnectionstring); 
                myconn.Open(); 
                OleDbDataAdapter adp = new OleDbDataAdapter(str, myconn); 
                adp.Fill(dsExcel, "ExcelTemp");             myconn.Close(); 
                return dsE; 
            } 
    参考
    http://www.cnblogs.com/shiningrise/archive/2007/05/18/751391.html
      

  3.   

     不好意思,忘了写不通过数据库了,直接导入GridVieW,忘了,不好意思~~
      

  4.   


    //上传代码
    //fileUpload是FileUpload控件
    protected void btnUpload_Click(object sender, EventArgs e) 

            //判断是否上传了文件 
            if (fileUpload.HasFile) 
            { 
                //指定上传文件在服务器上的保存路径 
                string savePath = Server.MapPath("~/upload/"); 
                //检查服务器上是否存在这个物理路径,如果不存在则创建 
                if (!System.IO.Directory.Exists(savePath)) 
                { 
                    System.IO.Directory.CreateDirectory(savePath); 
                } 
                string fileName = savePath + "\\" + fileUpload.FileName; 
                fileUpload.SaveAs(fileName);//保存文件 
                //不过需要注意的是,在客户端访问却需要指定的是URL地址,而不是在服务器上的物理地址 
                GridView1.DataSource = BindData(savePath,fileName,'sheet1');
               GridView1.DataBind(); 
            } 
    } //excelPath 路径
    //excelName 文件名
    //sheet excel表名
    private static DataView BindData(string excelPath, string excelName, string sheet) 
            { 
                DataSet ds = new DataSet(); 
                string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @System.Web.HttpContext.Current.Server.MapPath(excelPath) + @"\" + excelName+ ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'"; 
                string str = "SELECT * FROM [" + sheet + "$]"; 
                OleDbConnection myconn = new OleDbConnection(connectionstring); 
                myconn.Open(); 
                OleDbDataAdapter adp = new OleDbDataAdapter(str, myconn); 
                adp.Fill(ds, sheet); 
                myconn.Close(); 
                return ds.Table[0].DefaultView; 
            } 
      

  5.   

    以前记得见过直接以Excel作为数据源的方法,找找看,如果找到就给楼主贴出来
      

  6.   

    protected void btnUpload_Click(object sender, EventArgs e) 

            //判断是否上传了文件 
            if (fileUpload.HasFile) 
            { 
                //指定上传文件在服务器上的保存路径 
                string savePath = Server.MapPath("~/upload/"); 
                //检查服务器上是否存在这个物理路径,如果不存在则创建 
                if (!System.IO.Directory.Exists(savePath)) 
                { 
                    System.IO.Directory.CreateDirectory(savePath); 
                } 
                string fileName = savePath + "\\" + fileUpload.FileName; 
                fileUpload.SaveAs(fileName);//保存文件 
                //不过需要注意的是,在客户端访问却需要指定的是URL地址,而不是在服务器上的物理地址 
                GridView1.DataSource = BindData(savePath,fileName,'sheet1');
               GridView1.DataBind(); 
            } 
    } //excelPath 路径
    //excelName 文件名
    //sheet excel表名
    private static DataView BindData(string excelPath, string excelName, string sheet) 
            { 
                DataSet ds = new DataSet(); 
                string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @System.Web.HttpContext.Current.Server.MapPath(excelPath) + @"\" + excelName+ ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'"; 
                string str = "SELECT * FROM [" + sheet + "$]"; 
                OleDbConnection myconn = new OleDbConnection(connectionstring); 
                myconn.Open(); 
                OleDbDataAdapter adp = new OleDbDataAdapter(str, myconn); 
                adp.Fill(ds, sheet); 
                myconn.Close(); 
                return ds.Table[0].DefaultView; 
            } 
      

  7.   


    (this.File1.PostedFile.FileName);file1??是什么意思 是控件 还是需要引用的额?????