生活随笔
收集整理的這篇文章主要介紹了
基于MFC和ACCESS的学生综合素质能力评价系统软件开发(6-报表功能)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注:本文所涉及軟件已申請軟件著作權,請勿盜版,侵權必究。
文章目錄
六、報表功能
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
) ; 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 ( )
{ 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 {
:: 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 ; 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
) ; 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數據庫內的問題,在此不做贅述。
下一篇:無 上一篇:基于MFC和ACCESS的學生綜合素質能力評價系統軟件開發(5-主界面目錄)
總結
以上是生活随笔 為你收集整理的基于MFC和ACCESS的学生综合素质能力评价系统软件开发(6-报表功能) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。