Why do it the hard way? If it is a datagrid with Data source XPath you can right click add button and select Export to Excell. When the users uses search to filter the records only the filtered items will be exported.
Or ami I missing something here on why you do it the hard way?
Regards,
Ronald