[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
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:
Module Module1
Const SourceExcelFileName = "C:\vbnet\source.xls"
Sub Main()
On Error Resume Next
Dim oExcel As Excel.ApplicationClass
Dim oBook As Excel.WorkbookClass
Dim oBooks As Excel.Workbooks
'Start Excel and open the workbook.
oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oBooks = oExcel.Workbooks
oBook = oBooks.Open(SourceExcelFileName)
' oBook.SaveAs("C:\FolderName\SaveFilename.XML",Excel.XlFileFormat.xlXMLSpreadsheet)
oBook.SaveAs("C:\vbnet\SaveFilename.xls")
'Clean-up: Close the workbook and quit Excel.
oBook.Close(False)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
oBook = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
oBooks = Nothing
oExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
oExcel = Nothing
Exit Sub
End Sub
End Module
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:
'Look for the template file.
If mTemplate = "" Then
xlapp.FileSearch.FileName = "RunReportTemplate.xlt"
xlapp.FileSearch.LookIn = "C:\"
xlapp.FileSearch.SearchSubFolders = True
xlapp.FileSearch.Execute()
mTemplate = xlapp.FileSearch.FoundFiles.Item(1)
mTemplatePath = System.IO.Path.GetDirectoryName(mTemplate) & "\"
End If
'Create a new doc based on the template.
MyBase.xlBook = MyBase.xlApp.Workbooks.Add(mTemplate)
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.
Re: Using Excel Template question
I used the one that is similar to post 2....it's quite good thou.....
and fast......