Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Answer. My solution won't allow ASP.NET to access office objects :((

Our environment...


Now I'm back to square 1. trying to get the grid results to load into Excel. Do you have a suggestion on how I could send the results to a data set?



I don't understand why I'm getting the message "Expression is a value and therefore cannot be the target of an assignment" when I try to assign the grid results to an excel sheet.

VB
Private Sub ibtnExcel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnExcel.Click
        imgSearch.Visible = False
        imgClear.Visible = False
        imgAdd.Visible = False
        imgApply.Visible = False
        ibtnExcel.Visible = False
        ibtnVersion.Visible = False
        ibtnHelp.Visible = False

        _strstyleSheet = ""


        Dim i, j As Integer
        Dim xlApp As Excel.Application
        Dim xlSht As Excel.Worksheet
        Dim rng As Excel.Range

        xlApp = New Excel._ExcelApplication
        xlApp.DisplayAlerts = False

        xlApp.Workbooks.Add()

        xlSht = CType(xlApp.Sheets(1), Excel.Worksheet)
        xlSht.Name = "BDIS-Download"

        Dim dc As DataColumn
        Dim dr As DataRow


        For i = 0 To grdResult.Columns.Count - 1
            xlSht.Cells(i + 1).value = grdResult.Columns(i)      
        Next

        With xlSht
            rng = CType(.Columns(1), Excel.Range)
            rng.AutoFit()
        End With

        xlApp.ActiveWorkbook.Close(True, "C:\BDISTest.xls")
        xlApp.Quit()

        ReleaseComObject(rng)
        ReleaseComObject(xlSht)
        ReleaseComObject(xlApp)

    End Sub
Posted
Updated 1-Feb-10 9:00am
v2

Well, did you google the error message?

http://msdn.microsoft.com/en-us/library/76435b93%28VS.80%29.aspx[^]
 
Share this answer
 
The examples[^] that I've seen refer to "value2", making your code read;

xlSht.Cells(i + 1).Value2 = grdResult.Columns(i)
 
Share this answer
 
Maybe you need to specify xlSht.Cells(i+1).Value or something like that (and you may even need to cast it from an object to a specific type).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900