Export excel file to sql server with asp.vb.net

S

sanju

hi guyz .. can anyone send sample code to export a excel file into sql
server table using web interface .
ie programatically read and select some columns of excel and insert into a
table.
please give me an idea on this

thank u very much
san
auckland
 
Joined
Nov 11, 2008
Messages
1
Reaction score
0
There is much simpler way

'add this to page load and call this page from any button on any .net form

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sFile As String
sFile = Server.MapPath(Request.ApplicationPath) & "\files\MyExcel.xls"
Dim fs As New FileStream(sFile, FileMode.Create, FileAccess.Write)
Dim s As New StreamWriter(fs)
Dim sql As String = "SELECT * FROM dbo.tbl_course_students"
Me.dsAccounts.SelectCommand = Sql 'you need to define ds accounts as a sql table on the form where the page loads
Dim thisconnection As New SqlConnection("add your connection string here")
Dim thisCommand As New SqlCommand _
(sql, _
thisConnection)


' Open Connection
thisconnection.Open()

Dim br As String
br = "<BR>"
Dim td As String
td = "<TD>"
s.WriteLine(td & "my excel spreadsheet" & br)
' Execute Query
s.WriteLine("<center><TABLE>")
s.WriteLine(td & "First name" & td & "Last name" & td & "Email" & td & "Phone" & "<TR>")
Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
Dim statusx As String

While thisReader.Read()
s.WriteLine("<TD>" & thisReader(1) & "<Td>" & thisReader(2) & "<TD>" & thisReader(3) & td & thisReader(4) & "<tr>")

End While
s.WriteLine("</table>")

s.Close()
Response.Redirect("/portal/files/MyExcel.xls")
 
Last edited:

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top