Solved

Export to Word from ASP

Posted on 2014-12-29
4
257 Views
Last Modified: 2015-01-05
Hi Experts
i am working in an old application developed using classic ASP and VB, i am trying to export an HTML table from an asp page to word, i searched the internet for this and i found a good code that its working perfectly, he problem is that it export the whole page to word, i need to export only a part of the page which is the <table></table>

<%@ Language=VBScript %>
<%
Option Explicit
Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-word"
Response.AddHeader "Content-Disposition", "attachment; filename = MyDoc.doc"
%>
<html>
<head>
</head>
<body>
<%
Dim Conn,strSQL,objRec
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("shotdev/mydatabase.mdb"),"" , ""
strSQL = "SELECT * FROM customer "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3
%>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<%
While Not objRec.EOF
%>
<tr>
<td><div align="center"><%=objRec.Fields("CustomerID").Value%></div></td>
<td><%=objRec.Fields("Name").Value%></td>
<td><%=objRec.Fields("Email").Value%></td>
<td><div align="center"><%=objRec.Fields("CountryCode").Value%></div></td>
<td align="right"><%=objRec.Fields("Budget").Value%></td>
<td align="right"><%=objRec.Fields("Used").Value%></td>
</tr>
<%
objRec.MoveNext
Wend
%>
</table>
<%
objRec.Close()
Conn.Close()
Set objRec = Nothing
Set Conn = Nothing
%>      
</body>
</html>

Open in new window

can you please help me to solve this issue thank you
the link to the code
How to use ASP & Word Response.ContentType
0
Comment
Question by:AZZA-KHAMEES
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 40523226
What do you mean by part of a page?  If the table takes up 4" on a 11" tall document, you are still going to get an 8.5" by 11" document with the table taking up whatever room it needs.
0
 
LVL 31

Expert Comment

by:Wayne Barron
ID: 40523231
Are you saying that it exports more then what is in the TABLE?
If that is the case, then you need to have a link, that the user click on, that takes them to a page that is used for: Printing
And only have on that page, the needed information that the user will require to have exported into WORD.

That is all that needs to be done.
Good Luck
Carrzkiss
0
 

Author Comment

by:AZZA-KHAMEES
ID: 40523252
thank you for the reply, sorry but i posted the original code, what i mean that when i add a form with forms fields and the i click export to word the form fields also will be exported, i need only the table to be exported to the word document
0
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 40523326
One option is to place your form on page 1 and have your code above on page 2.  Submit the form on page 1 to page 2.

If your table contains a lot of data where the word document is something like 5 or 10 pages long, you will find that streaming to the browser like you are doing can eat up your resources.  If there is only one user it may not be an issue.    

What I find is a better solution is to simply write the file to the server naming the file as a something.doc and no need to use the response.contenttype.
Response.ContentType = "application/vnd.ms-word"
Response.AddHeader "Content-Disposition", "attachment; filename = MyDoc.doc"

Open in new window


Instead, you will need to write everything to disk with file system object.   http://www.w3schools.com/asp/asp_ref_filesystem.asp
Using the simple example from the link
<%
dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile("c:\test.txt",true)
fname.WriteLine("Hello World!")
fname.Close
set fname=nothing
set fs=nothing
%>

Open in new window

You can convert that to what you have
<%@ Language=VBScript %>
<%
Option Explicit

dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
' ************************************************************************************
'***  NOTE: You must set the folder to where you are writing the file to have write permissions  ****

set fname=fs.CreateTextFile("c:\mysitefile\mydocument.doc",true)

' ************************************************************************************
Dim Conn,strSQL,objRec
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("shotdev/mydatabase.mdb"),"" , ""
strSQL = "SELECT * FROM customer "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3

fname.WriteLine "<table width='600' border='1'>"
fname.WriteLine "<tr>"
fname.WriteLine "<th width='91'> <div align='center'>CustomerID </div></th>"
fname.WriteLine "<th width='98'> <div align='center'>Name </div></th>"
fname.WriteLine "<th width='198'> <div align='center'>Email </div></th>"
fname.WriteLine "<th width='97'> <div align='center'>CountryCode </div></th>"
fname.WriteLine "<th width='59'> <div align='center'>Budget </div></th>"
fname.WriteLine "<th width='71'> <div align='center'>Used </div></th>"
fname.WriteLine "</tr>"

While Not objRec.EOF

fname.WriteLine "<tr>"
fname.WriteLine "<td><div align='center'>"&objRec.Fields("CustomerID").Value&"</div></td>"
fname.WriteLine "<td>"&objRec.Fields("Name").Value&"</td>"
fname.WriteLine "<td>"&objRec.Fields("Email").Value&"</td>"
fname.WriteLine "<td><div align='center'>"&objRec.Fields("CountryCode").Value&"</div></td>"
fname.WriteLine "<td align='right'>"&objRec.Fields("Budget").Value&"</td>"
fname.WriteLine "<td align='right'>"&objRec.Fields("Used").Value&"</td>"
fname.WriteLine "</tr>"

objRec.MoveNext
Wend

fname.WriteLine "</table>"

objRec.Close()
Conn.Close()
Set objRec = Nothing
Set Conn = Nothing
%>      

Open in new window

Another tip is if you have multiple users, it is very easy to over write the file name.  You can name the file with the session id http://www.w3schools.com/asp/prop_sessionid.asp or a timestamp.  

sid = Session.SessionID
doc_name = "mydocument_"&sid&".doc"
set fname=fs.CreateTextFile("c:\mysitefile\"&doc_name,true)

Open in new window

That naming gives you a little security through obscurity too. But never assume it is hidden.

Another tip is to place these files in a separate folder, then run a scheduled task every night to clean out all files in that folder so they are not taking up space or easily found.

If the files really need to be secure, I will save them to a folder that is outside of the site files and call them up when needed.  But that would be for another question.  For now, I would practice just making a static file, then try out using your db.

This method will be a little more coding for you, but a better experience for your user.

You can run this script on the same page as the form.  But once completed, you can give a link to the page like
response.write "<a href='folder/mydocumentfolder/"&doc_name&"'>Right Click To Download</a>"

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to count occurrences of each item in an array.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question