Software development of students' comprehensive quality and ability evaluation system based on MFC and ACCESS (6-report function)

Posted by centipede on Thu, 10 Feb 2022 20:54:18 +0100

Note: the software involved in this article has applied for software copyright. Please do not pirate. Infringement must be investigated.

6, Report function

1. Create report in report function

(1) Add the class - > from a type library in the class wizard, find the path where word is located, and add msword OLB, add the required classes as needed. Generally, you need to add_ Application,_ Document,Documents,Range,Cell,_ Bookmark s, Bookmarks, etc.
(2) Design a template in word and name it template Dot, save. Bookmark where you need to add content.
(3) Add a button in the report window, For report generation (originally, I wanted to generate it directly when opening the report window and add code to the OnCreate function, but it was always stuck when running. Later, it was found that the RichEdit control was not initialized in InitInstance, so it was better to remove it. Later, I felt that it was not good to generate it when opening the window, so every time I switch the window in use, a report will be generated and used It is relatively slow and unnecessary, so the generate report button is retained).
(4) Add the following code to the corresponding function of the generate report button:

	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("This machine is not installed word Products!"));
      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("Li Meng"));
  
  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);
 
  // Exit word application
  docx.Close(covFalse, covOptional, covOptional);
  wordApp.Quit(covOptional, covOptional, covOptional);
  range.ReleaseDispatch();
  bookmarks.ReleaseDispatch();
  wordApp.ReleaseDispatch();
 
  AfxMessageBox(_T("Report generation succeeded!"));

Because the path in the program is under the root directory of Disk C, you need to use the administrator identity when running. At present, you can insert a string into the document to specify the location of the bookmark. The generated documents are as follows:

2. Display Report

(1) Right click - > ActiveX control in the report view to add Microsoft Web Browser control. Define the member variable m for the control in the CFormViewReport class_ report.
(2) Define the message response function of the menu print report in CMainFrame class:

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);//The above four lines open the data source connection
	}
	catch(_com_error e)
	{
		AfxMessageBox("Database connection failed!");
	}
	try
	{
  	_bstr_t ha = "select * from REPORT"; 
	m_pRecordset = m_pConnection->Execute(ha, NULL, adCmdText);
	}
	catch(_com_error e)
	{
		AfxMessageBox("Open table REPORT fail!");
	}
	CString name=(char*)(_bstr_t)m_pRecordset->GetCollect("USER_NAME");
}

When you click the menu item, connect to the database and create a view.
(3) In the member function OnCreate of CFormViewReport class, add:

int CFormViewReport::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	//create a file
	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("This machine is not installed word Products!"));
      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);
 
  // Exit word application
  docx.Close(covFalse, covOptional, covOptional);
  wordApp.Quit(covOptional, covOptional, covOptional);
  range.ReleaseDispatch();
  bookmarks.ReleaseDispatch();
  wordApp.ReleaseDispatch();
 
  AfxMessageBox(_T("Report generation succeeded!"));

  	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;
}

This place was originally realized with buttons, but the effect is not very good (mainly because the button screen is too simple), so it is changed to directly generate and display reports in OnCreate. The disadvantage is that the report view will be displayed after clicking the "report" menu for a while, and the database will be reconnected and the document will be regenerated every time the view is switched. However, the implementation is relatively simple. Moreover, the Web Browser control comes with buttons such as print and save, so you don't need to do anything else. The effect is as follows:

The specific items in the table are only bookmarked, and the table can be filled by querying the data in the database. Other operations are problems in the ACCESS database and will not be repeated here.

Topics: MFC access