C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Easy way for exporting large volume of data ( not formatted data ) to excel using VB .Net


Posted Date: 15 Jan 2007    Resource Type: Articles    Category: .NET Framework
Author: Subramanian V .BMember Level: Bronze    
Rating: 1 out of 5Points: 10



Introduction



Using the below code you can export text data ( not formatted data ) to excel.

The Code





Imports System.Data.SqlClient
Imports System.IO
Public Class Export
Public Sub ExportToExcel(ByVal FileName As String, ByVal SavePath As String, ByVal objDataReader As DataTable)
Dim i As Integer
Dim sb As New System.Text.StringBuilder
Try
Dim intColumn, intColumnValue As Integer
Dim row As DataRow
For intColumn = 0 To objDataReader.Columns.Count - 1
sb.Append(objDataReader.Columns(intColumn).ColumnName)
If intColumnValue <> objDataReader.Columns.Count - 1 Then
sb.Append(vbTab)
End If
Next
sb.Append(vbCrLf)
For Each row In objDataReader.Rows
For intColumnValue = 0 To objDataReader.Columns.Count - 1
sb.Append(StrConv(IIf(IsDBNull(row.Item(intColumnValue)), "", row.Item(intColumnValue)), VbStrConv.ProperCase))
If intColumnValue <> objDataReader.Columns.Count - 1 Then
sb.Append(vbTab)
End If
Next
sb.Append(vbCrLf)
Next
SaveExcel(SavePath & "\" & FileName & ".xls", sb)
Catch ex As Exception
Throw
Finally
objDataReader = Nothing
sb = Nothing
End Try
End Sub

Private Sub SaveExcel(ByVal fpath As String, ByVal sb As System.Text.StringBuilder)
Dim fsFile As New FileStream(fpath, FileMode.Create, FileAccess.Write)
Dim strWriter As New StreamWriter(fsFile)
Try
With strWriter
.BaseStream.Seek(0, SeekOrigin.End)
.WriteLine(sb)
.Close()
End With
Catch e As Exception
Throw
Finally
sb = Nothing
strWriter = Nothing
fsFile = Nothing
End Try
End Sub
End Class







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Way  .  Large volume of data  .  Exporting  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Properties: Old wine in new bottle.
Previous Resource: Project for Beginers: File Handling Utility
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use