Solved

Export to Word from ASP

Posted on 2014-12-29
4
230 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
  • 2
4 Comments
 
LVL 52

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 30

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 52

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now