Keywords: Delphi; Word; Excel; Report;
1. introduction
When developing applications using Delphi, we usually have to design many documents and reports in various formats, and we often need to create, edit and modify files. Although Delphi itself provides many controls or methods for designing reports, there are obvious The shortcomings, such as Chinese input and data format control, are more troublesome to operate. Using the Office tools that everyone is familiar with can solve this problem well. The following is a detailed introduction to how to use Office to complete the production of various documents and reports.
2. Implementation method
There are three main methods for developing using Delphi and Office:
(1) Embed Office through Delphi's control TOleContainer. This is the easiest Ole embedding. It can directly call Office documents. You only need to use ToleContainer.Run to directly start the Office document. Moreover, the Office document launched in this way is a whole (from the interface), but it has an insurmountable disadvantage, that is, the Office document cannot be controlled through Delphi, and the purpose of flexibly manipulating Office cannot be achieved.
(2) Use the Servers control provided by Delphi to call Office, use the properties of Office, and use the Servers control of Delphi to manipulate Office. Delphi can implement code prompts during programming. Overall, it can better implement Delphi's control of Office, but There are also some Office functions that cannot be called in Delphi (such as VBA macro code written by yourself). And when implementing the function, the parameters that can be selected in the VBA code must be added when calling Delphi, otherwise, even the compilation cannot be passed. Office and Delphi programs started in this way belong to two forms.
(3) Using CreateOleObject will start Office and then control Office in Ole mode. This method is to call Office in CreateOleObjects, which is actually Ole, but this method can truly achieve complete control of Office files and use all the properties of Office, including VBA macro code written by yourself. Compared with Servers control and Com technology, this method can truly use various properties of Office. It is basically the same as writing your own code in VBA, and the default code does not need to be used.
3 . Software implementation
3.1 The first method: Embed Office through Delphi's control TOleContainer. The operation method is as follows:
(1) Call the System page control ToleContainer implementation in Delphi;
(2) Double-click OleContainer and the following interface appears:
(3) At this time, you can make a choice, select "Create from File", select the corresponding document, and simply implement the function of adjusting the OFFICE document; the following figure:
3.2 The second method: use the Servers control provided by Delphi to call Office; it makes it easy for us to control applications in Office (Word, Excel, Powerpoint, Outlook and access, etc.) as a com application server. Taking Word as an example, introduce its implementation method:
(1) Call the TwoDocument (create Word file object), TwoDocument (start Word and establish a connection with Word), TwoDoFont (set the font of the Word file), and TwoDParagraphFormat (set the paragraph format in the Word file);
(2) Start Word and implement it through TwitterApplication;
Try
Wordapplication.Connect;
Except
MessageDlg('Word may not be installed', mtError, [mbOk], 0);
Abort;
End;
(3) Create a new file and implement it through TwitterApplication;
Template := EmptyParam; // means that the template is not used
NewTemplate := False; // means that the type of new document is document.
// Add a new document
WordApplication.Documents.Add(Template, NewTemplate); WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex ));
//Close pinyin search and grammar search to improve the efficiency of program operation
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
(3) Insert data and implement it through the TwitterDocument;
//text
WordDocument.Range.InsertAfter('Oracle Database'+chr(#13));
// sheet
WordDocument1.Tables.Add(WordDocument1.Range,rownum,colnum,template,newtemplate); // where rownum is the number of table rows and colnum is the number of table columns
// Insert data into the table
WordDocument1.Tables.Item(1).Cell(1,1).Range.Text:='Certificate number';
WordDocument1.Tables.Item(1).Cell(1,2).Range.Text:='ID number'
(4) Format settings
WordFont.ConnectTo(WordDocument.Sentences.GetLast.Font);//Set certain texts
WordFont.ConnectTo(WordDocument.Paragraphs.GetLast.Font);//Set a certain text
WordFont.Bold:=1;
WordFont.Italic:=1;……………………
WordDocument1.PageSetup.HeaderDistance:=1.5; //Set page margins
WordDocument1.PageSetup.FooterDistance:=1.75;
//Set the header and footer
WordDocument1.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageFooter; WordDocument1.ActiveWindow.ActivePane.Selection.InsertAfter('Thread'+inttostr(wdFieldNumPages)+'Page');
//Let the footer bend to the right
WordDocument1.ActiveWindow.ActivePane.Selection.ParagraphFormat.Alignment:=wdAlignParagraphRight;
……………………………………………………………………
3.3 The third method: Use CreateOleObject to start Office, and then control Office in Ole mode. Take Excel tables as an example (the format of the table can be set in advance or defined by yourself. Here we take the format of the table set in advance as an example), explain it:
(1) Add Comobj class in Uses;
(2) Apply for a global variable: FvExcel, type Variant; indicates that it is an Excel object
(3) Define a function to start Excel, such as define function OpenExcel(strFileName: string): Boolean;
Function Tform1. OpenExcel(strFileName: string) : Boolean;
Begin
Result := True;
try
FvExcel := CreateOleObject('Excel.Application');
except
Application.MessageBox('Enabled Excel to be opened',PChar(Application.Title),MB_ICONERROR);
Result := False;
end;
FvExcel.Visible := True;
FvExcel.Caption := '';
FvExcel.WorkBooks.Open(strFileName); // Open the workbook
FvExcel.WorkSheets[1].Activate; // Set the first worksheet as the activity worksheet
end;
(4) Insert data into FvExcel
FvExcel.cells[row,col].value:='China' ;//row represents row, col represents column;
(5) Set the format
fvexcel.rows[row].font.color:=clred; // Set the color of a certain row of data
fvexcel.ActiveSheet.PageSetup.LeftMargin:=1; // Set the left page margin
fvexcel.ActiveSheet.PageSetup. RightMargin:=1; // Set right page margin
fvexcel.ActiveSheet.PageSetup.Zoom:=100; // Set the display ratio
fvexcel.ActiveSheet.PageSetup.PaperSize:= xlPaperA4; // Set the printing paper size
4 . Some experiences
During the programming process, we often need to set the format of Office documents and tables. At this time, we need to use many attributes, methods and functions. These attributes, methods and functions are usually rarely used, if we don't have them. It is difficult to find these things in the VBA manual, so the better way is to first open OFFICE, make the format you want to design in advance, record the macros in OFFICE, and then open the Visual Basic editor in OFFICE, and view the code. You can see the properties, methods or functions used by your operations. Usually, many properties, functions or methods can be directly used in Delphi programs, and some of them can be used in Delphi as long as they are slightly modified.
5 . Conclusion
Delphi can be perfectly combined with Office, and various styles of tables, texts, slides, etc. are designed. At the same time, through Delphi, the required data is inserted into the appropriate location of Office, and the functions of Office itself are previewed, printed and saved, thereby reducing the time for program debugging and positioning debugging, simplifying the difficulty of programming, and reducing programmers' Workload. For users, using familiar Office increases system operability. This idea has been widely used in the "Professional Title Review Management Information System".