MFC创建Word文档,进行插入表格、图片操作

本文介绍如何在VS2013环境下,利用Office2013,通过MFC来创建Word文档,并实现表格和图片的插入。在数据处理完成后,程序会提示用户选择保存路径,可以选择保存为doc或PDF格式。
摘要由CSDN通过智能技术生成

VS2013+office2013实现Word文档的创建,以及实现插入表格、图片的操作,在数据保存完毕后会弹出窗口选择保存路径,可以保存成doc和PDF的格式。

首先在界面上添加一个保存按钮,然后添加点击事件响应函数OnBnClickedButton1(),函数里面调用报表生成函数OnMakeExport(),OnMakeExport()函数代码如下,对象的创建放到了CTest002Dlg.h文件里。

OLB文件下载链接 下载OLB文件实现MFC与office的连接。

BOOL CTest002Dlg::OnMakeExport()
{
	CoInitialize(NULL);
	CApplication wordApp;
	CDocuments wordDocs;
	CDocument0 wordDoc;
	CSelection wordSelection;
	CRange wordRange;
	CTables0 wordTables;
	CTable0 wordTable;
	CCell wordCell;
	CCells wordCells;
	//Paragraphs wordParagraphs;
	//Paragraph wordParagraph;
	CFont0 wordFont;
	CShading wordShading;
	CParagraph wordParagraph;
	CParagraphs wordParagraphs;
	//
	/*int nRow = 0;*/
	//
	vector<CString> picNAME, picLONGT, picLATI, picALTI, picPANEL, picHOT, picSerHOT, picNorHOT;
	_GetDefectInfo(picNAME, picLONGT, picLATI, picALTI, picPANEL, picHOT, picSerHOT, picNorHOT);
	COleVariant vTrue((short)TRUE),
		vFalse((short)FALSE),
		vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
	CComVariant defaultBehavior(1), AutoFitBehavior(0);

	CString defectNUM = DefPicNum;//error image count

	CString  PanelNUM = PanelNum;//error panel count

	CString  HotSpotNUM = HotSpotNum;//hotspot count

	CString  AllImgNUM = AllImgCount;//all image count

	CString  FlyDistance1 = FlyDistance;//fly distance

	CString  FlyTime1 = FlyTime;//fly time count

	CString  NorHotSpotsNUM = NorHotPNum;//normal hotspot count

	CString  SerHotSpotsNUM = SerHotPNum;//serious hotspot count
	// 	if (/*!AfxOleInit()*/CoInitialize(NULL) != S_OK)
	// 	{
	// 		AfxMessageBox(_T("Initialize COM context failed!"));
	// 		return FALSE;
	// 	}
	//创建word application实例
	if (!wordApp.CreateDispatch(_T("Word.Application"), NULL))
	{
		AfxMessageBox(_T("Microsoft office is not exist,please install Microsoft office2010 and newer version"));
		return FALSE;
	}
	//wordApp.SetVisible(TRUE);//make visible
	wordApp.put_Visible(TRUE);
	wordApp.Activate();//activate
	wordDocs = wordApp.get_Documents();
	wordApp.put_Visible(FALSE);
	//wordDocs = wordApp.GetDocuments();//get documents object
	//
	CString strDocTemplate;
	CString strPath;
	wchar_t szFullPath[_MAX_PATH];
	::GetModuleFileName(NULL, szFullPath, _MAX_PATH);
	strDocTemplate.Format(_T("%s"), szFullPath);
	int nPos = strDocTemplate.ReverseFind('""');
	strDocTemplate = strDocTemplate.Left(nPos);
	strPath = strDocTemplate;
	TRACE("%s/n", strDocTemplate);

	CComVariant tpl(_T("")), Visble, DocType(0), NewTemplate(false);
	wordDoc = wordDocs.Add(&tpl, &NewTemplate, &DocType, &Visble);//Add(&tpl, &NewTemplate, &DocType, &Visble); 
	wordSelection = wordApp.get_Selection();//.GetSelection()
	//wordTables.AttachDispatch(wordDoc.GetTables());
	wordTables = wordDoc.get_Tables();//.GetTables();

	//move to end of story
	wordSelection.EndOf(COleVariant((short)6), COleVariant((short)0));

	//wordApp.get_Version();
	wordApp.put_Visible(FALSE);


	// 	CWnd* pWnd = GetDlgItem(IDC_STATIONNAME);
	// 	CWnd* pWnd1 = GetDlgItem(IDC_STATIONSITE);
	// 	CWnd* pWnd2 = GetDlgItem(IDC_STATIONAREA);
	// 	CWnd* pWnd3 = GetDlgItem(IDC_STATIONALTITUDE);
	// 	CWnd* pWnd4 = GetDlgItem(IDC_STATIONCAPACITY);

	CWnd* pWnd8 = GetDlgItem(IDC_STATIONTEMP);
	CWnd* pWnd10 = GetDlgItem(IDC_STATIONFLYAREA);

	CString text, text0, text1, text2, text3, text4, text5, text6, text7, text8, text9, text10;
	text = "";
	// 	pWnd->GetWindowTextW(text0);//station name
	// 	pWnd1->GetWindowTextW(text1);//station address
	// 	pWnd2->GetWindowTextW(text2);//station area
	// 	pWnd3->GetWindowTextW(text3);//station altitude
	// 	pWnd4->GetWindowTextW(text4);//station size

	com1.GetWindowTextW(text0);//station name
	com2.GetWindowTextW(text1);//station address
	com3.GetWindowTextW(text2);//station area
	com4.GetWindowTextW(text3);//station altitude
	com5.GetWindowTextW(text4);//station size
	com6.GetWindowTextW(text5);//electric time
	int nSel;
	// 获取组合框控件的列表框中选中项的索引   
	nSel = combo.GetCurSel();
	// 根据选中项索引获取该项字符串   
	combo.GetLBText(nSel, text7);//weather
	bool result;
	pWnd8->GetWindowTextW(text8);//temperature

	int windSel;
	// 获取组合框控件的列表框中选中项的索引   
	windSel = combo3.GetCurSel();
	// 根据选中项索引获取该项字符串   
	combo3.GetLBText(windSel, text9);//wind speed

	pWnd10->GetWindowTextW(text10);//fly area

	vector<CString> sentence1;
	vector<CString> sentence2;

	sentence1.push_back(_T("parameter1     "));
	sentence1.push_back(_T("parameter2     "));

	sentence2.push_back(_T("parameter3    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值