Dzwebs.Net

撰写电脑技术杂文十余年

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

Admin | 2008-1-21 8:18:58 | 被阅次数 | 13551

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

网上转载……

//上传文件
            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();


该杂文来自: 网站开发杂文

上一篇:asp.net 编译错误类型“同时存在于”不同的dll中

下一篇:"authentication mode="Windows"/>"之“/”应用程

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计