How to generate WORD Documents from database data

Source: Internet
Author: User

From: Xiao XiaoYu
Graduation design requires: Getting records from the library to generate WORD Documents.
I have been searching for a long time. There are several main methods:
1. Change the header, just like Excel
Response. Buffer = true
Response. contenttype = "application/vnd. MS-excel" '-- excel
Response. contenttype = "application/MSWord" '-- word
Excel is okay, very good, word is always an error. Weak!
2. The example of Microsoft's RTF-DOC is very successful,
It can be changed according to actual needs. It is difficult to get it, and the format is not easy to control.
3. Call the word. Application object. Set objworddoc = Createobject ("word. Document") on the client ")
To call word generation, but it is not good to change the security level settings of the client! It is also difficult to control the format.
I have been busy for a long time.
4. This is the method described in this article, but I only found the idea and introduced the idea of generating excel.
I was so depressed that I tried it many times and finally got the methods and examples in this article. I dare not say it is original. I can only say that it is integrated with everyone's things.
Let's take a look at the example.

In my example, a Word document is generated for the instructor's personal information.
==================================
'Write your database connection here
Response. Buffer = true ', which seems to have to be retained.
Peopleid = request ("peopleid ")
Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * from people where peopleid =" & peopleid
Rs. Open SQL, Conn, 3, 3
If Rs. EOF and Rs. bof then
Do something
End if
========================================================== ===
The above is to take out the instructor's information and replace it with your stuff.
The following file is generated.
====================================
Dim FSO, myfile
Set FSO = Createobject ("scripting. FileSystemObject") 'uses FSO
Sfilename = "temp.doc"
Set myfile = FSO. createtextfile (server. mappath (".") & "word" "& sfilename, true) 'file is placed under the word/in the current directory

Mystring = "here is the pre-prepared Word document. You have no space to fill in. How can you tell me! "
Myfile. writeline (mystring) myfile. Close 'close the file
==========================================
The file has been generated. Note: in IIS, set the word directory to "write ".
The following documents are written as data streams, so that IE is not automatically opened to prevent error messages.
========================================
Dim strfilepath
Const adtypebinary = 1
Strfilepath = "word/temp.doc"
Set objstream = server. Createobject ("ADODB. Stream ")
Objstream. Open
Objstream. type = adtypebinary
Objstream. loadfromfile server. mappath (strfilepath) 'Change the path if necessary
Response. contenttype = "application/octet-stream"
Response. addheader "content-disposition", "attachment; filename = data collection table of faculty and staff of the School of Chemical Engineering --" & RS ("name ")&". doc "'If the file name is fixed, then directly filename=file name .doc. This name is the name that appears when the user selects" save ".

Response. binarywrite
Objstream. readresponse. Flush
Objstream. Close
Set objstream = nothing: finish writing and release the object.
====================================
The above is to open the Word document in the form of attachments, of course, no error.
If the attachment is removed, it will be opened directly with IE. Don't let it hit it!
======================================
Rs. Close
Set rs = nothing
Conn. closeset conn = nothing
================================
Close the connection to release the database.
======================================
Note: What is in mystring =?
Use Word to make a document in the format you want,
Mark the place to be filled, such as writing a few words or something.
Then, "Save as" web page. This article is about the absence of images in the document, and I will not.

Go to your hard disk and find this web page. Open and view the source file, CTRL + A, CTRL + C,
In word, CTRL + N, CTRL + V, CTRL + F, click "replace,
Step 1: Fill in double quotation marks for the search content, replace them with two double quotation marks, and replace them all.
Step 2: click "advanced". Fill in the "section mark" of "special characters" in the search content, replace it with the "uninterrupted space" of "special characters", and replace it with all.
CTRL + A, CTRL + C, and place the paste between the two quotation marks of mystring =.
========================================================== =
At this time, the. asp file can be executed, but the generated Empty Word file is useless.
In the quotation marks behind mystring, find your tags. For example, you fill in "watermelon" in the name column ",
Now, find the word "watermelon", delete it, and press "& RS (" name ")&",
Contains two quotation marks. And so on, all the places are handled with records in the database.
==========================================================
Oh, now. The requirements are basically met.
Note: several minor issues

1. Sometimes, when you save your Word documents to a Web page, a folder named "your file name. Files" is generated.
When you use this. asp file to dynamically generate a Word document, the system will prompt "XXXX lost!
Solution: on the Web page you saved, view the source file and find "your file name. Files",
Delete all related items. There will usually be styles. Delete it.

2. This. asp file will display a download prompt box. If you select "open,
Word will open the generated document. I found that sometimes it is opened in "Web View,
Sometimes it is opened in "Page View. It was very strange. After careful comparison, we found that:
As long as you find it in your mystring and add print to it, it will be opened in "Page View. Good!

 

Original: http://www.hanxun.cn/document/content.php? Id = 113

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.