Outputting to Excel

A

almurph

Hi everyone,


I'm trying to output some numbers to an Excel file. the first number
in A1 the second in A2, you know like this:

1
2
3
4
etc...

Anyway, can anyone give me a few pointers as how to do it. I have
imported the Excell dll and can create an object of it like:

Dim oExcel As Excel.Application

but after that I start to get confused. Can you help me please? Any
suggestions/comment/code-samples would be most appreciated.

Thank,
Al.
 
M

Mythran

Hi everyone,


I'm trying to output some numbers to an Excel file. the first number
in A1 the second in A2, you know like this:

1
2
3
4
etc...

Anyway, can anyone give me a few pointers as how to do it. I have
imported the Excell dll and can create an object of it like:

Dim oExcel As Excel.Application

but after that I start to get confused. Can you help me please? Any
suggestions/comment/code-samples would be most appreciated.

Thank,
Al.

Here is an example I threw together for ya :)

Private Sub WriteToNewWorkBook(ByVal WorkbookPath As String)
Dim app As Excel.Application = New Excel.Application()

' Create a new workbook to write to.
Dim wb As Excel.Workbook = app.Workbooks.Add()

Try

' Get a reference to the active worksheet.
Dim ws As Excel.Worksheet = _
DirectCast(wb.ActiveSheet, Excel.Worksheet)

' Write the values to the first cell in the first 10 rows.
For i As Integer = 1 To 10
' Get a reference to the first cell in each row.
Dim cell As Excel.Range = _
DirectCast(ws.Cells.Item(i, 1), Excel.Range)

' Write the value to the cell.
cell.Value = i
Next i

' Save the workbook file. Make sure the file doesn't exist first!
If System.IO.File.Exists(WorkbookPath)
Throw New Exception( _
"The specified workbook path already exists." _
)
End If

wb.SaveAs(WorkbookPath)
Finally
' Close the workbook file.
wb.Close(False)

' Close the Excel application.
app.Quit()

WriteLn("HERE")
End Try
End Sub

HTH,
Mythran
 
R

rowe_newsgroups

Hi everyone,

I'm trying to output some numbers to an Excel file. the first number
in A1 the second in A2, you know like this:

1
2
3
4
etc...

Anyway, can anyone give me a few pointers as how to do it. I have
imported the Excell dll and can create an object of it like:

Dim oExcel As Excel.Application

but after that I start to get confused. Can you help me please? Any
suggestions/comment/code-samples would be most appreciated.

Thank,
Al.

Have you searched in the following two places? There is a lot of
samples for this type of thing:

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/topics?lnk=gschg&safe=on
www.google.com

After searching, if you run into a specific problem and you can't find
an answer for it, post back and I or someone else will take a look and
try to help you out.

Thanks,

Seth Rowe [MVP]
 
B

Bill Voorhees

The most easy way to do that is to create a text file named numbers.csv and
writeline each number. Excel will read the file.
 
R

rowe_newsgroups

The most easy way to do that is to create a text file named numbers.csv and
writeline each number. Excel will read the file.

But you lose the formatting functionality, which though not specified
in the original post, most likely is a requirement or will soon be
added as a requirement. I would rather do the Office Interop now and
save the time during maintenance.

Thanks,

Seth Rowe [MVP]
 

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