0

我正在开发一个网站,我想以批量方式注册学童,因为他们将提供 excel 表,并希望当我上传该表时,它会自动在 userinfo 表中注册用户

这是代码

    if (Request.Files["FileUpload1"] != null && Request.Files["FileUpload1"].ContentLength > 0)
    {
        string extension = System.IO.Path.GetExtension(Request.Files["FileUpload1"].FileName);
        string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/UploadedFolder"), Request.Files["FileUpload1"].FileName);
        if (System.IO.File.Exists(path1))
            System.IO.File.Delete(path1);

        Request.Files["FileUpload1"].SaveAs(path1);
        string sqlConnectionString = @"Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Planetskool-20130901224446;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Planetskool-20130901224446.mdf;Database=DefaultConnection; Trusted_Connection=true;Persist Security Info=True";


        //Create connection string to Excel work book
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path1 + ";Extended Properties=Excel 12.0;Persist Security Info=False";
        //Create Connection to Excel work book
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        //Create OleDbCommand to fetch data from Excel
        OleDbCommand cmd = new OleDbCommand("Select [UserInfoID],[UserID],[GraphID],[UserLevelEnumId],[Title],[FirstName],[MiddleName],[LastName],[Birthdate],[Gender],[Email],[MobileNo],[Country],[Zipcode],[CountFollowers],[CountFollows],[CountFiles],[CountPhotos],[Quote],[AvatarURL],[isVerified],[VerificationCount],[UserEnumType],[UserCreatorId] from [Sheet1$]", excelConnection);
         excelConnection.Open();
        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();

        SqlBulkCopy sqlBulk = new SqlBulkCopy(sqlConnectionString);
        //Give your Destination table name
        sqlBulk.DestinationTableName = "UserInfo";
        sqlBulk.WriteToServer(dReader);
        excelConnection.Close();

        // SQL Server Connection String


    }

    return RedirectToAction("Import");
4

1 回答 1

1

你的代码如下

    if (Request.Files["FileUpload1"].ContentLength > 0)
            {

                string fileExtension = System.IO.Path.GetExtension(Request.Files["FileUpload1"].FileName);

                if (fileExtension == ".xls" || fileExtension == ".xlsx")
                {
                    // Create a folder in App_Data named ExcelFiles because you need to save the file temporarily location and getting data from there. 
                    string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/UploadedFolder"), Request.Files["FileUpload1"].FileName);
                    if (System.IO.File.Exists(path1))
                        System.IO.File.Delete(path1);
                    Request.Files["FileUpload1"].SaveAs(path1);
                    string sqlConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path1 + ";Extended Properties=Excel 12.0;Persist Security Info=False";
                    //Create Connection to Excel work book and add oledb namespace
                    OleDbConnection excelConnection = new OleDbConnection(sqlConnectionString);
                    excelConnection.Open();
                    DataTable dt = new DataTable();
                    dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    if (dt == null)
                    {
                        return null;
                    }

                    String[] excelSheets = new String[dt.Rows.Count];
                    int t = 0;
                    //excel data saves in temp file here.
                    foreach (DataRow row in dt.Rows)
                    {
                        excelSheets[t] = row["TABLE_NAME"].ToString();
                        Debug.Write("SheetTitle = " + excelSheets[t]);
                        t++;
                    }
                    OleDbConnection excelConnection1 = new OleDbConnection(sqlConnectionString);
                    DataSet ds = new DataSet();

                    string query = string.Format("Select * from [{0}]", excelSheets[0]);
                    using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                    {
                        dataAdapter.Fill(ds);
                    }



                    for (int j = 0; j <= ds.Tables[0].Rows.Count - 1; j++)
                    {
}
}
于 2015-03-17T09:17:26.750 回答