Monday, 11 July 2011

ASP: export data to a Ms Word document

   


We can create a Ms Word document as output of an ASP page. In the past I published articles about exporting to Ms Excel, to Ms Access and to PDF. In this post we'll see how to export data to Ms Word.

The way data is exported to a Ms Word file is probably the easiest. I assume we gather data from a database and have a resulting recordset (called RS_Word). We can display the records with a repeat region or not. In the following example I assume the data is unique, thus we will not use a repeat region. Let's imagine the data is just some pieces of plain text.


The code
There's not much to say about the code. Anyway, the first thing we do is to create a variable for the file name:
<%
Dim file
file = "filename.doc"
Change filename with an appropriate file name. We then start creating the Word file:
With Response
    .Buffer = True
    .ContentType = "application/msword"
    .AddHeader "content-disposition", "inline; filename=" & file
Now we insert the content of the Word document. It is very easy to do and a single line is enough to create - as in the example - a title:
    .Write "<center><strong>" & RS_Word.Fields.Item("title").Value & "</strong></center><br><br>"
Then we insert a new line:

    .Write strLine
And continue inserting the text:
    .Write "<br><br>" & RS_Word.Fields.Item("text").Value
Finally we wrap everything up:
    .Flush
    .End
End With
%>
Very straightforward, isn't it?

As usual, let me know what you think in the comment section below.

4 comments:

  1. hi

    i have one question that i can't fine a good answer yet

    i export a data (a table) to word/open office i wont to have a title on each page (the title is the same on each page)


    thanks for your time

    my email is yair_abraham@hotmail.com

    ReplyDelete
  2. Yair, the issue is more complicated than I thought. Please have a look at this page:
    http://techsynapse.blogspot.com/2007/03/generating-word-document-dynamically.html

    ReplyDelete
  3. Yair, I did search again on the issue, and I've found something. If you create a new .doc file with a header and save it as HTML page, you will notice that the header is treated as an external file, embedded through CSS (with @page). That is because the header should appear on every page. You should try and reproduce that, and I believe you are done.

    ReplyDelete
  4. I adjusted your code, I would like the end result to permit me to replace content found inside the word document with new content inserted from a form.

    Found inside word: Answer_to_question_1

    replace with...

    Variable collected from asp form: Asp_answer_to_question_1

    'Creates a MS Word File
    Dim file
    file = "wingding1.doc"
    yepper = "test_variable"
    With Response
    .Buffer = True
    .ContentType = "application/msword"
    .AddHeader "content-disposition", "inline; filename=" & file
    .Write "" & yepper & ""
    .Write strLine
    .Write "testing123"
    .Flush
    .End
    End With

    Any input would be great,

    Thanks,

    Kevin

    ReplyDelete

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.