Reading/Writing to Excel Spreadsheet

hubbard92

Member
Joined
Dec 12, 2006
Messages
11
Programming Experience
Beginner
Hello,

I'm trying to figure out how to read and write to an excel spreadsheet. I found a prevoius post and had the following example to read an excel spreadsheet. Have found other examples also, which all have had the following statement (XXXX = NEW Excel.Application).

I have referenced com excel 11.0 object library and imported it. When I run these few lines (changing the path of course), I receive the following error, " 'New' cannot be used on an interface". Not sure at all what this means. But each example I've seen uses the XXXX as new excel.application.

So, two questions.

1) How do I get rid of the " 'New' cannot be used on an interface" error.

2) Can someone please give me an example of how to write to an excel spreadsheet without using .xml and without using a dataset. All I want to do is find the date in column A, and then write three more fields to the corresponding row.

Thanks in advance,

Dim xlsApp As Excel.Application
Dim xlsWB As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
Dim xlsCell As Excel.Range
Dim xlsDatei As String

xlsApp = New Excel.Application
xlsApp.Visible = True
xlsWB = xlsApp.Workbooks.Open(Path)
xlsSheet = xlsWB.Worksheets(1)
xlsCell = xlsSheet.Range("A1")
 
Maybe I'm asking for too much here. Can anyone help me out with just the first question. When I use the statement
AAA =
New Excel.Application - I receive the error "New cannot be used on an interface"

I see this statement in almost all the treads I've read that are reading Excel. Any help would still be very much appreciated.

Thanks
 
Here is code I have used...

VB.NET:
        ' Declare Excel object variables and create types
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
        xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
        xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)

        ' Insert data
        xlSheet.Cells(1, 2) = 5000
        xlSheet.Cells(2, 2) = 75
        xlSheet.Cells(3, 1) = "Total"
        ' Insert a Sum formula in cell B3
        xlSheet.Range("B3").Formula = "=Sum(R1C2:R2C2)"
        ' Format cell B3 with bold
        xlSheet.Range("B3").Font.Bold = True
        ' Display the sheet
        xlSheet.Application.Visible = True
 
Back
Top