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 » Code Snippets » File Operations »

multiple sheets in one excel file + vb.net


Posted Date: 09 Oct 2009    Resource Type: Code Snippets    Category: File Operations
Author: Amol H. KagdeMember Level: Gold    
Rating: 1 out of 5Points: 7



multiple sheets in one excel file + vb.net

note: import excel dll


Public Sub ExcelReport(ByVal ds As DataSet)
Dim k, i, j, l As Integer
Me.Cursor = Cursors.WaitCursor
Dim Xlapp As Excel.Application
Xlapp = Nothing
Xlapp = New Excel.Application
Dim Xlwb As Excel.Workbook
Xlwb = Nothing
Dim xlshOne As Excel.Worksheet
Dim theExcelDataMinus As System.Text.StringBuilder
Xlwb = Xlapp.Workbooks.Add
Dim dt As DataTable
Dim dtr As DataRow
Dim ColCnt, cntCols As Integer
If Xlwb.Worksheets.Count >= 1 Then
For l = 1 To Xlwb.Sheets.Count - 1
xlshOne = Xlwb.Sheets(l)
xlshOne.Delete()
Next
End If
For i = 0 To ds.Tables.Count - 1 Step 1
cntCols = ds.Tables(i).Columns.Count
dt = ds.Tables(i)
k = 0
If ds.Tables(i).Rows.Count <> 0 Then
If ds.Tables(i).Columns.Count = 0 Then
xlshOne = Xlwb.Worksheets.Add
xlshOne.Name = ds.Tables(i).TableName.Trim()
Else
xlshOne = Xlwb.Worksheets.Add
If (i = 0) Then
xlshOne.Name = "GRPS"
ElseIf (i = 1) Then
xlshOne.Name = "AGINGREPORT"
ElseIf (i = 2) Then
xlshOne.Name = "SCHEDULE5"
ElseIf (i = 3) Then
xlshOne.Name = "DIRECT SCHEDULES"
ElseIf (i = 4) Then
xlshOne.Name = "SCHEDULES"
ElseIf (i = 5) Then
xlshOne.Name = "PL"
ElseIf (i = 6) Then
xlshOne.Name = "BS"
End If
theExcelDataMinus = New System.Text.StringBuilder
With xlshOne
'Adding heading to Excel Sheet
'For j = 0 To cntCols - 1
' theExcelDataMinus.Append(dt.Columns(j).Caption)
' theExcelDataMinus.Append(vbTab)
'Next
'theExcelDataMinus.Append(vbCrLf)
For Each dtr In dt.Rows
If ds.Tables(i).Rows.Count > 0 Then
For ColCnt = 0 To dt.Columns.Count - 1
If dtr(ColCnt) Is DBNull.Value Then
theExcelDataMinus.Append("")
Else
theExcelDataMinus.Append(dtr(ColCnt).ToString)
End If
theExcelDataMinus.Append(vbTab)
Next
theExcelDataMinus.Append(vbCrLf)
End If
k = k + 1
Next
Clipboard.SetDataObject(theExcelDataMinus.ToString, False)
.Cells.Font.Size = 10
.Cells.Font.Name = "Times New Roman"
.Range("A1:N1").Font.Bold = True
.Range("A1:AZ1").Font.Bold = True
.Range("A1:AZ1").Select()
.Cells(1, 1).select()
.Paste()
.Range("A1:U1").ShrinkToFit = True
.Range("T:T").NumberFormat = "@"
.Range("U:U").NumberFormat = "@"
.Range("v:v").NumberFormat = "@"
.Range("AD:AD").NumberFormat = "@"
.Columns.AutoFit()
theExcelDataMinus = Nothing
Application.DoEvents()
.Cells(1, 1).select()
.Paste()
.Columns.AutoFit()
Xlapp.Visible = True
End With
End If
End If
Next
Me.Cursor = Cursors.Default
ds = Nothing
End Sub



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.
Excel file operations  .  

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: Load Assembly
Previous Resource: Create and Write into Log file
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use