Detecting and calculating accuracy of centroid of light spot is an important factor which affects the detecting accuracy of Hartmann wavefront sensor. The influence of window selection on centroid detection error is great. The Prieto's method of progressively reducing window size is analysed, and the template matching method is adopted according to the feature of the light spots of Hartmann sensor. With template matching, the accuracy of centroid detection is improved. The computer simulation of the centroid calculations of spot array shows that the centroid's rms error can be decreased by 53.7%. In addition, the template matching method avoides iterative centroid calculations and reduces computer time consumption.