1、有问题请留言;
2、没问题请点赞;
3、看连载请加群;
4、下源码请加群;
【开发工具】
1、C++Builder10.2.2tokyo
2、FMSoft_uniGUI_Complete_Professional_1.10.0build1462(正版)
3、FastReport5.6.2
本例子将详细教授如何通过FastReport的PDF导出功能实现数据库记录的打印,本教程的源代码清到我的QQ群共享文件A02-源代码里下载。
一、新建项目
1、新建项目,主目录d:\testPrint
2、将MainModule窗口上放置一个UniConnection数据库联接控件和OracleUniProvider数据库驱动控件。
对UniConnection进行属性设置。
3、在UniMainModule窗口的OnCreate事件中添加连接数据库代码
void __fastcall TUniMainModule::UniGUIMainModuleCreate(TObject *Sender)
{
String dbIp="127.0.0.1",dbPort="1521",dbSid="ORCL",dbUser="webframe",dbPass="webframe2018";
UniConnection1->Disconnect();
UniConnection1->ProviderName="Oracle";
UniConnection1->SpecificOptions->Add("direct=true");
//UniConnection1->SpecificOptions->Add("Charset=ZHS16GBK");
UniConnection1->Username=dbUser;
UniConnection1->Password=dbPass;
UniConnection1->Server=dbIp+":"+dbPort+":"+dbSid;
//ShowMessageN(UniConnection1->Server);
try
{
UniConnection1->Connect();
}
catch(...)
{
Uniguidialogs::ShowMessageN("连接数据库["+dbIp+"]失败!");
//FormMain->Close();
}
}
//---------------------------------------------------------------------------
4、在UniMainModule窗口的OnBrowserClose事件中添加断开数据库代码
void __fastcall TUniMainModule::UniGUIMainModuleBrowserClose(TObject *Sender)
{
UniConnection1->Close();
}
//---------------------------------------------------------------------------
5、在UniMainModule.h中定义rowId全局变量
//---------------------------------------------------------------------------
#ifndef MainModuleH
#define MainModuleH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <UniGUIMainModule.hpp>
#include "DBAccess.hpp"
#include "OracleUniProvider.hpp"
#include "Uni.hpp"
#include "UniProvider.hpp"
#include <Data.DB.hpp>
//---------------------------------------------------------------------------
class TUniMainModule : public TUniGUIMainModule
{
__published: // IDE-managed Components
TUniConnection *UniConnection1;
TOracleUniProvider *OracleUniProvider1;
void __fastcall UniGUIMainModuleCreate(TObject *Sender);
void __fastcall UniGUIMainModuleBrowserClose(TObject *Sender);
private: // User declarations
public: // User declarations
String rowId;
__fastcall TUniMainModule(TComponent* Owner, TComponent* AUniApplication);
};
//---------------------------------------------------------------------------
TUniMainModule *UniMainModule(void);
//---------------------------------------------------------------------------
#endif
6、包含FastReport的头文件和静态库目录
C:\Program Files (x86)\Embarcadero\Studio\19.0\FastReport\LibD25
C:\Program Files (x86)\Embarcadero\Studio\19.0\FastReport\Source
二、设置UniMainForm主窗口
1、包含MainModule窗口
//---------------------------------------------------------------------------
#include <vcl.h>
#include <uniGUIVars.hpp>
#pragma hdrstop
#include "Main.h"
#include "MainModule.h"
#include "Unit1.h"
2、main.h定义变量
class TMainForm : public TUniForm
{
__published: // IDE-managed Components
TUniBitBtn *UniBitBtn1;
TUniQuery *UniQuery1;
TUniDBGrid *UniDBGrid1;
TUniDataSource *UniDataSource1;
void __fastcall UniBitBtn1Click(TObject *Sender);
void __fastcall UniDBGrid1CellClick(TUniDBGridColumn *Column);
void __fastcall UniFormShow(TObject *Sender);
private: // User declarations
String mySql,rowId;
public: // User declarations
__fastcall TMainForm(TComponent* Owner);
};
//---------------------------------------------------------------------------
TMainForm *MainForm(void);
//---------------------------------------------------------------------------
#endif
3、摆放UniQuery,UniDBGrid,UniDataSource控件
UniDBGrid的DataSource指向UniDataSource1,UniDataSource1的DataSet指向UniQuery1,UniQuery1的connection指向UniMainModule.UniConnection1,
UniDBGrid1->Options->dgRowSelect=true
UniDBGrid1->Options->dgCheckSelect=true
4、UniDBGrid1添加OnCellClick事件
void __fastcall TMainForm::UniDBGrid1CellClick(TUniDBGridColumn *Column)
{
//点击表格选择记录
if(UniQuery1->RecNo<1)
{
return;
}
//取打印的行编号
UniMainModule()->rowId=UniQuery1->FieldByName("rowid")->AsString;
}
5、UniBitBtn1打印按钮事件
void __fastcall TMainForm::UniBitBtn1Click(TObject *Sender)
{
//打印
TUniForm1 *form1=new TUniForm1(UniApplication);
form1->ShowModal();
}
6、初始显示查询记录
void __fastcall TMainForm::UniFormShow(TObject *Sender)
{
//查询
UniQuery1->Close();
UniQuery1->SQL->Clear();
mySql="select work_id 申请编号,work_man 申请人,work_list 内部加班人,work_corp 外部加班人,";
mySql+="work_date1 计划开始时间,work_date2 计划结束时间,auth_team 审批部门,auth_name 审批人,";
mySql+="auth_date 审批时间,auth_stat 审批状态,work_reason 加班事由,work_date3 实际开始时间,";
mySql+="work_date4 实际结束时间, work_rest 酬劳方式,work_times 加班工资倍数,team_name 申请团队,part_name 申请部门,inst_name 申请机构,rowid";
mySql+=" from work_apply order by work_id desc";
UniQuery1->SQL->Add(mySql);
UniQuery1->Open();
UniDBGrid1->Columns->Items[0]->Width=100;
UniDBGrid1->Columns->Items[1]->Width=60;
UniDBGrid1->Columns->Items[2]->Width=150;
UniDBGrid1->Columns->Items[3]->Width=150;
UniDBGrid1->Columns->Items[4]->Width=110;
UniDBGrid1->Columns->Items[5]->Width=110;
UniDBGrid1->Columns->Items[6]->Width=80;
UniDBGrid1->Columns->Items[7]->Width=60;
UniDBGrid1->Columns->Items[8]->Width=110;
UniDBGrid1->Columns->Items[9]->Width=80;
UniDBGrid1->Columns->Items[10]->Width=200;
UniDBGrid1->Columns->Items[11]->Width=100;
UniDBGrid1->Columns->Items[12]->Width=100;
UniDBGrid1->Columns->Items[13]->Width=100;
UniDBGrid1->Columns->Items[14]->Width=100;
UniDBGrid1->Columns->Items[15]->Width=100;
UniDBGrid1->Columns->Items[16]->Width=100;
UniDBGrid1->Columns->Items[17]->Width=100;
UniMainModule()->rowId="";
}
//---------------------------------------------------------------------------
三、新建一个打印窗口
1、在新建窗口上放置UniPDFFrame1控件用于显示生成的PDF文件和自动出现打印机选择和打印按钮。
frxReport1用于配置打印页面,frxDBDataset1用户关联打印模板和数据集,frxPDFExport1用于将打印页面frxReport1导出PDF文档。
frxBarCodeObject1是打印模板用到的条形码控件。UniQuery1用于查询要打印的数据。
UniQuery1-〉Connection=UniMainModule.UniConnection1
2、包含UniMainModule和UniServerModule头文件
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "MainModule.h"
#include "ServerModule.h"
3、双击frxReport1打开打印模板设计
逐个编辑对应的数据字段,比如申请编号,双击输入:
设计好打印模板后,将其保存到执行代码运行的同级目录下,D:\testPrint\Win32\Debug。
4、打印代码
在本打印窗口的BefroeShow事件中添加打印事件
void __fastcall TUniForm1::UniFormBeforeShow(TObject *Sender)
{
//打印
AnsiString rptFileName,mySql;
String url;
//
UniQuery1->Close();
UniQuery1->SQL->Clear();
mySql="select work_id 申请编号,work_man 申请人,work_list 内部加班人,work_corp 外部加班人,";
mySql+="work_date1 计划开始时间,work_date2 计划结束时间,auth_team 审批部门,auth_name 审批人,";
mySql+="auth_date 审批时间,auth_stat 审批状态,work_reason 加班事由,work_date3 实际开始时间,";
mySql+="work_date4 实际结束时间, work_rest 酬劳方式,work_times 加班工资倍数,team_name 申请团队,part_name 申请部门,inst_name 申请机构,rowid";
mySql+=" from work_apply where rowid=:w1";
UniQuery1->SQL->Add(mySql);
UniQuery1->ParamByName("w1")->Value=UniMainModule()->rowId;
UniQuery1->Open();
rptFileName="report_jiaban.fr3";
if(UniQuery1->RecordCount>0)
{
if(!FileExists(UniServerModule()->StartPath+rptFileName))
{
ShowMessage("【打印失败】打印模板文件"+rptFileName+"不存在!");
return;
}
frxReport1->Clear();
frxDBDataset1->UserName="frxDBDataset1";
frxReport1->LoadFromFile(UniServerModule()->StartPath+rptFileName);
frxReport1->DataSets->Clear();
frxDBDataset1->DataSet=UniQuery1;
frxReport1->DataSets->Add(frxDBDataset1);
frxReport1->PrintOptions->ShowDialog=false;
frxReport1->ShowProgress=true;
frxReport1->EngineOptions->SilentMode=true;
frxReport1->EngineOptions->EnableThreadSafe=true;
frxReport1->EngineOptions->DestroyForms=false;
frxReport1->EngineOptions->UseGlobalDataSetList=false;
frxPDFExport1->Background=true;
frxPDFExport1->ShowProgress=true;
frxPDFExport1->ShowDialog=false;
frxPDFExport1->FileName=UniServerModule()->NewCacheFileUrl(false,"pdf","","",url,true);
frxPDFExport1->DefaultPath="";
frxReport1->PreviewOptions->AllowEdit=false;
frxReport1->PrepareReport();
frxReport1->Export(frxPDFExport1);
UniPDFFrame1->PdfURL=url;
}
else
{
ShowMessage("没有可以打印的记录!");
}
}
四、ORACLE数据表结构
1、表结构
2、建用户代码
DROP USER WEBFRAME CASCADE;
CREATE USER WEBFRAME
IDENTIFIED BY <password>
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP
PROFILE DEFAULT
ACCOUNT UNLOCK;
-- 3 Roles for WEBFRAME
GRANT CONNECT TO WEBFRAME;
GRANT DBA TO WEBFRAME;
GRANT RESOURCE TO WEBFRAME;
ALTER USER WEBFRAME DEFAULT ROLE ALL;
-- 1 System Privilege for WEBFRAME
GRANT UNLIMITED TABLESPACE TO WEBFRAME;
3、建表代码
ALTER TABLE WEBFRAME.WORK_APPLY
DROP PRIMARY KEY CASCADE;
DROP TABLE WEBFRAME.WORK_APPLY CASCADE CONSTRAINTS;
CREATE TABLE WEBFRAME.WORK_APPLY
(
INST_NAME VARCHAR2(50 BYTE),
PART_NAME VARCHAR2(50 BYTE),
TEAM_NAME VARCHAR2(50 BYTE),
WORK_ID VARCHAR2(20 BYTE),
WORK_MAN VARCHAR2(20 BYTE),
WORK_LIST VARCHAR2(100 BYTE),
WORK_CORP VARCHAR2(200 BYTE),
WORK_DATE1 DATE,
WORK_DATE2 DATE,
WORK_DATE3 DATE,
WORK_DATE4 DATE,
WORK_REASON VARCHAR2(200 BYTE),
AUTH_TEAM VARCHAR2(50 BYTE),
AUTH_NAME VARCHAR2(20 BYTE),
AUTH_DATE DATE,
AUTH_STAT VARCHAR2(10 BYTE),
WORK_HOURS FLOAT(126),
WORK_TIMES FLOAT(126),
WORK_FEES FLOAT(126),
WORK_REST VARCHAR2(10 BYTE)
)
TABLESPACE USERS
RESULT_CACHE (MODE DEFAULT)
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
COMMENT ON TABLE WEBFRAME.WORK_APPLY IS '加班申请表';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.INST_NAME IS '机构';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.PART_NAME IS '部门';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.TEAM_NAME IS '团队';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_ID IS '编号';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_MAN IS '申请人';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_LIST IS '行内人员';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_CORP IS '行外人员';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_DATE1 IS '计划开始时间';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_DATE2 IS '计划结束时间';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_DATE3 IS '实际开始时间';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_DATE4 IS '实际结束时间';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_REASON IS '加班原因';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.AUTH_TEAM IS '审批团队';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.AUTH_NAME IS '审批人';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.AUTH_DATE IS '审批时间';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.AUTH_STAT IS '审批状态';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_HOURS IS '小时数';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_TIMES IS '加班费倍数';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_FEES IS '加班费';
COMMENT ON COLUMN WEBFRAME.WORK_APPLY.WORK_REST IS '调休、加班费';
CREATE UNIQUE INDEX WEBFRAME.WORK_APPLY_PK ON WEBFRAME.WORK_APPLY
(WORK_ID, WORK_DATE1, INST_NAME, PART_NAME, TEAM_NAME)
LOGGING
TABLESPACE USERS
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
NOPARALLEL;
ALTER TABLE WEBFRAME.WORK_APPLY ADD (
CONSTRAINT WORK_APPLY_PK
PRIMARY KEY
(WORK_ID, WORK_DATE1, INST_NAME, PART_NAME, TEAM_NAME)
USING INDEX WEBFRAME.WORK_APPLY_PK
ENABLE VALIDATE);
五、运行结果
http://127.0.0.1:8077
就要起飞了,时间好紧张,终于搞定.本文仅简单介绍了单条记录的打印,其实如何打印全权取决于UniQuery查询回来多少数据和FaseReport如何设置打印模板..,飞了,大连见。