Results 1 to 4 of 4

Thread: [RESOLVED] Using Excel Template question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    484

    Resolved [RESOLVED] Using Excel Template question

    Dear all,

    I would like to know how can I use the excel template, and then save as a new worksheet?

    Under VB.Net......I am sort of know how to program and export my data to excel, but I don't know how to use the template, can anyone help?

    Thanks

    PlayKid

  2. #2
    Addicted Member
    Join Date
    May 2002
    Posts
    142

    Re: Using Excel Template question

    Here is some code I found. There is a part where you can open an excel file. I would suggest opening the templated excel worksheet or workbook and then do your adjustments and then sanve it as something else.

    VB Code:
    1. Module Module1
    2.     Const SourceExcelFileName = "C:\vbnet\source.xls"
    3.  
    4.     Sub Main()
    5.         On Error Resume Next
    6.         Dim oExcel As Excel.ApplicationClass
    7.         Dim oBook As Excel.WorkbookClass
    8.         Dim oBooks As Excel.Workbooks
    9.  
    10.         'Start Excel and open the workbook.
    11.         oExcel = CreateObject("Excel.Application")
    12.         oExcel.Visible = True
    13.         oBooks = oExcel.Workbooks
    14.         oBook = oBooks.Open(SourceExcelFileName)
    15.        ' oBook.SaveAs("C:\FolderName\SaveFilename.XML",Excel.XlFileFormat.xlXMLSpreadsheet)
    16.     oBook.SaveAs("C:\vbnet\SaveFilename.xls")
    17.         'Clean-up: Close the workbook and quit Excel.
    18.         oBook.Close(False)
    19.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
    20.         oBook = Nothing
    21.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
    22.         oBooks = Nothing
    23.         oExcel.Quit()
    24.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
    25.         oExcel = Nothing
    26.         Exit Sub
    27.     End Sub
    28. End Module

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Using Excel Template question

    Here's some code from the constructor of a class I wrote that looks for a template, and opens a workbook based on it when found.

    VB Code:
    1. 'Look for the template file.
    2.             If mTemplate = "" Then
    3.                 xlapp.FileSearch.FileName = "RunReportTemplate.xlt"
    4.                 xlapp.FileSearch.LookIn = "C:\"
    5.                 xlapp.FileSearch.SearchSubFolders = True
    6.                 xlapp.FileSearch.Execute()
    7.                 mTemplate = xlapp.FileSearch.FoundFiles.Item(1)
    8.                 mTemplatePath = System.IO.Path.GetDirectoryName(mTemplate) & "\"
    9.             End If
    10.  
    11.  
    12.             'Create a new doc based on the template.
    13.             MyBase.xlBook = MyBase.xlApp.Workbooks.Add(mTemplate)
    14.             MyBase.ImOpen = True

    mTemplate is the path of the template. The class is derived from another class I wrote that opens an Excel Workbook and does a few other things. That's why there are calls to the xlApp object in MyBase.

    I posted this, because the filesearch feature is pretty cool.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    484

    Re: Using Excel Template question

    I used the one that is similar to post 2....it's quite good thou.....
    and fast......

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