Excel 数据导入--服务器

来源:互联网 发布:淘宝懒人软件 编辑:程序博客网 时间:2024/05/22 11:45

         /// <summary>
        /// 把Excel 文件放在服务器上
        /// </summary>
        private string UpExcel()
        {
            string fullFileName = FileUpExcel.PostedFile.FileName;  //文件的全路径
            FileInfo nnn = new FileInfo(fullFileName);
            string filename = nnn.Name;//fullFileName.Substring(fullFileName.LastIndexOf("//") + 1);  文件名
            FileUpExcel.PostedFile.SaveAs("D://CMMI QMS" + filename); //服务器上的地址
            return "D://CMMI QMS" + filename;
        }

        /// <summary>
        /// 读取Excel文档
        /// </summary>
        /// <param name="Path">文件名称</param>
        /// <returns>返回一个数据集</returns>
        private DataSet ExcelToDS(string Path)
        {

            DataSet ds = null;
            if (Path.Trim() == "")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "aaaa", "<script   language=javascript>alert('Please import the Excel file!');</script>");
            }
            else
            {
                try
                {
                    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + UpExcel() + ";" + "Extended Properties=Excel 8.0;";
                    OleDbConnection conn = new OleDbConnection(strConn);
                    conn.Open();
                    string strExcel = "";
                    OleDbDataAdapter myCommand = null;
                    strExcel = "select * from [RiskInfo$]";
                    myCommand = new OleDbDataAdapter(strExcel, strConn);
                    ds = new DataSet();
                    myCommand.Fill(ds, "Risk");
                    conn.Close();
                }
                catch
                {

                    Page.ClientScript.RegisterStartupScript(this.GetType(), "aaaa", "<script   language=javascript>alert('The data failed to load! May be the imported Excel file does not meet the system requirements, please re-upload.');</script>");
                }

            }
            return ds;

        }

原创粉丝点击