六合开奖结果现场直播欢迎诸位!收藏本站注 册登 陆
常用三肖中特马:基础知识网络知识操作系统WPS网页制作数据库算法网页成功之路网络安全最新技术古典mid流行midmid背景下载中心
您现在的位置:首页 > 三肖中特马 > 文章页

Net读取(上传的)Excel内容显示到GridView示例源代码

Admin | 2008-1-21 8:18:58 | ReadNums | 10701 | 标签 asp.net | 打印本页
     

网上转载……

//上传文件
            string filename = FileUpload1.FileName;
            string path = Server.MapPath("c:\\123\\" + filename);
            FileUpload1.SaveAs(path);
        

            string strConn = GetExcelConnString(path);
            OleDbConnection conn = new OleDbConnection(strConn);
            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                //ShowClientJS("ShowInfo(\"错误:" + ex.Message + "\",1,5000);", this);
                return;
            }

//判断工作表个数
            DataTable dtTblNames = conn.GetSchema("Tables");
            if (dtTblNames.Rows.Count > 1)
            {
                conn.Close();
                //ShowClientJS("ShowInfo('导入失败:只允许1个工作表,当前有:" + dtTblNames.Rows.Count.ToString() + "',1,5000);", this);
                return;
            }
//只处理第一个工作表
            string tblName = dtTblNames.Rows[0]["Table_Name"].ToString();
            hidTableName.Value = tblName;
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select * from [" + tblName + "]";
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                //ShowClientJS("ShowInfo(\"预览时发生错误:" + ex.Message + "\",1,5000);", this);
            }
            finally
            {
                conn.Close();
            }
//显示数据
            GridView1.DataSource = dt;
            GridView1.DataBind();


问题未解决:在线咨询

网友评论

(访客)
内容实用原创,讲得很好。
20xx年x月x日
(站长)
有问题请在线咨询。
20xx年x月x日