cyberdeminout
Programmer
Here we will discuss a simple solution to create excel file from asp.net without excel software.
Function CreateExcelFile(ByVal dt As DataTable) As Boolean
Dim bFileCreated As Boolean = False
Dim sTableStart As String = "<HTML><BODY><TABLE Border=1><TR><TH>Header1</TH></TR>"
Dim sTableEnd As String = "</TABLE></BODY></HTML>"
Dim sTableData As String
Dim nRow As Long
For nRow = 0 To dt.Rows.Count - 1
sTableData &= "<TR><TD>" & dt.Rows(nRow).Item(0).ToString & "</TD></TR>"
Next
Dim sTable As String = sTableStart & sTableData & sTableEnd
Dim oExcelFile As System.IO.File
Dim oExcelWrite As System.IO.StreamWriter
sExcelFile = "c:/excelfile.xls"
oExcelWrite = oExcelFile.CreateText(sExcelFile)
oExcelWrite.WriteLine(sTable)
oExcelWrite.Close()
bFileCreated = True
Return bFileCreated
End Function
Rams
Function CreateExcelFile(ByVal dt As DataTable) As Boolean
Dim bFileCreated As Boolean = False
Dim sTableStart As String = "<HTML><BODY><TABLE Border=1><TR><TH>Header1</TH></TR>"
Dim sTableEnd As String = "</TABLE></BODY></HTML>"
Dim sTableData As String
Dim nRow As Long
For nRow = 0 To dt.Rows.Count - 1
sTableData &= "<TR><TD>" & dt.Rows(nRow).Item(0).ToString & "</TD></TR>"
Next
Dim sTable As String = sTableStart & sTableData & sTableEnd
Dim oExcelFile As System.IO.File
Dim oExcelWrite As System.IO.StreamWriter
sExcelFile = "c:/excelfile.xls"
oExcelWrite = oExcelFile.CreateText(sExcelFile)
oExcelWrite.WriteLine(sTable)
oExcelWrite.Close()
bFileCreated = True
Return bFileCreated
End Function
Rams