Search code examples
vb.netexcelexport-to-excel

Export with VB to Excel and update file


This is the code that i have to export data to Excel.

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add

oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "ID"
oSheet.Range("B1").Value = " Nome"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = CStr(Request("ID"))
oSheet.Range("B2").Value = "John"

    oBook.SaveAs("C:\Book1.xlsx")
    oExcel.Quit()

I can create and save the excel file, but i can't update the contents. How can i do it?

Thanks.


Solution

  • You're trying to set a Range to a value, I think either you need to set a Range to an array that can contain the value(s) or you need to set a single Cell to a single value.

    This link shows how to do either:

    http://support.microsoft.com/kb/301982