Private Static Stream exportdatatabletoexcel (datatable sourcetable, String Sheetname) {hssfworkbook workbook = New Hssfworkbook (); memorystream MS = New Memorystream (); isheet Sheet = Workbook. createsheet (sheetname); irow headerrow = Sheet. createrow ( 0 ); // Handling header. Foreach (Datacolumn Column In Sourcetable. columns) headerrow. createcell (column. ordinal). setcellvalue (column. columnname ); // Handling Value. Int Rowindex = 1 ; Foreach (Datarow row In Sourcetable. Rows) {irow datarow = Sheet. createrow (rowindex ); Foreach (Datacolumn Column In Sourcetable. columns) {datarow. createcell (column. ordinal). setcellvalue (row [column]. tostring ();} rowindex ++ ;} Workbook. Write (MS); Ms. Flush (); Ms. Position = 0 ; Sheet = Null ; Headerrow = Null ; Workbook = Null ; Return MS ;} /// <Summary> /// Export EXCEL from datatable /// </Summary> /// <Param name = "sourcetable"> Datatable to export data </Param> /// <Param name = "FILENAME"> Excel worksheet name </Param> /// <Returns> Excel worksheet </Returns> Public Static Void Exportdatatabletoexcel (datatable sourcetable, String Filename,String Sheetname) {memorystream MS = Exportdatatabletoexcel (sourcetable, sheetname) As Memorystream; httpcontext. Current. response. contentencoding = System. Text. encoding. getencoding ( " UTF-8 " ); Httpcontext. Current. response. appendheader ( " Content-Disposition " , " Attachment; filename = " + Filename); httpcontext. Current. response. binarywrite (Ms. toarray (); httpcontext. Current. response. End (); Ms. Close (); MS = Null ;}