Fix bug when generating CSV