|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Exporting to Excel
Hello, I have a page that pulls Data and places it in to a Table to display.
I would like to have a button that will EXPORT all the data in the table to an Excel file OR .CVS. How could this be accolimpished? This is what I currently Have: Notice I have a "Print" Button. I would like to have a "Export" button below it. Code:
<%
login=Request.Cookies("login")
if login <> "2" THEN
Response.Redirect("../t1/database/perfmgmt/newsite/noaccess.html")
end if
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<meta name="generator" content="Adobe GoLive 6">
<title>Create Log Entry</title>
<STYLE TYPE = "text/css">
P.basic {color: black}
SPAN {font:10pt arial; color: black}
</STYLE>
<script language="JavaScript">
<!--
function jsAlert(thecomments)
{
alert(thecomments);
}
// -->
</script>
<center>
<script language="JavaScript">
<!-- Begin
if (window.print) {
document.write('<form> '
+ '<input type=button name=print value="Print" '
+ 'onClick="javascript:window.print()"> </form>');
}
// End -->
</script>
</head>
<body bgcolor="#ffffff">
<%
themonth = request.form("month")
theYear = request.form("theYear")
'Get all entries in the database for the Agent for the entire month
startDate = theMonth&"/01/"&theYear
endDate = theMonth&"/31/"&theYear 'all dates must be less than 31, even if there aren't 31 days in every month! :-p
'Get information from DB & Connect to database
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=SQLOLEDB;Data Source=xx.xx.xx.xxx;database=xxxxxxx;uid=xx;pwd=xx ;"
query1 = "SELECT Agent,(count(*)) "
query1 = query1 & "FROM TICKETDOC where DATED >= '"&startDate&"' and DATED <= '"&endDate&"' GROUP BY AGENT"
set objRS=Conn.execute(query1)
if objRS.EOF THEN
%></div>
<center>
<table width="600" border="0" cellspacing="2" cellpadding="2" bgcolor="#dcdcdc">
<tr><td><div align="center">
<font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" size="5" color="#a98b5e">
No Records were Found that Matched This Criteria </font></div>
</td></tr></table><HR><%
else
%>
<div align="center">
<font size="3" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><b>TICKET DOCUMENTATIONS FOR: <%response.write(themonth)%><%response.write(theyear)%></b></font><BR><BR>
<center>
<table width="500" border = "0" cellspacing = "0" cellpadding = "2">
<tr><td width = "20%"><SPAN><b>AGENT</b></td><td width = "20%"><SPAN><b>TICKET COUNT</b></td></tr></table><%
'Populate Table Array from DB
tablearray = objRS.Getrows
objRS.Close
Conn.close %>
<table width="500" border = "0" cellspacing = "0" cellpadding = "2">
<%
'Display Results
for i = 0 to UBound(tablearray,2)
response.write("<tr>")
for j = 0 to UBound(tablearray,1)
%><td align="left" valign="top" width = "20%"><%
if j= 13 THEN
%><%=tablearray(j,i)%></SPAN></a>
<%
else %><SPAN><%
response.write(tablearray(j,i) & " ")
end if
NEXT
response.write("</tr>")
NEXT
%></table><%
end if
%>
<BR><BR><BR><BR><center>
<p><img src="allbrands.jpg" alt="" height="63" width="432" border="0"></p>
<table width="90%" border="0" cellspacing="0" cellpadding="0" bgcolor="#805d5d">
<tr>
<td>
<div align="center">
<font size="1" color="white" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Copyright © 2003 InterContinental Hotels Group. All Rights Reserved.</font><font size="1" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> </font></div>
</td>
</tr>
</table>
</div>
</body>
</html>
THANKS FOR ANY HELP! Chris chris@thesouthside.net |
|
#2
|
|||
|
|||
|
Hello Chris,
I have an Excel Export example written in PHP. You can use the concepts with the language of your choice. I created a PHP file called "ms_export.php" that contains the following code: <?php # This line will stream the file to the user rather than to the screen header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=$filename"); header("Pragma: no-cache"); header("Expires: 0"); echo $export_data; ?> Then I in my HTML code, I add the following: <?php print " <form name=export_form ENCTYPE=multipart/form-data ACTION=ms_export.php METHOD=POST> <input type=hidden name=export_data value=\"$export_data \"> <input type=hidden name=filename value=\"$filename\"> </form> <input type=button value='Excel Export' onClick='document.export_form.submit();'> "; ?> Simply fill in the $filename variable with a filename and ".xls" extension. Then fill the $export_data variable with anything you want to send to Excel using tabs \t as separators. Excel will read it directly in. BTW, if you change the filename extension to ".doc" it will feed your data to MS Word. Stephen |
|
#3
|
|||
|
|||
|
Hey! I can tell you how to export to excel from ASP....don't know if that's what you need. Anyway, here is the code to place in page...say generateExcel.asp before any response.write!!:
parameter = "attachment; filename=dumpFile - " & <Descripcion you want> & ".xls;" Response.buffer = true Response.ContentType = "application/vnd.ms-excel" Response.AddHeader "Content-Disposition", parameter Every response.write you make (as a table) is printed to an excel file, which you will be asked to save to disk. <HTML> .. .. <form action="generateExcel.asp" method="POST"> Enter the name for the file: <input type="text" name="fileName"> <A href="#" onClick="document.submit();"> Click here to download the Excel File </A> </form> .. .. </HTML> I've allways liked to use links instead of buttons to submit....the look better to me! Hope it works... Anibal. |
|
#4
|
|||
|
|||
|
Anibal, Thanks for both your help and Shatcher's Help.
I do need it in ASP code rather than PHP. I need a little push in the right direction. I'm trying to Follow your directions but I'm running into problems. I created the generateExcel.asp page and put it in the same directory as the main file. Here it is: Code:
<% parameter = "attachment; filename=dumpFile - " & <Description you want> & ".xls;" Response.buffer = true Response.ContentType = "application/vnd.ms-excel" Response.AddHeader "Content-Disposition", parameter %> I Need help Modifing my other ASP page that has the tables to reference "generateExcel.asp". I'm a little new to this! Code:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<meta name="generator" content="Adobe GoLive 6">
<title>Service Observation Summary</title>
<STYLE TYPE = "text/css">
P.basic {color: black}
SPAN {font:10pt arial; color: black}
</STYLE>
<script language="JavaScript">
<!--
function jsAlert(thecomments)
{
alert(thecomments);
}
// -->
</script>
<center>
<script language="JavaScript">
<!-- Begin
if (window.print) {
document.write('<form> '
+ '<input type=button name=print value="Print" '
+ 'onClick="javascript:window.print()"> </form>');
}
// End -->
</script>
</head>
<body bgcolor="#ffffff">
<%
themonth = request.form("month")
theYear = request.form("theYear")
'Get all entries in the database for the Agent for the entire month
startDate = theMonth&"/01/"&theYear
endDate = theMonth&"/31/"&theYear
'all dates must be less than 31, even if there aren't 31 days in every month!
'Get information from DB and let user know if there is more than 1 service observe for a date
'Connect to database to upload all form data.
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=SQLOLEDB;Data Source=XX.XX.XXX.XXX;database=XXXXX;uid=XXX;pwd=XX XXX;"
query1 = "SELECT Agent,Phone_Id "
query1 = query1 & "FROM TICKETDOC where DATED >= '"&startDate&"' and DATED <= '"&endDate&"' order BY AGENT"
set objRS=Conn.execute(query1)
%>
<b><font size="2" color="#ee0000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Service Observation Summary</font></b>
<table width="700" border=0 cellspacing=2 cellpadding=2 color="000000">
<tr align="left" valign="top">
<td bgcolor = "DDDDDD" align="center" valign="top" width="90"><b><font size="2" color="#ee0000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Agent </font></b></td>
<td bgcolor = "DDDDDD" align="center" valign="top" width="95"><b><font size="2" color="#ee0000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Count</font></b></td>
</tr>
<%
Do Until objRS.EOF
response.write ("<tr>")%><td bgcolor = "DDDDDD" align="center" valign="top"><font size="2" color="#000000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><%
response.write (objRS.Fields("AGENT") & "</td>")
%><td bgcolor = "DDDDDD" align="center" valign="top"><font size="2" color="#000000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><%
response.write (objRS.Fields("Phone_Id") & "</td>")
response.write ("</tr>")
objRS.movenext
loop
%>
<BR><center>
<table width="90%" border="0" cellspacing="0" cellpadding="0" bgcolor="#805d5d">
<tr>
<td>
<div align="center">
<font size="1" color="white" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Copyright © 2003 InterContinental Hotels Group. All Rights Reserved.</font><font size="1" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> </font></div>
</td>
</tr>
</table>
</div>
</body>
</html>
|
|
#5
|
|||
|
|||
|
Hey! It's quite easy..just place the code I gave you before you print anything in the page you have. For example, place the code after the <body> tag...Just copy it! it should work. I'm not sure, since, i don't programm the conections anymore (we have a file that does the connection
) but, I think you missed a recordSet Object. You are supposed to have a recordset object to recive the oConn execution (once again....not sure about it). If it works like it is...great.Anibal. PS: Here's how you add it! <html> <head> <title>Service Observation Summary</title> <STYLE TYPE = "text/css"> P.basic {color: black} SPAN {font:10pt arial; color: black} </STYLE> <script language="JavaScript"> <!-- function jsAlert(thecomments) { alert(thecomments); } // --> </script> <center> <script language="JavaScript"> <!-- Begin if (window.print) { document.write('<form> ' + '<input type=button name=print value="Print" ' + 'onClick="javascript:window.print()"> </form>'); } // End --> </script> </head> <body bgcolor="#ffffff"> <% parameter = "attachment; filename=dumpFile - " & <Description you want> & ".xls;" Response.buffer = true Response.ContentType = "application/vnd.ms-excel" Response.AddHeader "Content-Disposition", parameter themonth = request.form("month") theYear = request.form("theYear") 'Get all entries in the database for the Agent for the entire month startDate = theMonth&"/01/"&theYear endDate = theMonth&"/31/"&theYear 'all dates must be less than 31, even if there aren't 31 days in every month! 'Get information from DB and let user know if there is more than 1 service observe for a date 'Connect to database to upload all form data. Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "Provider=SQLOLEDB;Data Source=XX.XX.XXX.XXX;database=XXXXX;uid=XXX;pwd=XX XXX;" query1 = "SELECT Agent,Phone_Id " query1 = query1 & "FROM TICKETDOC where DATED >= '"&startDate&"' and DATED <= '"&endDate&"' order BY AGENT" set objRS=Conn.execute(query1) %> <b><font size="2" color="#ee0000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Service Observation Summary</font></b> <table width="700" border=0 cellspacing=2 cellpadding=2 color="000000"> <tr align="left" valign="top"> <td bgcolor = "DDDDDD" align="center" valign="top" width="90"><b><font size="2" color="#ee0000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Agent </font></b></td> <td bgcolor = "DDDDDD" align="center" valign="top" width="95"><b><font size="2" color="#ee0000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Count</font></b></td> </tr> <% Do Until objRS.EOF response.write ("<tr>")%><td bgcolor = "DDDDDD" align="center" valign="top"><font size="2" color="#000000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><% response.write (objRS.Fields("AGENT") & "</td>") %><td bgcolor = "DDDDDD" align="center" valign="top"><font size="2" color="#000000" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><% response.write (objRS.Fields("Phone_Id") & "</td>") response.write ("</tr>") objRS.movenext loop %> <BR><center> <table width="90%" border="0" cellspacing="0" cellpadding="0" bgcolor="#805d5d"> <tr> <td> <div align="center"> <font size="1" color="white" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Copyright © 2003 InterContinental Hotels Group. All Rights Reserved.</font><font size="1" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> </font></div> </td> </tr> </table> </div> </body> </html> |
|
#6
|
|||
|
|||
|
ive tried your asp code for the same reason and got
Syntax error /generateExcel.asp, line 2, column 49 parameter = "attachment; filename=dumpFile - " & <Description you want> & ".xls;" when it goes to the generateExcel.asp file |
|
#7
|
|||
|
|||
|
Hey flase420! I'm not sure wheather this is an old thread or not..but here it goes!
Pleaaaase, tell me you didn't use that line exactly and literaly (I quote)... Quote:
you did...didn't you ...here's an example of how to use it (just the line):parameter = "attachment; filename=exportedFile.xls;" That would generate a file called exportedFile.xls Good Luck Anibal. |
|
#8
|
||||
|
||||
|
this isn't javascript anymore, is it?
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Exporting to Excel |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|