水晶报表在VC++6.0中的简单使用方法

本文介绍如何在VC++6.0环境下利用水晶报表进行报表设计和开发,包括导入动态联接库、定义接口指针变量、实例化应用、获取报告变量等关键步骤,实现报表的生成与展示。

水晶报表是一个报表设计开发的强大工具,功能强大,设计灵活,在水晶报表光盘中只提供了一个完全动态生成报表的例子,使用繁琐。现介绍其在VC++6.0中的简单使用方法。编译环境:VC++6.0 sp5 、Windows 2000 Server sp3 (en)。一、导入水晶报表使用的动态联接库:根据实际修改文件路径。

1. #import "C:\Documents and Settings\Administrator\桌面\cr\craxdrt9.dll" no_namespace

二、定义接口指针变量

1. //水晶报表变量
2. IApplicationPtr m_Application;
3.      IReportPtr m_Report;
4. //水晶报表控件变量,在对话框中加入该控件
5.     CCrystalReportViewer9    m_CRViewer1;

三、具体实现步骤

01.   //实例化m_Application
02.     m_Application.CreateInstance (__uuidof(Application));
03. //获取m_Report变量
04. //staff.rpt为通过向导建立的报表文件,数据库采用SQL Server 7.0
05.    m_Report =m_Application->OpenReport ("C:\\Documents and Settings\\Administrator\\桌面\\cr\\debug\\staff.rpt");
06.   //设置报表标题
07.    m_Report->put_ReportTitle (_bstr_t("Title"));
08.   //设置数据库连接变量
09. //数据库服务器(local),数据库名staff,用户名sa,密码sa
10.    m_Report->Database ->Tables ->Item [1]->SetLogOnInfo("(local)","staff","sa","sa");
11. //设置检索SQL命令
12. m_Report->put_SQLQueryString ((_bstr_t)"select * from person where id<'4' order by id");
13. //不显示重复字段
14.    m_Report->PutEnableSelectDistinctRecords (TRUE);
15.    //设置检索条件,采用水晶报表语法,功能同设置检索SQL命令
16. m_Report->PutRecordSelectionFormula ((_bstr_t)"{person.id}='1'");
17. //设置报表作者
18.    m_Report->PutReportAuthor ("xiaojin");
19.    //将m_Report与报表控件连接
20.    m_CRViewer1.SetReportSource(m_Report);
21. //显示报表工具条
22.    m_CRViewer1.SetDisplayToolbar (TRUE);
23. //不显示报表左边的分组树
24.    m_CRViewer1.SetDisplayGroupTree (FALSE);
25.    //不显示控件边框
26.    m_CRViewer1.SetDisplayBorder (FALSE);
27.    //刷新数据
28.    m_CRViewer1.Refresh ();
29. //显示报表内容
30.    m_CRViewer1.ViewReport();

这样,漂亮的水晶报表就显示出来了。程序异常处理部分省略,请自行加入。

摘要:当用户需要对报表需要重新编辑的时候,用Excel或者Word生成报表就会比XML或者HTML的方式更加合理。但是由于Word的组件在使用时候要导入很多类,如果直接加在工程中会使其看上去很乱,所以封装到DLL中是个不错的主意。本文在前人经验指导下对一些常用的操作做了简单的封装,功能由从DLL中导出类CWordOperate提供。 关键字:Word2003,MFC扩展DLL,报表,CWordOperate 一、 鉴于www.vckbase.com中已经有文章对vc使用word生成报表方式做详细的论述,这里不准备对此赘述。仅仅介绍封装类CWordOperate中的函数和DLL的使用。#include "msword.h"class AFX_EXT_CLASS CWordOperate {public: CWordOperate(); virtual ~CWordOperate(); BOOL CreateWordApp(BOOL bShow); // 创建一个word应用程序 void DeleteApp(); // 用后,删除 void ShowApp(BOOL bShow); // 显示,或者隐藏 void QuitApp(); // 关闭外部打开的word应用程序 BOOL NewDoc(CString tpName); // 根据模板创建文档 void SaveDoc(CString Filename); // 保存文档到文件路径 void TypeText(CString Text); // 在当前位置输入文字 void SetTextAlign(int val); // 设置文字对齐方式 void SetTextColor(COLORREF cr); // 设置文本颜色 void AddPic(CString FileName); // 在当前位置插入图片文件 void RunMacro(CString macroName); // 运行一个宏 void AddTable(int nRow, int nCol); // 加一个几行几列的表格 void AddTable(int nRow, int nCol, int autoFit); // 加一个几行几列的表格,列宽度自动 void SelectTable(int i); // 选择当前文档中的第i个表格 void MoveToTableNext(int iTable); // 把选择区指向表格后面 void SetCellText(int iTable, int iRow, int iCol, CString text); // 设置第i个表格中的某行某列的文字 void SetTableTextVAlign(int iTable, int val); // 置第i个表格中文字对齐方式 void SetColWidth(int iTable, int iCol, float width); // 设置列宽度 void SetRowHeight(int iTable, int iRow, float height); // 设置行高度 // 合并两个单元格之间的区域 void MergeCell(int iTable, int cell1Row, int cell1Col, int cell2Row, int cell2Col, CString text); // 设置单元格文本颜色 void SetCellTextColor(int iTable, int iRow, int iCol, COLORREF cr); // 设置单元格背景颜色 void SetCellColor(int iTable, int iRow, int iCol, COLORREF crCell); // 设置单元格边框的四周风格, void SetCellBorderStyle(int iTale, int iRow, int iCol, int sTop, int sLeft, int sBottom, int sRight); // 设置两个单元格之间的区域,边框的四周风格, void SetCellsBorderStyle(int iTable, int iRow1, int iCol1, int iRow2, int iCol2,int sTop, int sLeft, int sBottom, int sRight); // 设置两个单元格之间的区域,背景颜色 void SetCellsColor(int iTable, int iRow1, int iCol1, int iRow2, int iCol2, COLORREF crCell); CString ToString(); protected: _Application app; // 应用程序 Selection sel; // 用来存放获得的选择范围 _Document saveDoc; // 当前活动文档 COleVariant vTrue; // 常量TRUE COleVariant vFalse; COleVariant vOpt;};二、使用DLL中导出类的方法:#include "WordOperate.h"#pragma comment(lib, "wordDll.lib")在类中定义成员: CWordOperate wordFile;使用示例:// 创建文件 CString str;COLORREF crCell = RGB(240, 240, 240);char buffer[255];wordFile.CreateWordApp(TRUE);GetCurrentDirectory(254, buffer);strcat(buffer, "\\tp.doc");wordFile.NewDoc(buffer); // 表格使用wordFile.AddTable(8, 11);wordFile.SelectTable(1);wordFile.MergeCell(iTable, 1, 6, 1, 8, "频域数据");crCell = RGB(255, 0, 0);wordFile.SetCellColor(iTable, 3, 2, crCell);wordFile.MoveToTableNext(1);// 写文字wordFile.TypeText("二. 分析图:\r\n");// 插入图片GetCurrentDirectory(254, buffer);strcat(buffer, "\\chart.jpg");wordFile.AddPic(buffer);// 保存文件,删除对象GetCurrentDirectory(254, buffer);strcat(buffer, "\\测试报表.doc");wordFile.SaveDoc(buffer);wordFile.DeleteApp();// 也可以在删除对象前先关闭程序wordFile.QuitApp();三、使用压缩包的测试程序,按钮二能提供的效果如下图:图一 Word报表效果屏幕截图四、说明:开发环境为:vc6 + sp6 + xp_sdkWord版本:2003企业版压缩包说明:wordDll为库的代码,TestDll为使用示例,宏.txt是格式化段落的宏附加:如果想知道函数中使用的值应该为多少,可以在Word操作的时候录制宏,然后使用单步调试的方式得到想要的值。注:本着开源的精神,把这个使用过的类和大家分享,希望大家有用,如果有错误的地方,希望指正,谢谢
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值