六、报表功能
1、在报表功能中创建报表
(1)在类向导中添加类->From a type library,找到word所在的路径,添加msword.olb,根据需要添加所需的类,一般需要添加_Application、_Document、Documents、Range、Cell、_Bookmark、Bookmarks等。
(2)在word中设计一个模板,命名为template.dot,保存。在需要添加内容的地方加上书签。
(3)在report窗口中添加一个按钮,作为生成报表用(本来想在打开report窗口的时候直接生成,在OnCreate函数中加代码,但是运行的时候老是卡死,后来发现是RichEdit控件没有在InitInstance中初始化的缘故,去掉就好了。后来又觉得在打开窗口的时候生成也不太好,这样在使用中每次切换窗口都会生成一次报告,用起来比较慢,也没有必要,所以还是保留了生成报表按钮)。
(4)在生成报表按钮的相应函数中添加以下代码:
COleVariant covZero((short)0),
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR),
covDocxType((short)0),
start_line, end_line,
dot(_T("C:\\template.dot"));
_Application wordApp;
Documents docs;
_Document docx;
Bookmarks bookmarks;
Bookmark bookmark;
Range range;
Cell cell;
if (!wordApp.CreateDispatch(_T("Word.Application")))
{
AfxMessageBox(_T("本机没有安装word产品!"));
return;
}
wordApp.SetVisible(FALSE);
CString wordVersion = wordApp.GetVersion();
docs = wordApp.GetDocuments();
docx = docs.Add(dot, covOptional, covOptional, covOptional);
bookmarks = docx.GetBookmarks();
bookmark = bookmarks.Item(&_variant_t(_T("name")));
range = bookmark.GetRange();
range.SetText(_T("李盟"));
CString strSavePath = _T("C:");
strSavePath += _T("\\test.rtf");
docx.SaveAs(COleVariant(strSavePath), covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
// 退出word应用
docx.Close(covFalse, covOptional, covOptional);
wordApp.Quit(covOptional, covOptional, covOptional);
range.ReleaseDispatch();
bookmarks.ReleaseDispatch();
wordApp.ReleaseDispatch();
AfxMessageBox(_T("报告生成成功!"));
因为程序中的路径有在C盘根目录下的,所以运行的时候需要用管理员身份。目前可以将字符串插入到文档中指定书签的位置。生成的文档如下:
2、显示报告
(1)在report视图中点右键->ActiveX控件,加入Microsoft Web Browser控件。在CFormViewReport类中为该控件定义成员变量m_report。
(2)在CMainFrame类中定义菜单“打印报表”的消息响应函数:
void CMainFrame::OnReportPrint()
{
// TODO: Add your command handler code here
m_wndSplitter2.DeleteView(0,1);
m_wndSplitter2.CreateView(0,1,RUNTIME_CLASS(CFormViewReport),CSize(300,400),NULL);
m_wndSplitter2.RecalcLayout();
CRect crt;
CCreateContext Cnt;
GetClientRect(crt);
Cnt.m_pCurrentDoc = NULL;
int top=crt.top+150;
int bottom=crt.bottom-50;
int left=crt.left+250;
int right=crt.right-50;
CRect crt_report=CRect(left, top, right, bottom);
m_pViewReport = new CFormViewReport;
m_pViewReport->m_report.Create(NULL, WS_CHILD | WS_VISIBLE, crt, this, 0x0006);
try
{
// m_pViewReport->UpdateData(true);
::CoInitialize(NULL);
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pConnection->Open("DSN=TQES_DATABASE","","",0);//上面四行为打开数据源连接
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败!");
}
try
{
_bstr_t ha = "select * from REPORT";
m_pRecordset = m_pConnection->Execute(ha, NULL, adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("打开表REPORT失败!");
}
CString name=(char*)(_bstr_t)m_pRecordset->GetCollect("USER_NAME");
}
点击菜单项时连接数据库,并创建视图。
(3)在CFormViewReport类的成员函数OnCreate中加入:
int CFormViewReport::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFormView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
//创建文件
COleVariant covZero((short)0),
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR),
covDocxType((short)0),
start_line, end_line,
dot(_T("E:\\template.dot"));
_Application wordApp;
Documents docs;
_Document docx;
Bookmarks bookmarks;
Bookmark bookmark;
Range range;
Cell cell;
if (!wordApp.CreateDispatch(_T("Word.Application")))
{
AfxMessageBox(_T("本机没有安装word产品!"));
return 0;
}
wordApp.SetVisible(FALSE);
CString wordVersion = wordApp.GetVersion();
docs = wordApp.GetDocuments();
docx = docs.Add(dot, covOptional, covOptional, covOptional);
bookmarks = docx.GetBookmarks();
bookmark = bookmarks.Item(&_variant_t(_T("name")));
range = bookmark.GetRange();
range.SetText(_T(m_Name));
bookmark = bookmarks.Item(&_variant_t(_T("id")));
range = bookmark.GetRange();
range.SetText(_T(m_Userid));
CString strSavePath = _T("E:");
strSavePath += _T("\\test.rtf");
docx.SaveAs(COleVariant(strSavePath), covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
// 退出word应用
docx.Close(covFalse, covOptional, covOptional);
wordApp.Quit(covOptional, covOptional, covOptional);
range.ReleaseDispatch();
bookmarks.ReleaseDispatch();
wordApp.ReleaseDispatch();
AfxMessageBox(_T("报告生成成功!"));
CRect crt;
CCreateContext Cnt;
GetClientRect(crt);
Cnt.m_pCurrentDoc = NULL;
m_report.Create(NULL, WS_CHILD | WS_VISIBLE, crt, this, 0x0006);
m_report.Navigate("E:\\test.rtf",NULL,NULL,NULL,NULL);
m_report.SetFullScreen(true);
return 0;
}
这个地方原来是用按钮实现的,但是感觉效果不是很好(主要是按钮画面太简单),所以改成在OnCreate中直接生成和显示报告。这样的缺点是在点击“报告”菜单之后会卡顿一会儿,然后才显示出报告视图,而且每次切换视图的时候都会重新连接数据库和重新生成文档。不过实现比较简单。而且Web Browser控件自带打印保存等按钮,就不需要再另外做了。效果如下:
表格中具体的项目都只做了书签,通过查询数据库中的数据对表格进行填充即可。其余的操作是ACCESS数据库内的问题,在此不做赘述。