C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Full Code to create a Excel report wihtout using Excel object model -Example


Posted Date: 26 Aug 2008    Resource Type: Articles    Category: .NET Framework

Posted By: D.Jeya kumar(JK)       Member Level: Diamond
Rating:     Points: 20



To create Excel or Word document file without Excel object model or word object model in vb.net

Step 1:
Add the below code in form load for testing the Sample code


'Added a new table with column and rows for demo purpose
'Create a datatable you wish to export to excel.
'Here sample Datatable has been created.

Dim Dt As New System.Data.DataTable
Dt.Columns.Add("Eno")
Dt.Columns.Add("EmpName")
Dt.Columns.Add("Salary")
Dim Dr As DataRow
Dr = Dt.NewRow()
Dr("Eno") = "1000000"
Dr("EmpName") = "JK"
Dr("Salary") = "190809"
Dt.Rows.Add(Dr)

Dr = Dt.NewRow()
Dr("Eno") = "1239834"
Dr("EmpName") = "Kumar"
Dr("Salary") = "234234444"
Dt.Rows.Add(Dr)

Dr = Dt.NewRow()
Dr("Eno") = "23476234"
Dr("EmpName") = "Rajes"
Dr("Salary") = "12312333"
Dt.Rows.Add(Dr)



Step 2:
Call the function to export the data to excel:
Parameters:
1st Argument: File Name
2nd Argument: File Path
3rd Argument: DataTable to be exported


'To call the function to create the excel report with filename , path and A datatable to create the excel file
ExportToExcelfile("text", "C:\", Dt)


Step 3:
Add this function to your code:
To append the values to a string builder with Vbtab for new cell and vbcrlf for new line pass the values to create excel sheet.

'To create the values with VBTab for New cell and VBcrlf for New line and append it in a string builder
'Pass the values to the Excel report to generate it

Public Sub ExportToExcelfile(ByVal FileName As String, ByVal SavePath As String, ByVal objDataReader As System.Data.DataTable)
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
SaveExcelfile(SavePath & "\" & FileName & ".xls", sb)
Catch ex As Exception
Throw
Finally
objDataReader = Nothing
sb = Nothing
End Try
End Sub



Step 4:
Add the following function to the code:

To create the Excel file using Stream writer

'Using the stream writer we can write the file with xls as extension or doc etc.,
'and finally disposing the objects
Private Sub SaveExcelfile(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





Attachments

  • To create a Excel report wihtout Excel object model (20501-26850-To create Excel without Excel object model in vb.doc)



  • Responses


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

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    To create Excel report  .  To create a Excel report wihtout using Excel object model  .  Export to Excel  .  Datatable to excel  .  ASP.NET Page to excel  .  

    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: Crystalreports
    Previous Resource: To create a Excel sheet with Formating from CSV File wihtout Excel object model.
    Return to Discussion Resource Index
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design

    Help Desk

    Contact Us    Privacy Policy    Terms Of Use