Excel range object into VB.net

L

Larry Jones

I want to place information from an active Excel spreadsheet to a VB.net
form. I just want to place numbers from an Excel cell in a standard
VB.net text box. Does anyone have sample code to demonstrate this
process? I have tried coding the process myself without success. I have
just purchased a 2003 version of VB.net and I am not sure if it is me or
changes in the program. I would like a working example to check my
setup.

Thanks,

Larry Jones
 
S

scorpion53061

Please forgive my inability to decipher your intent but it is not clear
to me what you are trying to do.

You want to put the contents of one cell or an entire worksheet in a
textbox?
 
R

Rob Nicholson

I want to place information from an active Excel spreadsheet to a VB.net
form. I just want to place numbers from an Excel cell in a standard
VB.net text box. Does anyone have sample code to demonstrate this
process? I have tried coding the process myself without success. I have
just purchased a 2003 version of VB.net and I am not sure if it is me or
changes in the program. I would like a working example to check my
setup.

Can we assume that Excel is also installed on the server? If so, then the
way to
approach this is to use the Excel object library to open the Excel file and
parse it. There's a bit of steep learning curve with Excel but I've done a
lot of Excel VBA work so can help out. This page_load event handler does
something like you're after:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ExcelApp As New Excel.Application
Dim Workbook As Excel.Workbook =
ExcelApp.Workbooks.Open("c:\scratch\book1.xls")
Dim Worksheet As Excel.Worksheet = Workbook.Sheets(1)
TextBox1.Text = Worksheet.Range("A1").Value
Workbook.Close(False)
ExcelApp.Quit()
End Sub

This does the following:

o Creates an Excel application object
o Opens the c:\scratch\book1.xls
o Finds the first sheet
o Copies the value from cell A1 into the textbox

Along the right lines?

Some issues:

o You'll need Excel installed and therefore a license
o You'll get access denied when you try and creat the Excel app unless the
IIS accounts have the correct access
o How you get the book1.xls uploaded to the server I leave as an exercise
for the reader.

Cheers, Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top