Does anyone know how to do this? I have tables that are generated from asp that i would like to allow clients to download to a .csv file to their pc.


Recommended Answers

All 5 Replies

if you alter the same script that generate the tables, get rid of the <table> and </table>, get rid of all </tr>, replace all instances of </td><td> with a tab, and replace all instances of <tr> wit ha line break, you have a properly formatted csv file (provided there are no tabs or line breaks in your data). You can then make a button that calls a script that generate the file for download.

Do you know how to create headers that force a download?

Excuse me for the bloat, I'm using dreamweaver for the asp pages. Here is the actual code for what I am trying to do. This page will display hours so far entered for the user that is logged in. It is using a session variable to know what data to pull. I want to be able to have a link that will allow the client to download this information in excel.

I have attached a sample of the results page.

Thanks for the help.

<!--#include file="../Connections/conn.asp" -->
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
If (Session("userid") <> "") Then 
  Recordset1__MMColParam = Session("userid")
End If
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_conn_STRING
Recordset1.Source = "SELECT * FROM hoursLog WHERE userID = " + Replace(Recordset1__MMColParam, "'", "''") + " ORDER BY dateAC DESC"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1

Recordset1_numRows = 0

Dim userName__MMColParam
userName__MMColParam = "1"
If (Session("userid") <> "") Then 
  userName__MMColParam = Session("userid")
End If
Dim userName
Dim userName_numRows

Set userName = Server.CreateObject("ADODB.Recordset")
userName.ActiveConnection = MM_conn_STRING
userName.Source = "SELECT Fname, Lname FROM Users WHERE UserID = " + Replace(userName__MMColParam, "'", "''") + ""
userName.CursorType = 0
userName.CursorLocation = 2
userName.LockType = 1

userName_numRows = 0
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = 15
Repeat1__index = 0
Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
Dim MM_paramName 
' *** Go To Record and Move To Record: create strings for maintaining URL and Form parameters

Dim MM_keepNone
Dim MM_keepURL
Dim MM_keepForm
Dim MM_keepBoth

Dim MM_removeList
Dim MM_item
Dim MM_nextItem

' create the list of parameters which should not be maintained
MM_removeList = "&index="
If (MM_paramName <> "") Then
  MM_removeList = MM_removeList & "&" & MM_paramName & "="
End If


' add the URL parameters to the MM_keepURL string
For Each MM_item In Request.QueryString
  MM_nextItem = "&" & MM_item & "="
  If (InStr(1,MM_removeList,MM_nextItem,1) = 0) Then
    MM_keepURL = MM_keepURL & MM_nextItem & Server.URLencode(Request.QueryString(MM_item))
  End If

' add the Form variables to the MM_keepForm string
For Each MM_item In Request.Form
  MM_nextItem = "&" & MM_item & "="
  If (InStr(1,MM_removeList,MM_nextItem,1) = 0) Then
    MM_keepForm = MM_keepForm & MM_nextItem & Server.URLencode(Request.Form(MM_item))
  End If

' create the Form + URL string and remove the intial '&' from each of the strings
MM_keepBoth = MM_keepURL & MM_keepForm
If (MM_keepBoth <> "") Then 
  MM_keepBoth = Right(MM_keepBoth, Len(MM_keepBoth) - 1)
End If
If (MM_keepURL <> "")  Then
  MM_keepURL  = Right(MM_keepURL, Len(MM_keepURL) - 1)
End If
If (MM_keepForm <> "") Then
  MM_keepForm = Right(MM_keepForm, Len(MM_keepForm) - 1)
End If

' a utility function used for adding additional parameters to these strings
Function MM_joinChar(firstItem)
  If (firstItem <> "") Then
    MM_joinChar = "&"
    MM_joinChar = ""
  End If
End Function
Repeat1__numRows = -1
Repeat1__index = 0
rsLocations_numRows = rsLocations_numRows + Repeat1__numRows

<title>NCL: Home</title>
A {
    text-decoration: none;
    font-size: 9px; 
    font-family: verdana;
a.gray{color:575757; font-size:9px; font-family:verdana; text-decoration:underline}
a.gray2{color:454533; font-size:9px; text-decoration:none}
a.mlia{color:E75E03; font:100; font-size:11px; font-family:arial}
.style5 {color: #FFFFFF; font-weight: bold; font-size: 14px; }
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
    <tr valign="top">
        <td width="50%" bgcolor="AE760C"></td>
        <td height="100%">
<table height="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="fafae9">
        <tr valign="top"> 
          <td background="/images/left.gif" rowspan="8"><img src="/images/left.gif" width="32" height="10"></td>
          <td height="90" valign="bottom" bgcolor="#333333"><img src="/images/top2.jpg" border="0" width="762" height="90" alt=""> 
          <td rowspan="8" background="/images/right.gif"><img src="/images/right.gif" width="28" height="10"></td>
          <td width="763" height="21" valign="bottom" background="/images/but_back.jpg" bgcolor="#333333" style="padding-left:30; padding-top:6"><img src="/images/small.gif" border="0" width="4" height="6" hspace="3"> 
            <a href="default.asp">Home</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            <img src="/images/small.gif" border="0" width="4" height="6" hspace="3"> 
            <a href="history.asp">NCL Facts and History&nbsp;</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            <img src="/images/small.gif" border="0" width="4" height="6" hspace="3"> 
            <a href="NCLProgram.asp">NCL Program</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            <img src="/images/small.gif" border="0" width="4" height="6" hspace="3"> 
            <a href="chapters.asp"> NCL Chapters</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            <img src="/images/small.gif" border="0" width="4" height="6" hspace="3"> 
            <a href="faq.asp"> FAQ</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src="/images/small.gif" border="0" width="4" height="6" hspace="3"> 
            <a href="/contact.asp">            Contact Information &nbsp;&nbsp;</a>&nbsp;&nbsp;&nbsp;&nbsp; 
            <img src="/images/small.gif" border="0" width="4" height="6" hspace="3"> 
            <a href="/process.asp"> My NCL </a></td>
          <td width="763" valign="top" bgcolor="fafae9"><p><font size="2">&nbsp;<br>
              <%=(userName.Fields.Item("Fname").Value)%>, here are your current hours loged:<br>
            </font> </p>
            <table width="90%" border="1" align="center" cellpadding="4" cellspacing="2" bordercolor="#AE760C">
              <tr bgcolor="#D69417"> 
                <td><div align="center" class="style5">Location</div></td>
                <td><div align="center" class="style5">Activity</div></td>
                <td><div align="center" class="style5">Job</div></td>
                <td><div align="center" class="style5">Hours</div></td>
                <td><div align="center" class="style5">Date</div></td>
                <td><div align="center" class="style5">Comment</div></td>
                <td><div align="center" class="style5">Edit</div></td>
              <% While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF)) %>
              <tr bgcolor="#F7EC82"> 
                <td><div align="center"><%=(Recordset1.Fields.Item("hours").Value)%></div></td>
                <td><div align="center"><A HREF="editHours.asp?<%= MM_keepURL & MM_joinChar(MM_keepURL) & "hoursID=" & Recordset1.Fields.Item("hoursID").Value %>" class="gray">EDIT</A></div></td>
            <div align="center"></div>
            <div align="center"></div>
            <p>&nbsp; </p>

          <td valign="top"> <table border="0" cellpadding="0" cellspacing="0">
                <td valign="middle" style="padding-left:20">You may need Macromedia 
                  Flash Player<br>
                  to view parts of this site. You may download it <a href="" class="mlia" style="font:900; font-size:9px">here</a>.</td>
                <td><img src="/images/footer1.jpg" border="0" width="12" height="86" alt=""></td>
                <td style="padding-left:20; padding-right:20">Copyright 2004, 
                  NCL Glendale. All rights reserved.<br>
                  Support and development by PC Techies</td>
                <td><img src="/images/footer2.jpg" border="0" width="9" height="86" alt=""></td>
                <td> <img src="/images/map1.jpg" alt="" width="49" height="86" border="0" usemap="#Map"><img src="/images/map2.jpg" alt="" width="37" height="86" border="0" usemap="#Map2"></td>
    <td width="50%" bgcolor="D69417"></td>

<map name="Map">
  <area shape="rect" coords="25,35,39,52" href="/default.asp">
<map name="Map2">
  <area shape="rect" coords="9,34,24,54" href="">
Set Recordset1 = Nothing
Set userName = Nothing

Hi jjorgensen626,

maybe I can help, but I have only done that in jsp-pages.

To export a table, I have a button, which opens a new jsp-Page, just call it excel.jsp. In excel.jsp I set the following values of the Response-object:

response.setHeader("Content-Disposition", "attachment; filename=\""+ strFile +"\";");

Afterwards, I open the connection to the database, get the ResultSet and instead of writing it into a table, I use a OutputStream, which I get from the Response-Object:
OutputStream ostr = response.getOutputStream();

For every col I add a ";" after the colvalue, after every row I add a "\n" for a linebreak.

Resultset rs = dbcon.createStatement().executeQuery(query);
while ({

Maybe this helps and you can change it so it works in asp, c#/.net.

best regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.