In our commonly used database applications, it is troublesome to write report programs, and it is inconvenient for users to modify the report programs in Delphi. For example, general data needs to add a header, and some of the data needs to be modified or several data needs to be changed. Combine the data from each table into a data set and then print it. These operations are powerless for the report program in Delphi, and most computers are equipped with office suites. Can we use the table processing capabilities and inherent capabilities of Excel? What about multiple printout capabilities? The answer is yes. We use the function of EXCEL to process DBASE files, as long as our database files are compatible with DBASE.
In the database desktop system in Delphi, create a new dbase IV of the Table table (only dabse files can be used, because EXCEL does not recognize files in other ways) file sample.dbf, which can create an index file. It should be noted here that in the database Only English fields can be entered in desktop. In fact, we only use database Desktop creates a file structure, and then you can modify it to Chinese in vfp. The initialization of data and the addition and subtraction of fields can be completed in vfp. You may ask why not directly create the data table file in vfp, because EXCEL does not recognize it at all. The data read out from the data table in vfp is messy.
After creating the data table sample.dbf, create a project in Delphi, and then select the data module module in file->new. The advantage of the established data module is that it may be common to all forms. Add the data source and data in the components of datamodule1 table, set related items, the databasename of the data table should be set to c: emp (the directory of your dbf file), set tablename to the data table you designed (sample.dbf), create a new button on form1, caption is edit and print, and add the following code to its onclick:
data.table1.close;//Close the current data table. If not closed, the data may be incomplete.
if fileexists(extractfiledir(application.exename)+'samplep.dbf') then DeleteFile(extractfiledir(application.exename)+'samplep.dbf');
//Determine whether the print data table samplep.dbf exists, and delete it if it exists.
copyfile(pchar(extractfiledir(application.exename)+'sample.dbf'),pchar(extractfiledir(application.exename)+'samplep.dbf'),true);
//Copy sample.dbf into the print data table samplep.dbf. samplep.dbf is special for editing and printing. If you use sample.dbf, EXCEL will prompt that sample.dbf is a read-only file. If you modify it, it will crash.
data.table1.open;//Open the current data table to make it active
shellexecute(0,'open','EXCEL.EXE',pchar(extractfiledir(application.exename)+'samplep.dbf'),pchar(extractfiledir(application.exename)),SW-SHOWMAXIMIZED);//Call excel and merge Read the samplep.dbf file into excel, and reference ShellAPI in the header file.
In this way, you can make use of the table function and multi-printing function of EXCEL, and you no longer have to design complicated report programs; I used this method in the invoice statistics system I wrote for my unit, and the users found it very convenient.