The energy calibration and resolution of the electromagnetic calorimeter (ECAL) of the CMS detector have been determined using proton-proton collision data from LHC operation in 2010 and 2011 at a centre-of-mass energy of √s = 7 TeV with integrated luminosities of about 5\fbinv. Crucial aspects of detector operation, such as the environmental stability, alignment, and synchronization, are presented. The in-situ calibration procedures are discussed in detail and include the maintenance of the calibration in the challenging radiation environment inside the CMS detector. The energy resolution for electrons from Z-boson decays is better than 2% in the central region of the ECAL barrel (for pseudorapidity |η| < 0.8) and is 2–5% elsewhere. The derived energy resolution for photons from 125 GeV Higgs boson decays varies across the barrel from 1.1% to 2.6% and from 2.2% to 5% in the endcaps. The calibration of the absolute energy is determined from Z→e+e− decays to a precision of 0.4% in the barrel and 0.8% in the endcaps.