?
Solved

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

Posted on 2006-03-29
5
Medium Priority
?
400 Views
Last Modified: 2011-10-03
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?
0
Comment
Question by:holemania
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
Kudzullc earned 500 total points
ID: 16322919
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
 

Author Comment

by:holemania
ID: 16382906
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
 
LVL 3

Expert Comment

by:Kudzullc
ID: 16382976
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
 

Author Comment

by:holemania
ID: 16384339
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
 
LVL 3

Expert Comment

by:Kudzullc
ID: 16384489
Great!

Thanks for the points.

gl,
Lucas
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question