Export array from vb.net to excel

  • Thread starter Thread starter michealmess
  • Start date Start date
M

michealmess

Can anyone help me. I wish to export an array from vb.net to excel
ranges. This will happen for multiple files. The ranges names will not
be start and end in the same cells on all files. How do i do this?
 
One way to do this is to write a subroutine in your Excel spreadsheet that
takes either an object or variant as a parameter, convert the object to a
string array and input your values from the array into the range that you
wish. I've done this in VB6 but not .NET. It looks like it should work in
..Net as well. Just include a reference to the Microsoft Excel Object library
(whatever version that's on your machine and the target machines). Then do
something like this:

Dim o As Excel.Application
Dim xsheet As Workbook
Dim myArray(5) As String

' is Excel already open?
o = GetObject(, "Excel.Application")

' if not, open it
If o Is Nothing Then
o = CreateObject("Excel.Application")
End If

For i As Integer = 0 To myArray.GetUpperBound(0)
myArray(i) = i.ToString()
Next

xsheet = o.Workbooks.Open(FileName:="somefilenameandpath")
xsheet.Application.Visible = True
' run the subroutine you want
xsheet.Application.Run("RoutineName_FileRange", myArray)

HTH
Steve
 
Hi Micheal

If you're not having any references to Excel then I don't imagine
you're going to be able to do this as ranges and names are Excel
specific so unless you are familiar with the Excel file format and are
comfortable creating files in this format (or you're using a version of
Excel that uses an XML file format) then you're left with the choice of
buying/using an Excel component (Aspose for example) or referencing
Excel.

HTH
Martin
 
Back
Top