Help with creating one excel file with multiple workbooks from multiple excel files using VB.NET

Hi,

If I have the following three excel files: file1.xls, fil2.xls and file3.xls, how do I create file4.xls with three workbooks containing data
from file1.xls, file2.xls and file3.xls?, how do I name the workbooks same names as the filenames?

Thanks,

Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
is it possible for you to think of using a component like http://www.aspose.com/.net/excel-component.aspx ?
0
Éric MoreauSenior .Net ConsultantCommented:
I am also aware (but never used) of this free library: http://excelpackage.codeplex.com/
0
louisfrCommented:
This code collects the first sheets from several workbooks into a new workbook.
    Sub Merge(result As String, ParamArray sources As String())
        Dim excel = New Microsoft.Office.Interop.Excel.Application
        Dim resultBook = excel.Workbooks.Add
        For Each source In sources
            Dim sourceBook = excel.Workbooks.Open(source)
            Dim sheet As Worksheet = sourceBook.Worksheets(1)
            sheet.Copy(resultBook.Worksheets(1))
            resultBook.Worksheets(1).Name = sourceBook.Name
            sourceBook.Close()
        Next
        resultBook.SaveAs(result, XlFileFormat.xlExcel8)
        resultBook.Close()
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

vcharlesAuthor Commented:
Hi,

Thanks for the comments. Regarding the last comment, how do you include the three files in the loop to create one excel file?

Victor
0
louisfrCommented:
You call that method with the result file name as the result parameter and the three files as the sources parameter:
Merge("C:\MyPath\file4.xls", "C:\MyPath\file1.xls", "C:\MyPath\file2.xls", "C:\MyPath\file3.xls")

Open in new window

0
vcharlesAuthor Commented:
Hi,

What do I need to import in my project to use the code from the last post. The code I showing errors.

Thanks,

Victor
0
louisfrCommented:
You need a reference to Microsoft.Office.Interop.Excel
0
vcharlesAuthor Commented:
Thank You.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.