Results 1 to 3 of 3

Thread: Use Excel Template to Create Excel File in VS - Questions

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Austin, TX
    Posts
    120

    Use Excel Template to Create Excel File in VS - Questions

    Hi,
    Below is what I am wanting to do. I am wondering if it is easy to do and how I should get started. Any info will be greatly appreciated!

    1) Take an existing Excel template file (or an existing Excel Workbook)
    2) Add/Change some of the cells' values, but none of the formatting
    3) Create an excel file (workbook or worksheet) from this

    It seems like it wouldn't be too big of a task, but what I have found in this forum and other help sites has been kind of confusing.

    I creating a Windows application, using VS 2005, CF2, and am coding in VB.

    Any advice, particularly on how to get started will be greatly appreciated! (ie. what references I need, what objects, etc. I should be working with, etc.)

    Thank you,
    Corey

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Austin, TX
    Posts
    120

    Re: Use Excel Template to Create Excel File in VS - Questions

    Also, another very important question I have is: does the computer running my application have to have excel installed for my program to work?
    Thanks!
    -Corey

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Austin, TX
    Posts
    120

    Re: Use Excel Template to Create Excel File in VS - Questions

    Well, I partially answered my question...

    First, you have to download the office interop files:


    Then add a com reference to the Microsoft Excel 11.0 Object Library

    Then add Imports Microsoft.Office.Interop

    The following code will open an excel file, Book1.xls, change cell A1 to "YAY!", save it as Book2.xls and close excel:

    Code:
    Dim oExcel As Excel.Application = New Excel.Application
    Dim oBook As Excel.Workbook = oExcel.Workbooks.Open("C:\Book1.xls")
    Dim oSheet As Excel.Worksheet = oBook.Worksheets.Item(1)
    
    'change a value in the sheet        
    oSheet.Range("A1").Value = "YAY!"
    
    'Save the Workbook and quit Excel.
    oBook.SaveAs("C:\" & "Book2.xls")
    oSheet = Nothing
    oBook = Nothing
    oExcel.Quit()
    oExcel = Nothing
    GC.Collect()
    MsgBox("done")

    I'm still wondering if the user has to have excel installed on his or her computer. My guess is no...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width