locked
Excel sheet contact import RRS feed

  • Question

  • User1519602350 posted

    I am using SQL compact database table called "Contacts" contains firtname & email. My project requiement is user can upload excel sheet using C# razor code and import into contacts table. In excel sheet contains the same column like firstname & lastname. So basically it reads the data from excel ans stroing into the database.

    Guide me how to achive the above.

    Thanks

    Monday, August 5, 2013 9:07 AM

Answers

  • User-821857111 posted

    If your Excel file has an xlsx extension, you need to use the ACE OleDb provider to connect to it and query it. If it has an xls extension, you can use the JET OleDb provider. Note that these providers must be installed on the server. Often, JET is installed as part of the Operating System files, but ACE isn't.

    If you can persuade your users to save the file as csv and then upload that instead, your job will be much easier. Then you can use the simple File API that forms part of .NET:

    var contacts = File.ReadAllLines(Server.MapPath("~/Path_To_File"));
    foreach(var line on contacts){
        var contact = line.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
        db.Execute("INSERT INTO Contacts (FirstName, LastName, Email) VALUES (@0, @1, @2)", contact[0], contact[1], contact[2]);
    } 



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 5, 2013 9:27 AM