Export a datatable to excel and save as file
Dim Currentci As System. Globalization. cultureinfo = system. Threading. thread. currentthread. currentculture
System. Threading. thread. currentthread. currentculture = New System. Globalization. cultureinfo ( " En-US " )
If (tmpdatatable is nothing ) then
return
end If
dim rownum as integer = tmpdatatable. rows. count
dim columnnum as integer = tmpdatatable. columns. count
dim rowindex as integer = 1
dim columnindex as integer = 0
dim xlapp as excel. application = New applicationclass ()
xlapp. defaultfilepath = ""
xlapp. displayalerts = true
xlapp. sheetsinnewworkbook = 1
dim xlbook as workbook = xlapp. workbooks. add ( true )
' import the column name of datatable into the first row of the Excel table
dim DC as datacolumn
for Each DC in tmpdatatable. columns
columnindex = columnindex + 1
xlapp. cells (rowindex, columnindex) = dc. columnname
next
'Import data from datatable to excel
DimIAs Integer=0
ForI =0 ToRownum-1
Rowindex = rowindex + 1
Columnindex = 0
Dim J As Integer = 0
For J = 0 To Columnnum- 1
Columnindex = columnindex + 1
Xlapp. cells (rowindex, columnindex) = convert. tostring (tmpdatatable. Rows (I) (j ))
Next
Next
'Xlbook. savecopyas (httputility. urldecode (strfilename, system. Text. encoding. utf8 ));
Xlbook. savecopyas (strfilename)
End