We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,642 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to create an excel file template via vb.net?

Hi,

I have a program that needs to create a downloadable excel template with the following headers on the first row named, name, age, gender. And I also want the template to only have 1 sheet on it.
how can I create a sample template using vb.net code?

Thanks

2
Contributors
6
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
jbutardo
Junior Poster in Training
74 posts since Jan 2012
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0

Since I see you have some experience generating an Excel file from VB.NET, I'll just post the "meat"

Dim wb As Workbook = excel.Workbooks.Add(Type.Missing)
      Dim ws As Worksheet = wb.Worksheets(1)

      Dim rowCurrent As Range = ws.Rows(1, Missing.Value)

      CType(rowCurrent.Cells(1, 1), Range).Value2 = "Name"
      CType(rowCurrent.Cells(1, 2), Range).Value2 = "Age"
      CType(rowCurrent.Cells(1, 3), Range).Value2 = "Gender"

      ' brutal (there is probably a better way...)
      While (wb.Worksheets.Count > 1)
         wb.Worksheets(wb.Worksheets.Count).Delete()
      End While
      ' store the template where templates go.
      Dim strAppDataDir As String = Environment.GetEnvironmentVariable("APPDATA")
      Dim strTemplateDir As String = strAppDataDir + "\Microsoft\Templates\Templ1.xlt"

      wb.SaveAs(strTemplateDir, XlFileFormat.xlTemplate)
      ' be sure to close the workbook and Quit out of Excel
thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

Since I see you have some experience generating an Excel file from VB.NET, I'll just post the "meat"

Dim wb As Workbook = excel.Workbooks.Add(Type.Missing)
      Dim ws As Worksheet = wb.Worksheets(1)

      Dim rowCurrent As Range = ws.Rows(1, Missing.Value)

      CType(rowCurrent.Cells(1, 1), Range).Value2 = "Name"
      CType(rowCurrent.Cells(1, 2), Range).Value2 = "Age"
      CType(rowCurrent.Cells(1, 3), Range).Value2 = "Gender"

      ' brutal (there is probably a better way...)
      While (wb.Worksheets.Count > 1)
         wb.Worksheets(wb.Worksheets.Count).Delete()
      End While
      ' store the template where templates go.
      Dim strAppDataDir As String = Environment.GetEnvironmentVariable("APPDATA")
      Dim strTemplateDir As String = strAppDataDir + "\Microsoft\Templates\Templ1.xlt"

      wb.SaveAs(strTemplateDir, XlFileFormat.xlTemplate)
      ' be sure to close the workbook and Quit out of Excel

okay, I have this new Idea, think it'll be better,
I have this stored template in the server, and I think that i'll just import this excel template file in the datatable and then use the datatable for the template of the excel templates, I just don't know how can I use the datatable as my template, can you please help me,

Thanks

jbutardo
Junior Poster in Training
74 posts since Jan 2012
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0

Why would you out it in a database?
Why not a file server or ftp server?

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

Why would you out it in a database?
Why not a file server or ftp server?

can you advise me on how to use fileserver of ftpserver?

jbutardo
Junior Poster in Training
74 posts since Jan 2012
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0

How is this template going to be used by you or the customer?
Will they click on a link and it is delivered to them?
Will they just browse to a file location and get it?
Will they set their templates directory (in Excel) to include these types of templates?

That will dictate where it's best to put it.

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

How is this template going to be used by you or the customer?
Will they click on a link and it is delivered to them?
Will they just browse to a file location and get it?
Will they set their templates directory (in Excel) to include these types of templates?

That will dictate where it's best to put it.

anyways, I have made a solution for my problem..
using an excel file in my server, i have used the response.write code to use the downloadable template in my server,,

thanks for the post by the way..

jbutardo
Junior Poster in Training
74 posts since Jan 2012
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by thines01

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0741 seconds using 2.66MB