2013-10-02 62 views
0

我正在開發一個網站,我想以批量方式註冊學校兒童,因爲他們將提供Excel表格,並希望當我上傳該表格時在用戶信息表導入csv和xls用於批量上傳用戶在網站上註冊

這裏自動註冊用戶代碼

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"); 
+1

你的問題是爵士......? – Shyju

+0

@Shyju我想從excel表單或csv文件批量註冊用戶,所以我怎麼能? –

+0

當你運行你的代碼時會發生什麼?你需要什麼幫助? –

回答

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++) 
        { 
} 
} 
相關問題