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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Excel file and add multiple worksheets

Status
Not open for further replies.

malaygal

IS-IT--Management
Feb 22, 2006
192
0
0
US
I have a sub that creates an excel file and populate it with records from a datatable.
The event that calls this sub will loop thru multiple datatable and create different worksheet in the same excel file, without saving the file.

Does anybody know how I can code this? My current code creates new excel file for each of the datatable.
 
BTW, the way I want to implement this is for the event to loop thru datatables.
The first pass will call the sub, passing the datatable, and create the excel file and populate the first worksheet,
the second pass will just add the 2nd worksheet, populate the sheet and so forth and so on.
 
Hi,

Please post the code that your referred to.

However, generally speaking, set a flag once you have created workbook.
[pre]
If Flag Then
Add a sheet object to workbook object
Else
Add a workbook object to application object
Flag = TRUE
End
[/pre]
Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'loop here thru datatables

for each dt in datatables - pseudo

createXLReport(dt)

next




end sub



private sub createXLReport(ByVal contTable As DataTable)

Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()

If xlApp Is Nothing Then
MessageBox.Show("Excel is not properly installed!!")
Exit Sub
End If

Dim xlWorkBook As New Excel.Workbook
xlWorkBook = xlApp.Workbooks.Add()
Dim xlWorkSheet As New Excel.Worksheet


'If contTable IsNot Nothing AndAlso contTable.Rows.Count > 0 Then Need to add spreadsheet regardless
Dim i As Integer = 1
xlWorkSheet = CType(xlApp.Worksheets.Add(, , i), Excel.Worksheet)
xlApp.Visible = True
xlWorkSheet = xlWorkBook.Worksheets(i)
xlWorkSheet.Activate()
xlWorkBook.Sheets(i).Name() = contTable.Name

'populate cells here

end sub
 
Maybe something like this. HOWEVER, the Flag and i (Worksheet counter) must be declared as Public/Global variables OR pass them as arguments in your procedure, as you are calling a private procedure...

ALSO, the application object and the workbook object must be a public object variable that persists after the createXLReport procedure ends.

Code:
Private Sub createXLReport(ByVal contTable As DataTable)

    Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
    
    If xlApp Is Nothing Then
    MessageBox.Show ("Excel is not properly installed!!")
    Exit Sub
    End If
    
    If Flag Then
        i = i + 1
    Else
        Flag = True
        Dim xlWorkBook As New Excel.Workbook
        xlWorkBook = xlApp.Workbooks.Add()
        'If contTable IsNot Nothing AndAlso contTable.Rows.Count > 0 Then Need to add spreadsheet regardless
        i = 1
    
    End If
    
    Dim xlWorkSheet As New Excel.Worksheet
    xlWorkSheet = CType(xlApp.Worksheets.Add(, , i), Excel.Worksheet)
    xlApp.Visible = True
    xlWorkSheet = xlWorkBook.Worksheets(i)
'.....
End Sub

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Thanks for the reply. I have tried that before with a flag (I used _newxl as an indicator for the first datatable).
But once I Dim the xlWorkBook inside an if condition, I get xlWorkBook not declared, and any for other var inside the condition
 
“But once I Dim the xlWorkBook inside an if...”

That’s why the xlWorkBook must be declared as a public variable. It can be created inside an If condition, but it must be declared elsewhere.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top