asp.net读取Excel数据

先通过控件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 static DataSet ImportExcel(string filename)
    {
        try
        {
            //文件路径
            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=Yes;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)
            {
                throw ex;
            }
            finally
            {
                odcon.Close();
            }
            return ds;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

 

posted @ 2014-10-09 22:35  巴蒂尔  阅读(685)  评论(0编辑  收藏  举报