Results 1 to 2 of 2

Thread: Reading from Excel

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question Reading from Excel

    How do I read from an Excel file? How should the code be compared to the code of reading from Access?
    Code:
    Protected Sub bindKomponenter()
            Dim strCn As String = "Provider=Microsoft.jet.oledb.4.0;"
            strCn += "Data Source=" & Server.MapPath("priser04.mdb") & ";"
            Dim cn As OleDbConnection = New OleDbConnection(strCn)
            Dim cmdText As String = "Select komponenter.Id as kompId, komponenter.Komponent FROM komponenter,materiale_komponenter WHERE materiale_komponenter.kompId=komponenter.id AND materiale_komponenter.MatId=" & intMatNr
            Dim cmd As OleDbCommand = New OleDbCommand(cmdText, cn)
            cmd.Connection.Open()
            komp1.DataSource = cmd.ExecuteReader()
            komp1.DataValueField = "kompId"
            komp1.DataTextField = "Komponent"
            komp1.DataBind()
            cmd.Connection.Close()
            cmd.Connection.Open()
            komp2.DataSource = cmd.ExecuteReader()
            komp2.DataValueField = "kompId"
            komp2.DataTextField = "Komponent"
            komp2.DataBind()
            cmd.Connection.Close()
        End Sub

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    I have this code for ASP 3.0. Can anyone help me making it work in ASP.Net?
    Code:
    <%
    'Name of the excel file being displayed
    exceldb="test.xls"
    
    ' Create a server connection object
    Set cn = Server.CreateObject("ADODB.Connection")
    cn.Open "DBQ=" & Server.MapPath(exceldb) & ";" & _
    "DRIVER={Microsoft Excel Driver (*.xls)};"
    
    
    ' Create a server recordset object
    Set rs = Server.CreateObject("ADODB.Recordset")
    sql="select * from hpl"
    ' Execute the sql
    rs.Open sql, cn
    dim hpl1 
    hpl1= rs("Price")
    Response.Write(hpl1)
    
    Response.Write("<table border=1 align=center>")
    
    For counter = 0 To rs.fields.count - 1
    	Response.Write("<th>")
    	' Write out the field names 
    	response.write rs.fields.item(counter).name
    	Response.Write("</th>")
    	' Move to the next field
    next 
    
    	' Move to the first record
    	rs.movefirst
    	'Write out the record set
    do while not rs.eof 
    	response.write("<tr>") 
    	' Loop through all of the fileds
    	for counter = 0 to rs.fields.count - 1
    		Response.Write("<td align=right>")
    		' Write out the field values 
    		response.write rs.fields.item(counter).value
    		Response.Write("</td>")
    		' Move to the next field
    	next 
    	Response.Write("</tr>")
    	' Move to the next record
    rs.movenext
    loop
    
    Response.Write("</table>")
    
    ' Kill the recordset
    rs.close
    Set rs = nothing
    ' Kill the connection
    cn.close
    Set cn = nothing
    %>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width