The optimal attitude control of an underactuated spacecraft is investigated in this paper. The flywheels of the spacecraft can somehow only provide control inputs in two independent directions. The dynamic equations are formulated for the spacecraft under a nonholonomic constraint resulting from the constant time-rate of the total angular momentum of the system. The reorientation of such underactuated spacecraft is transformed into an optimal control problem. A genetic algorithm is proposed to derive the control laws of the two flywheels angle velocity inputs. The control laws are approximated by the discrete orthogonal wavelets. The numerical simulations indicate that the genetic algorithm with the wavelet approximation is an effective approach to deal with the optimal reorientation of underactuated spacecraft.