Fixed potential exception when exporting patients as Excel
(copied from 30eb7c218cc641bfa54b0769d92efc7404db630b)