Database data generates a Word document method

zhaozj2021-02-16  139

Record the record from the library to generate a Word document. There are such a few ways: 1. Reform, it is like an excel's response.buffer = truerente.contentType = "Application / VND.MS-Excel" --ExcelResponse.contentType = "Application / Msword" - Word Excel is nothing, very good, Word is Total error. weak! 2. The example of Microsoft's RTF-DOC, its example is very successful, can be changed according to actual needs, not good, format is not good. 3. Call the word.application object, call Word generation with this set objactoc = createObject ("Word.Document"), but you want to change the security level setting of the client to the client! The format is controlled. I have drumed the old half-day. 4. It is the method introduced in this article, but I only find my thoughts, or introduce the idea of ​​generating Excel, the example is not. Depressed to the pole, the test has been used, and finally there is a method and example of this article. I don't dare to say that original, I can only say that I have a comprehensive thing, I have made this thing.

Less nonsense, see example.

I am a Word document that generates teachers' personal information. ============================== "Write your database connection response.buffer = true" This seems to be left peopleid = Request ("PeopleID") SET RS = Server.createObject ("AdoDb.Recordset") SQL = "SELECT * from people where people" & people) SQL, CONN, 3, 3 if ql, conn, 3, 3 if xi e e Click End if =========================================== The above is The teacher's information is taken out, replace it into your things. The file is generated below. ======================================= Dim fso, myfile set fso = creteObject ("scripting.filesystemObject") 'uses FSO sfilename = "Temp.doc" set myfile = fso.createtextfile (server.mappath (") &" / word / "& sfilename, true) 'file is placed in the current directory Word / under mystring =" Here is you pre-row Good Word document, the place to fill is air, how can you tell! "MyFile.Writeline (MyString) myfile.close 'Close the file ================== ================= The file has been generated. Note setting the Word directory to "Write" in the IIS. The Word document is written below, and the IE does not automatically open to prevent error prompts.

================================================================================================================= Server.CreateObject ( "ADODB.Stream") objStream.OpenobjStream.Type = adTypeBinaryobjStream.LoadFromFile Server.MapPath (strFilePath) 'change the path if necessaryResponse.ContentType = "application / octet-stream" Response.AddHeader "Content-Disposition", "Attachment; FileName = Chemical College Teachers Data Acquisition Table -" & RS ("Name") & "DOC" "If the file name is fixed, then directly filename = file name .doc, this name is the user chooses" save " When the name, the name. BinaryWrite Objstream.ReadResponse.flushobjstream.closset objstream = nothing 'Write, release the object ========================= ===== The above is to open the Word document in the form of an attachment, of course, there is no error. I found that IE will be opened directly by IE. Don't let it play directly! ======================= rs.closset = NothingConn.closset conn = Nothing ================ ==== Turn off the database connection. ================================== 注: MyString = "" What is it in the end? Do a document you want to use with Word, which is rugged, and make a tag to fill it, such as writing a few words. Then "Save As" Web page. What is mentioned in this article is that there is no picture in the document, and I will not.

Find this web page in your hard drive, open, view the source file, Ctrl A, Ctrl C, in Word, Ctrl N, Ctrl V, Ctrl F, click "Replace" tag, first step: Find content fill double quotation marks ", replace it with two double quotes" ", all replaceable. Step 2: Point" Advanced ", find the" Special Character "" Paragraph Mark ", replace" Special "Uninterruptible space", all replacement can be replaced. Then Ctrl A, Ctrl C, between the two quarters of mystring = "". ============ ============================== This time the .asp file can be executed, but the generated empty Word document, no Fill, useless. In the quotation marks behind MyString, find you tag, such as the name, you fill in "Watermelon", so find "Watermelon", delete, play "& RS (" Name ") & ", Including two quotes before and after. Together, all places are used in the records in the library. ========================== ============== Oh, now it is. It is basically satisfied. Note: Several small problems 1. Sometimes you save your Word document to the web page, generate "your file Folder. Then, when you use this .asp file to dynamically generate a Word document, you will prompt "XXXX Lost", unhappy! Solution: In your saved web page, view the source file, find "your file name .files ", the relevant places are removed. Generally there will be and style. Look at the deletion.

2. This .asp file execution is the download prompt box, if "Open", Word will open the generated document, I found that sometimes it is opened with "Web View", sometimes "page view" open. Very strange, careful comparison, found: As long as you find in your MyString, add Print , then open with "page view" . it is good!

转载请注明原文地址:https://www.9cbs.com/read-8323.html

New Post(0)