How do I write the contents of the database into the Word document?

zhaozj2021-02-16  59

When programming, because Microsoft Word's editing is very powerful, we often want to save some data as a Word document so that we have professional editing. When saving as a Word document, we can pass OLE, but often encounter to transfer the real Word Editing interface to the program so that the program interface is confusing. Also, ordinary calling words must specify the absolute address of the Word executable file, because many users are not consistent with the path to the Word installed, so we often encounter the problem of calling errors, can there be a way to call Word will not It shows that it is independent of version, installed paths? The answer is yes. Below, I will come to a small program to give you a demonstration, only playing a role of a throwing jade. For friends for reference only. First, we build an access2000 database data.mdb, we create a table UserData in the library, there is a "work number", "name", "gender", "department" four fields, all of which are characters, and then enter Some data, we build a form FORM1, in Form1, place the following controls:

Control Name Property Value Description Tlabel1Caption "Please enter the file name" TEdit1Name "" to enter the file name, with the extension TButton1Caption "save" TButton2Caption "exit" ADOTable1ActiveConnectionStringTableNameTrueProvider = Microsoft.Jet.OLEDB.4.0; Data Source = data.mdb; Persist security info = falseuserdata

Here is the related code: // ------------------------------------------- --------------------------------

#include #pragma HDRSTOP

#include "unit1.h" ------------------------------------------- -------------------------------- # pragma package (smart_init) #pragma resource "* .dfm" TFORM1 * FORM1; -------------------------------------------------- -------------------------__ fastcall tform1 :: tForm1 (tComponent * Owner): TFORM (OWNER) {} -------- -------------------------------------------------- -----------------

void __fastcall TForm1 :: Button1Click (TObject * Sender) {Variant OLEObject; AnsiString dataword; if (Trim (Edit1-> Text) == "") {ShowMessage ( "Please enter the file name"); return;} String currentPath = GetCurrentDir () "//" Trim (edit1-> text); oleobject = creteoleObject ("word.basic"); oleobject.exec ("filenew")); oleobject.exec (Procedure ("endofdocument")) ; Adotable1-> first (); while (adotable1-> eof! = True) {dataword = adotable1-> FieldValues ​​["IDO"] adotable1-> FieldValues ​​["Name"] adotable1-> FieldValues ​​["Gender" ] Adotable1-> fieldvalues ​​["department"]; oleobject.exec ("insert") << DataWord; adotable1-> next (); oleobject.exec (Procedure ("StartofDocument"); oleObject.exec ("Filesaveas") << currentpath; oleobject.exec ("fileclose") << 1); showMessage ("file has been written! Thank you!");} ---------- -------------------------------------------------- ----------------- void __fastcall tform1 :: butt2click (Tobject * sender) {this-> close ();} ------------- -------------------------------------------------- ------------ Void __fastcall tform1 :: edit1click (TOBJECT * Sender) {edit1-> text = "";} ------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Author: Hu Guangtao

If you have any better suggestions and comments, please contact me. Email / msn: huguangtao0515@hotmail.comqq: 621150

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

New Post(0)