Pass Values from VB.Net application to Excel Template and back to the application?

I am in need of assistance in passing values from a VB.Net application to an Excel spreadsheet template and than be able to pass it back to the program for editing.  

Here's an overview of the process and what I want to accomplish.  I want to create a vb.net program that has the similar form or template as the Excel spreadsheet template, when a person creates a new form and save it will insert the values from the program to the Excel template.  If they open an existing form (Using conditions to determine if it's a new or existing form), the program will pull the value from the Excel spreadsheet and populate the application for editing.  I think that if they hit "New" to create a new form, it will default to the template otherwise it will use the existing Excel spreadsheet for this.

Summary:

1)  How would I pass value from VB.Net application to an existing Excel Template?  Please provide example of opening the Template and than passing from VB.Net application to the template.  For example txtFName will be pass to cell A1, txtLName to cell B1, etc....

2)  If the user opens an existing excel spreadsheet, how would I read the excel spreadsheet and pass it to the application for editing?  For example the spreadsheet A1 has value "Jon", how would I pass that to textbox txtFName?
holemaniaAsked:
Who is Participating?
 
KudzullcCommented:
Hi holemania,

For reference try this Question
I got the method exporting a DataSet / DataTable to Excel file from:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20744146.html

========================================================
Imports Excel
Imports System.Runtime.InteropServices

Public Class ExportExcel
    Sub Export_To_Excel(ByVal dtSource As System.Data.DataTable, ByVal sFileName As String)

        Dim iRowCount As Integer = dtSource.Rows.Count
        Dim iColCount As Integer = dtSource.Columns.Count
        Dim oData(iRowCount, iColCount) As Object

        Dim iRow As Integer, iCol As Integer
        For iRow = 0 To iRowCount - 1
            For iCol = 0 To iColCount - 1
                oData(iRow, iCol) = dtSource.Rows(iRow).Item(iCol)
            Next
        Next

        ' Start Excel and get Application object
        Dim oExcel As Excel.Application = New Excel.Application()
        'oExcel.Visible = True  ' Make visible

        ' Get a new workbook
        Dim oBook As Excel._Workbook = CType(oExcel.Workbooks.Add(Missing.Value), Excel._Workbook)
        Dim oSheet As Excel._Worksheet = CType(oBook.ActiveSheet, Excel._Worksheet)

        Dim oRange As Excel.Range = oSheet.Range("A1")
        oRange = oRange.Resize(iRowCount, iColCount)
        oRange.Value = oData

        oSheet.SaveAs(sFileName)
        oExcel.Workbooks.Close()
        oExcel.Quit()

        oBook = Nothing
        oSheet = Nothing
        oExcel = Nothing
    End Sub
End Class


Part 2 of your question is to pay attention to this part
  Dim iRow As Integer, iCol As Integer
        For iRow = 0 To iRowCount - 1
            For iCol = 0 To iColCount - 1
                oData(iRow, iCol) = dtSource.Rows(iRow).Item(iCol)
            Next
        Next

can be modified dramatically to get this...
                   txtFName.text  = dtSource.Rows(A).Item(1)

Hope this helps.  I have NOT tried this code so good luck. It should put you off in right direction.

Best Regards,
Lucas
   
       
0
 
holemaniaAuthor Commented:
Sorry for getting back so late.  I was on Vacation and just got back.

It is working, but can you give me an example of opening a template?  For example we have an existing Excel spreadsheet that I want to pass the value to each cell.
0
 
KudzullcCommented:
holemania,
Do you mean opening a template fromVB?  I dont know if you can do that but I may be wrong.  Which really only means i have never done it before.  

gl,
Lucas
0
 
holemaniaAuthor Commented:
I figured it out.  I meant opening a Excel template.  See I want to use an existing template and fill that template with values from the VB application.  I switched to VB and not VB.Net because we were having some issues with trusting the assembles so it seems to work out with your example.  Just need to tweak it a little.

Thanks.
0
 
KudzullcCommented:
Great!

Thanks for the points.

gl,
Lucas
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.