码迷,mamicode.com
首页 > Web开发 > 详细

asp.net读取Excel数据

时间:2014-10-10 00:21:53      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   java   文件   

先通过控件FileUpload获取excel文件路径

     protected void btnReadExcelFromFileUpload_Click(object sender, EventArgs e)
        {
            if (fupExcel.PostedFile.ContentLength > 0)
            {
                //获取全路径
                string fullFileName = fupExcel.PostedFile.FileName.ToString();
                //获取文件名
                string fileName = fupExcel.FileName.ToString();
                //获取文件类型
                string type = fileName.Substring(fileName.LastIndexOf(".") + 1);
                //获取上传路径
                string path = Server.MapPath("UploadFile\\")+fileName;
                Response.Write(path);
                //文件大小
                FileInfo file = new FileInfo(fullFileName);
                float size=(file.Length/1024)/1024;
                if (type != "xls")
                {
                    Response.Write("<script language=‘javascript‘>alert(‘不是Excel文件!‘)</script>");
                }
                else
                {
                 //   fupExcel.PostedFile.SaveAs(path);//上传到服务器
                    DataSet ds = ImportExcel(fullFileName);
                    gvFirst.DataSource = ds;
                    gvFirst.DataBind();
                }

            }
        }


读取excel文件里的数据到DataSet里

    /// <summary>
        /// 读取Excel数据到DataSet
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public DataSet ImportExcel(string filename)
        {
            //文件路径
            string ExcelName = filename;
            //HDR=YES  有两个值:YES/NO,表示第一行是否字段名,默认是YES,第一行是字段名
            /* 
             IMEX有三个值0,1,2,其他两个值分别表示什么
             当 IMEX=0 时为“汇出模式”,这个模式开启的 Excel 档案只能用来做“写入”用途。
             当 IMEX=1 时为“汇入模式”,这个模式开启的 Excel 档案只能用来做“读取”用途。
             当 IMEX=2 时为“连結模式”,这个模式开启的 Excel 档案可同时支援“读取”与“写入”用途。
            详见:http://www.cnblogs.com/goto/archive/2012/04/12/2443670.html
              */
            string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelName + ";Extended Properties=‘Excel 8.0;HDR=No;IMEX=1‘;";//连接excel文件的字符串
            if (ExcelName == null)
            {
                return null;
            }
            OleDbConnection odcon = new OleDbConnection(strcon);//建立连接
            odcon.Open();//打开连接
            System.Data.DataTable sTable = odcon.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
            //Sheets Name
            string tableName = sTable.Rows[0][2].ToString().Trim();
            if (tableName == "")
            {
                return null;
            }
            else
            {
                tableName = "[" + tableName + "]";
            }
            OleDbDataAdapter odda = new OleDbDataAdapter("select * from " + tableName, odcon);
            DataSet ds = new DataSet();
            try
            {
                odda.Fill(ds);
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<script language=‘javascript‘ defer>alert(‘"+ex.Message+"‘);</script>");
            }
            return ds;
        }

 

asp.net读取Excel数据

标签:style   blog   http   color   io   os   ar   java   文件   

原文地址:http://www.cnblogs.com/lijianhong90/p/4014627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!