In one of my projects, I faced a requirement to create a custom list and upload around 5000 user records from an excel sheet. I used the  "Import SpreadSheet" option to import the user records from excel sheet  into a custom list. But for all the records the "Created By" and "Modified By" column was showing the "System Account" while the requirement was that both the column must have the name of user to which the record belongs. Luckily there was a column in the list which contains login ID of the users. Then I written following code which loops through the list items and for each item it reads the user's login id and update the same in the "Created By" and "Modified By" column.

private void UpdateCreatedByAndModifiedByFieldData(string strSiteUrl, string strListName, string strUserFieldName)
{
  try
  {
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
      using (SPSite site = new SPSite(strSiteUrl))
      {
        using (SPWeb web = site.OpenWeb())
        {
          SPList list = web.Lists[strListName];

          foreach (SPListItem listItem in list.Items)
          {
            //Read user id from a list column of user type and create SPUser object
            SPUser user = web.EnsureUser(listItem[strUserFieldName].ToString().Trim());

            if (user != null)
            {
              string userValue = user.ID + ";#" + user.Name;
 
              //Assign the above user to the "Created By" column
              listItem["Author"] = userValue;

              //Assign the above user to the "Modified By" column
              listItem["Editor"] = userValue;
 
              //Call the update method to apply the above changes
              listItem.Update();
              web.Update();
            }
          }
        }
      }
    });
  }
  catch (Exception ex)
  {
    throw ex;
  }
}