473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How To Export Your Displayed Data To Excel

Merlin1857
14 New Member
Its great producing data for users to look at in your web pages and generally that is sufficient for their needs but sometimes you may want to supply your user with the data in a form they can actually do something more with. This code shows you how to display data from your database and then how to give that data to the user in the form of a useable Excel spreadsheet which they can then take away and play with themselves. The way I have shown this done here is to display the data first, using my article 'How To Dynamically Search A Database Table' you could give your users the ability to re-query the data before they produce it. This is the code for the first page called 01viewdata.asp :

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ Language=VBScript %>
  3. <!-- #include file="../incfiles/adovbs.inc" -->
  4. <% 
  5. 'Declare some variables
  6. dim Conn,RS1
  7. '==================CONNECTION CODE TO SQL SERVER=======================
  8. 'Create a connection object to the database
  9. Set Conn = Server.CreateObject("ADODB.Connection")
  10.  
  11. 'Create a recordset object
  12. Set RS1 = Server.CreateObject("ADODB.RecordSet")
  13.  
  14. 'Feed the connection string into a variable called strConn
  15. strConn = "Provider=SQLOLEDB;Data Source=MYSERVERNAME;Initial Catalog=MYDATABASENAME;"&_
  16. "User Id=USERNAME;Password=PASSWORD;"
  17.  
  18. 'Feed the connection string into the connection object
  19. Conn.Open strConn 
  20. '==========================CONNECTION CODE=============================
  21.  
  22. 'Create your sql statement
  23. sql1 = "Select * from tblMYTABLE WHERE ID < 10000" 
  24.  
  25. 'Obtain the data using all the work done above
  26. RS1.Open sql1, Conn 
  27. 'Test to see if we have a recordset coming back from the database and trap the error if there is no data
  28. If RS1.eof Then
  29. %>
  30. <html>
  31. <body>
  32. <table border="0" width="100%" cellspacing="0" cellpadding="2">
  33. <tr>
  34. <td>&nbsp;<font face="Arial" color="#FF0000" size="2">No Records Match Your Search</font></td>
  35. </tr>
  36. </table>
  37. <%
  38. 'If there is data carry on
  39. Else
  40. %>
  41. <!-- 
  42. We will need to fire the data we gather to the next page which produces the Excel sheet so here 
  43. we use a form to do so instead of formulating the sql statement again when we export to excel 
  44. I pass the statement above inside a hidden field called 'InpSQL' below, this makes sure that 
  45. what they see on this page is exactly what they get in Excel.
  46. -->
  47. <form method="POST" action="02extoex.asp">
  48. <table border=0 width="100%" cellspacing="0" cellpadding="2">
  49. <tr>
  50. <td>
  51. <input type="submit" value="Export To Excel Spread Sheet" name="B1" style="font-size: 8pt">&nbsp;
  52. <input type="hidden" name="InpSQL" size="63" value="<%=sql1%>"></td>
  53. </tr>
  54. </table>
  55.  
  56. </form>
  57. <!-- This area is for display to the user and shows how the data to be exported will look -->
  58. <table border="1" width="100%" cellspacing="0" cellpadding="2" bordercolor="#000000">
  59. <tr>
  60. <td bgcolor="#000080"><font face="Arial" size="2" color="#FFFFFF">Field1Header</font></td>
  61. <td bgcolor="#000080"><font face="Arial" size="2" color="#FFFFFF">Field2Header</font></td>
  62. <td bgcolor="#000080"><font face="Arial" size="2" color="#FFFFFF">Field3Header</font></td>
  63. <td bgcolor="#000080"><font face="Arial" size="2" color="#FFFFFF">Field4Header</font></td>
  64. <td bgcolor="#000080"><font face="Arial" size="2" color="#FFFFFF">Field5Header</font></td>
  65. <td bgcolor="#000080"><font face="Arial" size="2" color="#FFFFFF">Field6Header</font></td>
  66. <td bgcolor="#000080"><font face="Arial" size="2" color="#FFFFFF">Field7Header</font></td>
  67. </tr>
  68. <%
  69. 'Whilst there are records to show keep going
  70. Do While Not RS1.eof
  71. %>
  72. <!-- 
  73. Note the use of &nbsp; in each field this makes sure the data looks OK in
  74. a bordered table if the return is null. 
  75. -->
  76. <tr>
  77. <td><font face="Arial" size="2"><%=RS1("Field1Result")%>&nbsp;</font></td>
  78. <td><font face="Arial" size="2"><%=RS1("Field2Result")%>&nbsp;</font></td>
  79. <td><font face="Arial" size="2"><%=RS1("Field3Result")%>&nbsp;</font></td>
  80. <td><font face="Arial" size="2"><%=RS1("Field4Result")%>&nbsp;</font></td>
  81. <td><font face="Arial" size="2"><%=RS1("Field5Result")%>&nbsp;</font></td>
  82. <td><font face="Arial" size="2"><%=RS1("Field6Result")%>&nbsp;</font></td>
  83. <td><font face="Arial" size="2"><%=RS1("Field7Result")%>&nbsp;</font></td>
  84. </tr>
  85. <%
  86. 'Loop through each record and write it to the screen
  87. RS1.Movenext
  88. Loop
  89. End If
  90. %>
  91. </table>
  92. </body>
  93. </html>
  94.  
  95. <%
  96. 'Clean up and close the connections and recordset objects
  97. RS1.Close
  98. Conn.Close
  99.  
  100. Set Conn = Nothing
  101. Set RS1 = Nothing
  102. %>
  103.  
This is the clever bit which exports to excel. You could fire this out straight from a link without the need to display it like we did above if you wanted to which would result in the Excel spreadsheet coming straight into existence. This is the code for a page called 02extoex.asp which the link from the first page produces.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <%@ Language=VBScript %>
  4. <!-- #include file="../incfiles/adovbs.inc" -->
  5. <% 
  6. 'Declare some variables
  7. dim Conn,RS1
  8.  
  9. '==================CONNECTION CODE TO SQL SERVER=======================
  10. 'Create a connection object to the database
  11. Set Conn = Server.CreateObject("ADODB.Connection")
  12.  
  13. 'Create a recordset object
  14. Set RS1 = Server.CreateObject("ADODB.RecordSet")
  15.  
  16. 'Feed the connection string into a variable called strConn
  17. strConn = "Provider=SQLOLEDB;Data Source=MYSERVERNAME;Initial Catalog=MYDATABASENAME;"&_
  18. "User Id=USERNAME;Password=PASSWORD;"
  19.  
  20. 'Feed the connection string into the connection object
  21. Conn.Open strConn 
  22. '==========================CONNECTION CODE=============================
  23.  
  24. 'Obtain the sql statement which we fed into the hidden field in the last page
  25. sql1 = Request("InpSQL")
  26.  
  27. 'If you aren't happy with doing that repeat your sql statement here
  28. 'sql1 = "Select * from tblMYTABLE WHERE ID < 10000"
  29.  
  30. 'The assumption here is that if we saw data in the last page the data exists so there is no need to
  31. 'test again for errors so we just go for the data
  32.  
  33. RS1.Open sql1, Conn 
  34.  
  35. 'This is the the code which tells the page to open Excel and give it the data to display
  36. Response.ContentType = "application/vnd.ms-excel"
  37. 'You can give the spreadsheet a name at the point its produced
  38. Response.AddHeader "Content-Disposition", "attachment; filename=MYSPREADSHEETNAME.xls" 
  39. %>
  40.  
  41. <!-- 
  42. Note that I have formatted the output header here to a dark blue background and white text
  43. this will be reflected in the spreadsheet when its produced and you could extend this to your own tastes of course.
  44. -->
  45.  
  46. <table border="1" width="100%">
  47. <tr>
  48. <td bgcolor="#000080"><font color="#FFFFFF">Field1Header</font></td>
  49. <td bgcolor="#000080"><font color="#FFFFFF">Field2Header</font></td>
  50. <td bgcolor="#000080"><font color="#FFFFFF">Field3Header</font></td>
  51. <td bgcolor="#000080"><font color="#FFFFFF">Field4Header</font></td>
  52. <td bgcolor="#000080"><font color="#FFFFFF">Field5Header</font></td>
  53. <td bgcolor="#000080"><font color="#FFFFFF">Field6Header</font></td>
  54. <td bgcolor="#000080"><font color="#FFFFFF">Field7Header</font></td>
  55. </tr>
  56. <%Do While Not RS1.eof%>
  57. <tr>
  58. <td><%=RS1("Field1Result")%></td>
  59. <td><%=RS1("Field2Result")%></td>
  60. <td><%=RS1("Field3Result")%></td>
  61. <td><%=RS1("Field4Result")%></td>
  62. <td><%=RS1("Field5Result")%></td>
  63. <td><%=RS1("Field6Result")%></td>
  64. <td><%=RS1("Field7Result")%></td>
  65. </tr>
  66. <%
  67. RS1.Movenext
  68. Loop
  69. %>
  70. </table>
  71. <%
  72. RS1.Close
  73. Conn.Close
  74.  
  75. Set Conn = Nothing
  76. Set RS1 = Nothing
  77. %>
  78.  
That's how its done. A practical and easy way to make your site more inter-active.
Sep 12 '07 #1
7 28880
movieking81
52 New Member
Great, maybe you can answer a question for me. How do you default the gridlines in Excel to "on" when you export. This has been driving me nuts. I think you can do it with .NET, but I'm using classic ASP at the moment.

Thanks

Dean
Oct 18 '07 #2
jhardman
3,406 Recognized Expert Specialist
Dean,

It appears that Merlin is just using the HTML equivalent for producing the XLS output. So I would use the HTML border attribute or possibly an appropriate style attribute since those are the closest HTML equivalent to what you are asking. If you try it, let me know if it works.

Jared
Oct 18 '07 #3
movieking81
52 New Member
Yeah, I've used the border attributes in the past, it just bugs me that every time I export data to an Excel sheet the default grid lines are turned off. It's not a big deal, it's just turning the grid lines on is quicker than formatting the exported data with a border. Also some of my users expect grid lines and it's always a 10 min discussion about why they have to turn them on. I don't know why there isn't a function/property for that, maybe...

workbookname.gr idlines = on

How hard would that have been. Something that Excel could read when it opens the document. Sorry, now I'm just ranting.

Thanks

Dean
Oct 19 '07 #4
srkidd12
14 New Member
This example works great for me, but do you know how to have the excel file automatically attach it to an email being sent out by a CDO message?
May 2 '08 #5
DrBunchman
979 Recognized Expert Contributor
Hi srkidd12,

You would do this in a slightly different way - instead of setting a page's content type to Excel you would need to create the excel file on your server, write the content to it and save it. You could then attach that file to an e-mail.

Hope this helps,

Dr B
Jun 11 '08 #6
aashishn86
52 New Member
that was a real helpful article....
just wanted to know , how can i create more than one worksheet in the same workbook ??
Mar 5 '09 #7
rambow4
1 New Member
Please excuse my ignorance, I am very new to ASP and programming, could someone explain what is meant by "FieldResul t" on line 58, I really have no idea what to put there? I am able to open a recordset, run getRows(), store the values to a variable and display the contents, but the rs.("FieldResul t") is unknown to me. could someone provide a better example or explanation.


Example of what I can do

rs.open(sql,con n)
dim values = rs.GetRows()
response.write( values(0,0)

display Result is "Bob"
Apr 6 '15 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
4754
by: Funbeat | last post by:
Hi everybody, I'm facing with the following problem (bug ?) : A page is calling another one (export.aspx) for exporting data to excel. The tecnhique used is to create a Excel-MIME stream for viewing it directly in the browser. (below is a sample code)
1
5012
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table. Before I iterate through the recordset I instruct the browser that the content type is Excel using the following line: (Response.ContentType =...
4
14749
by: Paolo | last post by:
Friends, I need help with some code to export different tables to a single spreadsheet in Excel. My excel file is named REPORT and the spreadsheet is named CLIENTS. I do have the code to export a single table to Excel but have problems with multimple tables. Thanks.
2
2460
by: Bidarkota | last post by:
Hi, I have a DataGrid in which there are some images and Data are displayed and in the webform.asp page i am using some stylesheets. when i export the datagrid all the images are also exported to datagrid and it is giving an alert that following stylesheets are missing when i export it. can anyone help me about this problem. I have seen...
2
9219
by: wubin_98 | last post by:
Hi, I want to export a gridview data and a image from image control to export to an Excel document. When I executed following code, GridView data was exported to Excel properly. But no image was displayed in Excel. Only a blank image icon was show inside Excel. Could any body point out what is wrong in my code or missing anything?
5
2810
by: karthick | last post by:
Hi, I am exporting a Gridview to Excel and it works fine without any issues. But as one of the field holds values such as "71646E100" it gets converted to: "7.16E+104" (like a formula) in Excel. How can I prevent this from happening ?. I need to value to be in Excel as it is displayed in the Gridview (on the web page).
1
9757
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm having I'd be most appreciative. The database is already constructed, I'm just wanting to export the data to an excel file. In short, I'm hoping...
2
5547
by: hal | last post by:
Hello all, I've been searching all day for an article or tutorial on how to get data from a SQL Server 2000 database and export the data to excel 2003 so that multiple worksheets are created, and information is displayed in these worksheets. Data will be coming from multiple tables, and I'm using C# and asp.net 2.0. User will click on an...
2
3991
by: mangalamonkey | last post by:
I want to export data into an excel file using iReports.I could do that but the data is displayed as an image. I need the data to be displayed as cells as in the excel sheet. plz do reply.........
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.