The electronic and optical properties of O-doped MgF2 are studied by first-principles calculations and experiments. As for the calculations, geometric structures, electronic structures and optical properties of O-doped MgF2 are systematically investigated using the first-principles calculations. The calculation results show that the band gap of MgF2 is narrowed by doping O, but with further increase of O level, the band gap changes a bit. Besides, both the refractive index and the extinction coefficient increase with the raise of O-doping level, and a new absorption peak can be found after doping. In term of experiments, magnetron sputtering deposited films are investigated by X-ray photoelectron spectroscopy and spectroscopic ellipsometer. The experimental results indicate that the MgF2 films with different O-doping levels have similar band gap values, while the film with larger amount of O possesses higher refractive index and extinction coefficient.