Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an Excel sheet in VB.NET 1

Status
Not open for further replies.

depistol

Programmer
Sep 16, 2005
2
CA
Hello,

I have a question. I want to create an Excel file in a VB.NET program and store it on the C drive. I dont want the spreadsheet to open just to be created according to the path. I also need to be able to update the file later, again without opening the spreadsheet. Anyone have any ideas?

Thank you,
Dep
 
Here is an excerpt of the relevant lines from a vb.net project I am working on. The code prepares the excel sheet just fine, provided the applicable variables are entered in your vb.net application.

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles Button1.Click
' Declare Excel object variables and create types
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlChart As Excel.Chart
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)

' Insert Data
xlSheet.Cells(1, 2) = Val(TextBox1.Text)
xlSheet.Cells(2, 2) = Val(TextBox2.Text)
xlSheet.Cells(3, 2) = Val(TextBox3.Text)
xlSheet.Cells(4, 2) = Val(TextBox4.Text)
xlSheet.Cells(5, 2) = Val(TextBox5.Text)
xlSheet.Cells(6, 2) = Val(TextBox6.Text)
xlSheet.Cells(7, 2) = Val(TextBox7.Text)
xlSheet.Cells(8, 2) = Val(TextBox8.Text)
xlSheet.Cells(9, 2) = Val(TextBox9.Text)
xlSheet.Cells(10, 2) = Val(TextBox10.Text)

' Saves the sheet with Client Name and today's date
Dim ThisName As String
Dim mm, dd, yy, Middle As String
Middle = lblClock.Text
dd = Middle.Substring(0, 2)
mm = Middle.Substring(3, 2)
yy = Middle.Substring(6, 2)
ThisName = "c:\programming\PATHClientData\" & cboClientName.Text & "_" & dd & "-" & mm & "-" & yy & ".xls"
xlBook.SaveAs(FileName:=ThisName)
 
What reference do you set to get the Excel stuff? Thanks!

Have a great day!

j2consulting@yahoo.com
 
A COM reference to Excel needs to be added. Also, don't forget this line:
Code:
    xlApp.Quit()

Have a great day!

j2consulting@yahoo.com
 
There appears to be a bug in .Net. Using the above code, the process is still running. One of the guys I work with has researched the really ugly code you have to run to take care of it. I'll try to post it when I get a chance.

Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top