Better Output VFP Report by Word
I. Overview
Whether it is Western or Chinese Visual FoxPro 3.0 (hereinafter referred to as VFP), its report output has a serious defect, which is that when a field of the database has overflow extension properties (that is, when the content of the field is longer than the specified output width, automatic When the attributes of the wrap are prone to half of the Chinese characters, the contents of the report appear "garbled".
In addition, the VFP is the same as other database management systems, and the report output module also has the following serious shortcomings:
1 The width and length of the report cannot be changed by the user, and any change in the page cannot be implemented;
2 Since the length of the data in the database is different, it will inevitably lead to the width of the fields in the output report, the system cannot adjust the width of the outputs of the output of the actual content of the database, the overall effect of the report is poor;
3 Once the report is output, the user cannot edit the report format;
4 Will not implement individual output or combined outputs for certain specific fields.
To this end, the author uses the VFP's OLE automation technology, and outputs the records of the database into a Word document through the Word.Basic object provided by Microsoft Word 6.0.
Second, automatically generate the basic principles and methods of Word documentation in VFP
1. Basic Principles for Generating Data Word Documents with Word.Basic We know that Windows OLE technology has established an information sharing mechanism between two applications with OLE capabilities. Apply the app called the OLE service to request the OLE service app called the client. Since the VFP cannot provide available objects for other OLE capabilities, they cannot be used as an OLE server. In order to use OLE automation techniques in VFP to resolve the problem of report output, you must select an application that can be used as an OLE server in a Windows environment. Microsoft Word 6.0 can not only provide OLE object type Word.basic, but also generate complex tables, with strong editing functions, is an ideal tool to generate a database report.
The information sharing mechanism between VFP and Microsoft Word 6.0 is implemented by creating by MS Word 6.0 in the VFP to deliver the OLE object word.basic. Therefore, to use OLE automation technology, you must first create a function creteObject () creation of the OLE object (): oleword = creteObject ("word.basic")
2. Basic methods of generating data Word documents with Word.Basic After creating an OLE object with the CreateObject () function, we can use the object-oriented programming method to call the specific order of Word.Basic to generate the specific content of the database. Word documentation.
The Word.Basic is used to generate a Word document for the database. There are two common methods: the first method is to generate a database of a series of commands in a series of commands in Word.Basic; another way is to use Word.Basic to open prefabricated basic Document framework, use Word.Basic's editing command to generate a Word document for the database. These two methods have advantages, and sometimes they use them. The first method is mainly used for automatic generation of all required database reports. Due to automatic generation technology, although automatic adjustment of output pages and automatic adjustment of various field widths in the report can be implemented, it cannot be directly The title with the corresponding characteristics is output according to the specific database. The second method has greater flexibility in the format of the title and output. Typically, use the first method to output a general document of some databases, and then modify it into a basic document framework for special report output by W Ord for a second method.
The specific implementation and main algorithm of the second method are described below.
(1) Customize the basic Word document framework
In the basic document frame, you can include any content of the Word document, such as inserting some pictures, etc. in both sides of the title. However, in order to implement the positioning output of the database field, the document frame must contain a table that can be designed in any complex form, such as the following document frame:
Table 1 Adverse geological: gas information
In order to enable Word.Basic to correctly output the fields to be output in the database to the basic framework, add location tags in the basic framework. We can use methods to add bookmarks in the Word Basic Document Framework to guide Word.Basic's correct positioning in the basic framework, and these bookmarks add all the fields of only one-line body in the basic document frame. Since Word.Basic is output in the order in which the subsequent record is output, the output of output fields in the first table is output, so that the correctness of the subsequent line output can be guaranteed. For convenience, the book signature is defined as the same name with the field name. During the actual data output, since we allow any changes to the output page, it is difficult to ensure that the number of rows in each page is the same for optimizing the output page. In order to make the header of the output page correctly print the header of each output page, set the header of the table to "Title Allow" (first selected to set it to the header allowed, and then use "table" The "Title" command in the menu is set). In order to make the height of the output page, "Cell Height and Width" dialog box in the "Table" menu can be used to select the "Allow Cross-page Broken" option, and of course, it is also possible to use the word.basic's tabler owheight command to "allow transplitive cross-page break". Set.
In addition, when defining the basic document framework, it is not necessary to consider that the total table width should be enlarged as the size of the field and the proportional relationship between the fields, only need to define the width of each field as the minimum width you want. The specific purpose will be explained in the main algorithm of the following database output.
(2) Main algorithms for generating Word documents
When using word.basic to output a database to a Word document, you need to program the page of the output report and the width of each field, which describes the adjustment policies and main algorithms used in the programming.
Due to the randomness of the data in the database, in particular the unprocal characteristic and the length of the note data, the proportional relationship between each field width in the table is difficult to predetermine, and the adjustment is required after the entire report generation. The basic policy of adjustment is to output the specific content of the fields to be output in each record to the table, and write down the maximum width (character number) of each column during the output, use it as a column Basis of bit width ratio relationship. If adjustments are performed completely according to this basis, the body portion must be equipped, but the proof of the head portion portion may change (such as the "Take an Action" header, due to the content of the field in the database, may be very small When the field width adjustment is performed, it is possible to make the head text into multi-line arrangements), and the overall effect of the report is still poor. So it is necessary to know the minimum width of each column in advance to prevent the header from being arranged in multiple lines of the header when each field width is adjusted. To this end, the width of each field in the conventional document frame is the minimum width, and the field width adjustment is only performed on the character type and the note field.
Based on the above adjustment strategy, we introduce the corresponding algorithm. For ease of introduction, we assume that the settings of the page and the margins are available to widen the width of the table to NWIDTH inches and define the following data type:
Local AfieldName [NoutColumn, 6]
among them:
NOUTCOLUMN is the number of fields to output fields and the maximum number of columns of the table;
AfiledName [NoutColumn, 1] is a book signature;
AfiledName [NoutColumn, 2] for the field type of the bookmark where the bookmark is in the field;
AfiledName [NoutColumn, 3] For the field width of the field corresponding field of the bookmark;
AfiledName [NOUTCOLUMN, 4] For the number of fragments of the bookmarks corresponding to the field;
AfiledName [NoutColumn, 5] minimum TWIP number for this bookmark; (1 inch = 1440 TWIP)
AfiledName [NOUTCOLUMN, 6] For the maximum number of characters in the column where the bookmark is located.
Thus, after calculating the maximum number of characters of each field and the minimum TWIP number of each field, the number of total characters that need to be adjusted and the number of widths available for use can be calculated.
Assume that the total number of widths available is J (TWIP), you need to adjust the number of characters (characters), and the width to adjust the field should be: J / K * AfieldName [i, 6] The number of the tuning column in the table. However, this width may be less than the minimum width of the field, so that the field should not be called, the type of type A fieldName [i, 2] of this column needs to be modified to "U" (non-C and not M) In order to configure the column of non-adjustment, but also modify J and K accordingly, and then check it from the first column to see if there is still such a column, if there is a need to repeat the above steps. Until the column of the above type is no longer. Finally, use the TableColumnWidth command to adjust the relics that need to be adjusted. Third, the process of generating a Word document implementation According to the above adjustment policies and algorithms, we give us a general program that outputs a Word document from the VFP database using VFP. This procedure does not rely on specific database structures, nor does it depend on specific report output formats, which can be used for Word document output for any VFP database.
Local Noldrecno, OleWord, i, J, K, NoutColumn, CString
Local AfieldList [1]
Use b1
If EOF () && no output results return directly
Return
ENDIF
= Afields (Afieldlist) && deposits the current database in the array
noldrecno = recno ()
* Let's first generate a word.basic object, then use word.basic to open document commands
Open basic document framework
OleWord = CreateObject ("word.basic")
OleWord.FileOpen ("c: /mf/data/word.doc")
* The following is the first movement of the headlights from the Word document until moving to the form
Said.
* The reason for this is that the top of the basic document frame can have additional content.
Do while oleword.selinfo (12) # - 1
Oleword.Linedown
Enddo
* The following is whether the number of checkscripts is the same as the maximum number of tables.
NOUTCOLUMN = Oleword.countbookmarks ()
If NOUTCOLUMN # oleword.selinfo (18)
OleWord.AppClose ("Microsoft Word")
= MessageBox ("The number of bookmarks and tables", 16, "prompt information")
Return
ENDIF
* The following is the array of definitions, and fill in the first 4 columns of the array
Local AfieldName [NoutColumn, 6]
* - AfieldName Significance: 1 bookmark, 2 field type, 3 field width, 4, small
Number,
* - 5 Minimum TWIP number of each column width in the original table
* - 6 Most of the characters in all rows listed in the bookmark during the specific filling process
Large number
For i = 1 to NoutColumn
AfieldName [i, 1] = oleword.bookmarkname (i)
For j = 1 to fcount () 1
IF j = fcount () 1
OleWord.AppClose ("Microsoft Word")
= MessageBox ("Tagname:" AfieldName [i, 1];
"Not the field name", 16, "
Tip information ")
Return
ENDIF
IF Upper (AfieldName [i, 1]) == Upper (allt (Afieldlis
T [j, 1]))))
AfieldName [i, 2] = AfieldList [J, 2] && type
AfieldName [i, 3] = AfieldList [J, 3] && Width
AfieldName [I, 4] = AfieldList [J, 4] && Digital Exit
ENDIF
ENDFOR
ENDFOR
* Move the insertion point to the bottom rightmostmost end of the table
Do while oleword.selinfo (12) = - 1
Oleword.Linedown
Enddo && Press the arrow to remove the table down
Do while oleword.selinfo (12) # - 1
Oleword.charleft
Enddo && moves to the end of the last column of the left arrow
IF oleword.selinfo (16) # oleword.selinfo (18) 1
OleWord.AppClose ("Microsoft Word")
= MessageBox ("Failed to the end of the final column of the table", 16, "prompt information")
Return
ENDIF
* - The width of each column in the original table is calculated below, that is, the minimum width of the column (with TWIP)
unit)
J = oleword.selInfo (5)
For i = noutcolumn to 1 step -1
Oleword.pRevcell
K = oleword.selinfo (5)
AfieldName [i, 5] = j-k
AfieldName [i, 6] = 0
J = K
ENDFOR
IF oleword.selinfo (16) # 1
OleWord.AppClose ("Microsoft Word")
= MessageBox ("Last not to the first column of the table column", 16, "prompt information")
Return
ENDIF
* The minimum width calculation of each column is completed
* The table below fills in the specific content.
* Record the maximum number of characters in each column to AFIELDNAME when filling out the specific content [
I, 6]
Locate
Oleword.editgoto ((AfieldName [1,1])))
Oleword.prevcell ()
Do While! EOF ()
For i = 1 to NoutColumn
Do Case
Case AfieldName [i, 2] == "n"
CString = STR (& AfieldName [i, 1], AfieldName [i, 3],
AfieldName [i, 4])
Case AfieldName [i, 2] == "i"
CString = Allt (Str (& AfieldName [i, 1], 10, 0]))
Case AfieldName [I, 2] == "C" OR AFIELDNAME [I, 2] == "M"
CString = Trim (& AfieldName [i, 1])
If len (cstring)> AfieldName [i, 6]
AfieldName [i, 6] = len (cstring)
ENDIF
Otherwise
CString = "Temporary Type"
Endcase
Oleword.nextCell ()
Oleword.insert ((cstring) && characters must be applied ()
ENDFOR
Skip
Enddo
* - Form content is completed
* The width of some columns in the table is re-adjusted according to the specific content filled in.
* (In the following calculation process, unit is TWIP)
J = nwidth * 1440 && nwidth is a global variable in inches.
&& 1 inch = 1440TWIP
K = 0
For i = 1 to NoutColumn
IF AfieldName [i, 2] # "c" and afieldname [i, 2] # "m" j = j-AfieldName [i, 5] && remaining available width
Else
K = K AfieldName [i, 6] && needs to adjust the maximum width
The sum of the characters
ENDIF
Endfor && minus the width of the prohibition
For i = 1 to NoutColumn
IF AfieldName [i, 2] = "c" or afieldname [i, 2] = "m"
* If the width to be adjusted is less than or equal to the minimum width, it is included in the non-adjusted column.
IF J / K * AfieldName [i, 6] <= AfieldName [i, 5]
AfieldName [i, 2] = "u"
J = J-AfieldName [i, 5]
K = K-AfieldName [i, 6]
i = 1 && from the head cycle
Loop
ENDIF
ENDIF
ENDFOR
For i = 1 to NoutColumn
IF AfieldName [i, 2] = "c" or afieldname [i, 2] = "m"
OleWord.editgoto ((AfieldName [i, 1])))
Oleword.tablesElectrColumn
Oleword.TableColumnWidth (J / K * AfieldName [i, 6] / 20, 2,
0, 0, 0, 0)
* 1 pound = 20TWIP, the default unit of TableColumn () is pound
ENDIF
ENDFOR
* Width adjustment is completed
Oleword.filesaves ("c: /mf/data/word2.doc")
* oleword.fileprint (0, 0, "", "," ", 0, 1," ", 0, 0, 0," ")
OleWord.AppClose ("Microsoft Word")
Wait "Uninstalling Word Basic" Window Nowait
WAIT CLEAR
Return
One way to generate a Word document with the database content is the shape of the document
Save, another method is not only to save the document, but also print, at this time
It is not possible to note that it cannot be printed in the background, the above procedures - Olwword.fil
EPRINT (0, 0, "", "" "" "", 0, 1, "" "," * "before the appearance" * "
.