Spectral reconstruction method based on narrow-band measurements has been demonstrated to achieve ultrafast spectroscopic imaging with high spatial and spectral resolution, in which multiple narrow-band images are collected by using several specific filters. Although commercially available filters can be employed in such method, filters with complex transmittance that are difficult to be fabricated typically show significant improvement in spectral reconstruction accuracy. In this study, a two dimensional programmable optical filter based on digital micromirror device (DMD) is proposed, in which its transmittance spectrum can be arbitrarily and quickly switched to realize complex transmittance. Furthermore, its flexible transmittance enables directly hardware-based spectral data post-processing, which can perform data acquisition and analysis simultaneously. Those have been evaluated by the diffuse reflectance spectra from normal and occluded skin flaps, as well as Raman spectra from live, apoptosis and necrosis leukemia cells. Our simulation results show that much higher spectral reconstruction accuracies can be achieved by the optimized filters with complex transmittance. Furthermore, the classification accuracy by using the proposed method is comparable to those achieved by conventional numerical methods. Therefore, based on the proposed programmable optical filter, fast spectroscopic imaging with high spatial and spectral resolution can be achieved for observing fast changing phenomena and even real-time target identification.