Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.NET Tips-Simple way 2 Create Excel 4m DataTable

Status
Not open for further replies.

cyberdeminout

Programmer
Jul 19, 2005
37
US
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
 
If you are going to use that method, you should use a StringBuilder to create the string of data as the above could be very memory intensive.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
good tip.
 
and one more thing. try to use DataReaders as much as possible in such cases as most of the time you are making a forward only movement...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top