INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Create Excel file and add multiple worksheets

Create Excel file and add multiple worksheets

(OP)
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.

RE: Create Excel file and add multiple worksheets

(OP)
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.

RE: Create Excel file and add multiple worksheets

Hi,

Please post the code that your referred to.

However, generally speaking, set a flag once you have created workbook.
If Flag Then
   Add a sheet object to workbook object
Else
   Add a workbook object to application object
   Flag = TRUE
End
 
Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Create Excel file and add multiple worksheets

(OP)
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

RE: Create Excel file and add multiple worksheets

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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Create Excel file and add multiple worksheets

(OP)
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

RE: Create Excel file and add multiple worksheets

“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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close