Jump to content

How to Improt Data from Recordset to xls file


raviprakashg

Recommended Posts

Hisomeone tell me how to import data from Recordset(RS) to excel file and how to Retrieve it.i googled about this and found that the RS are save to xls using FSO, is it a better way? or any one have best one then FSO, and i don'nt know what to do for Retrieving data from xls.thanks to everyone, who are going to help me,URSRaviprakash

Link to comment
Share on other sites

Guest acrophobia

Try this a example for exportin datas from db to excel.ASP Source Code: --------------------------------------------------------------------------------<%' Selected constants from adovbs.incConst adOpenStatic = 3Const adLockPessimistic = 2Dim cnnExcelDim rstExcelDim IDim iCols' This is all standard ADO except for the connection string.' You can also use a DSN instead, but so it'll run out of the' box on your machine I'm using the string instead.Set cnnExcel = Server.CreateObject("ADODB.Connection")cnnExcel.Open "DBQ=" & Server.MapPath("xl_data.xls") & ";" & _ "DRIVER={Microsoft Excel Driver (*.xls)};"' Same as any other data source.' FYI: TestData is my named range in the Excel fileSet rstExcel = Server.CreateObject("ADODB.Recordset")rstExcel.Open "SELECT * FROM TestData;", cnnExcel, _ adOpenStatic, adLockPessimistic' Get a count of the fields and subtract one since we start' counting from 0.iCols = rstExcel.Fields.Count%><table border="1"> <thead> <% ' Show the names that are contained in the first row ' of the named range. Make sure you include them in ' your range when you create it. For I = 0 To iCols - 1 Response.Write "<th>" Response.Write rstExcel.Fields.Item(I).Name Response.Write "</th>" & vbCrLf Next 'I %> </thead> <% rstExcel.MoveFirst ' Loop through the data rows showing data in an HTML table. Do While Not rstExcel.EOF Response.Write "<tr>" & vbCrLf For I = 0 To iCols - 1 Response.Write "<td>" Response.Write rstExcel.Fields.Item(I).Value Response.Write "</td>" & vbCrLf Next 'I Response.Write "</tr>" & vbCrLf rstExcel.MoveNext Loop %></table><%rstExcel.CloseSet rstExcel = NothingcnnExcel.CloseSet cnnExcel = Nothing%>This example has been quoted from asp101.com

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...