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 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 sentence1;
vector sentence2;
sentence1.push_back(_T("parameter1 "));
sentence1.push_back(_T("