www.pudn.com > 在VC中如何将数据导入 Word 中.rar > PrintInWord.cpp


// PrintInWord.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "useword.h" 
#include "PrintInWord.h" 
#include "wordconst.h" 
#include "comdef.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CPrintInWord 
 
CPrintInWord::CPrintInWord():m_InitPrint(false) 
{ 
} 
 
CPrintInWord::~CPrintInWord() 
{ 
 
} 
 
CPrintInWord::CPrintInWord(datanode * Node 
						   ,CString strShipName 
						   ,CString strShipName_En 
						   ,CString strDrawingName 
						   ,CString strSystemName 
						   ,CString strSystemName_En 
						   ,HWND hwnd 
						   ):m_NodeList(Node) 
{ 
	m_strShipName = strShipName; 
	m_strShipName_En = strShipName_En; 
	m_strDrawingName = strDrawingName; 
	m_strSystemName = strSystemName; 
	m_strSystemName_En = strSystemName_En; 
	m_dlgHwnd = hwnd; 
 
	m_strShipName ="110000DWT 成品油/原油兼用船"; 
	m_strShipName_En = "110000DWT PRODUCT/CRUDE OIL TANKER"; 
	m_strDrawingName ="DNS520-460-001MX"; 
	m_strSystemName ="燃油注入和驳运系统材料表 "; 
	m_strSystemName_En ="F.O FILLING&TRANSFER SYSTEM"; 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CPrintInWord message handlers 
 
//初始化Com口和Word应用 
//如果失败返回false;所有的打印程序都要被终止 
 
bool CPrintInWord::InitPrintSetting() 
{ 
    //初始化Com口, 
	if (::CoInitialize(0) != 0) 
	{ 
		AfxMessageBox("初始化COM库失败"); 
		//	::CoUninitialize(); 
		//	return false; 
	} 
	 
	//创建应用对象 
	if (!sApp.CreateDispatch("Word.Application", NULL)) 
	{ 
		AfxMessageBox("创建失败!", MB_OK | MB_SETFOREGROUND);  
		::CoUninitialize(); 
		return false; 
	} 
	return true; 
} 
 
bool CPrintInWord::BeginPrint() 
{ 
	try 
	{ 
		//初始化打印程序 
		::PostMessage(m_dlgHwnd, WM_INITPRINTING,NULL,NULL); 
		if (!InitPrintSetting()) 
			return false; 
		 
		//加载模版 
		::SendMessage(m_dlgHwnd, WM_ADDTEMPLATE,NULL,NULL); 
		if (!AddTemplate()) 
			return false; 
		 
		//打印文件头 
		::PostMessage(m_dlgHwnd, WM_PRINTHEADER,NULL,NULL); 
		if (!PrintHeader()) 
			return false; 
		 
		//打印阀件		 
		::PostMessage(m_dlgHwnd, WM_PRINTPARTONE,NULL,NULL); 
		if (!PrintBodyTypeOne()) 
			return false; 
			 
		//打印附件 
		::PostMessage(m_dlgHwnd, WM_PRINTPARTTWO,NULL,NULL); 
		if (!PrintBodyTypeTwo()) 
			return false; 
 
		//打印管件 
		::PostMessage(m_dlgHwnd, WM_PRINTPARTTHREE,NULL,NULL); 
		if (!PrintBodyTypeThree()) 
			return false; 
 
		//更改最后一个表头 
		AddTailHeader(); 
 
		//给文档添加页数 
		AddPageNumber(); 
		 
		//存盘 
		//::PostMessage(m_dlgHwnd, WM_SAVEDOCUMENT,NULL,NULL); 
		//需要发送CopyData消息把存储路径传到主窗口 
		 
		SaveDocument(); 
		 
		COPYDATASTRUCT CopyStr; 
		CopyStr.cbData = (DWORD)m_strFilePath.GetLength(); 
		CopyStr.dwData = (DWORD)1; 
		CopyStr.lpData = m_strFilePath.GetBuffer(m_strFilePath.GetLength()); 
		::SendMessage(m_dlgHwnd,WM_COPYDATA,NULL,(LPARAM)&CopyStr); 
		 
		//退出 
		::PostMessage(m_dlgHwnd, WM_EXITPRINT,NULL,NULL); 
		EndPrint(); 
		 
		::PostMessage(m_dlgHwnd, WM_LOADDOCUMENT, NULL, NULL); 
		 
		::PostMessage(m_dlgHwnd, WM_FINISHEDPRINT, NULL, NULL); 
	} 
	catch (CException e) 
	{ 
		CString str; 
		e.GetErrorMessage(str.GetBuffer(255),255); 
		str.ReleaseBuffer(); 
		AfxMessageBox(str); 
		EndPrint(); 
	} 
	 
	return true; 
} 
bool CPrintInWord::AddTemplate() 
{ 
	 
	CString strFileName; 
	strFileName = GetAppPath() +"\\Doc12.dot"; 
	 
	CFileFind filefind; 
	if(!filefind.FindFile(strFileName)) 
	{ 
		AfxMessageBox("没有找到模版文档,请其查找!"); 
		 
		CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"模版文档|*.dot||",NULL); 
		 
		if (IDOK == dlg.DoModal()) 
		{ 
			strFileName = dlg.GetPathName();  
		} 
		else 
		{ 
			return false; 
		} 
	} 
	 
 
	//从word对象中取得文档对象集合 
	sDocs=sApp.GetDocuments();	 
	sDocs.AttachDispatch(sApp.GetDocuments(),true); 
	COleVariant vFalse((long)0),vTrue((long)1);	 
	sDoc.AttachDispatch(sDocs.Add(COleVariant(_variant_t(strFileName)), vFalse,vFalse, vTrue)); 
	 
	return true; 
} 
 
//打印报表的头文件 
bool CPrintInWord::PrintHeader() 
{	 
	 
	//激活为页眉页脚 
	SetActiveView(wdSeekCurrentPageHeader); 
	sSel=sApp.GetSelection(); 
	 
	//船的名称 
	sSel.TypeText(m_strShipName + "\n" + m_strShipName_En); 
	sSel.MoveRight(vwdCell, vOne,vMove); 
	sSel.MoveRight(vwdCell, vOne,vMove); 
	//船的图号 
	sSel.TypeText(m_strDrawingName); 
	sSel.MoveRight(vwdCell, vOne,vMove); 
	//CString strPageNumber; 
	//总的页数 
	//strPageNumber.Format("共   %d 页TOTAL %d SHEETS",100,100); 
	//strPageNumber = "共   页 \nTOTAL SHEETS"; 
	 
	//sSel.TypeText(strPageNumber);	 
 
	sSel.MoveDown(vwdLine,vOne,vMove); 
	sSel.TypeText(m_strSystemName + m_strSystemName_En + m_strDrawingName); 
	sSel.MoveLeft(vwdCell, vOne, vMove); 
	sSel.TypeText("一. 阀件部分  VALVE PART"); 
	 
	return true; 
} 
 
CString CPrintInWord::GetAppPath() 
{ 
	CString strAppPath; 
	::GetModuleFileName(NULL,strAppPath.GetBuffer(255),255); 
	strAppPath.ReleaseBuffer(); 
	 
	int intLen  = strAppPath.ReverseFind('\\'); 
	strAppPath = strAppPath.Left(intLen); 
	 
	return strAppPath; 
} 
 
void CPrintInWord::EndPrint() 
{ 
	//退出 
	VARIANT vt ; 
	vt.vt =VT_ERROR; 
	vt.scode =DISP_E_PARAMNOTFOUND; 
	 
	VARIANT v; 
	v.vt =VT_BOOL; 
	v.boolVal =VARIANT_FALSE; 
	if (sApp != NULL) 
		sApp.Quit (&v,&vt,&vt); 
} 
 
void CPrintInWord::SaveDocument() 
{ 
	VARIANT vt ; 
	vt.vt =VT_ERROR; 
	vt.scode =DISP_E_PARAMNOTFOUND; 
	 
	VARIANT varFileName; 
	VariantInit(&varFileName); 
	varFileName.vt =VT_BSTR; 
 
	CFileDialog dlg(FALSE, "doc", "统计信息表",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"Word文档||*.doc||",NULL); 
 
	bool blnUnSelect = true; 
	while(blnUnSelect) 
	{ 
		if (IDOK == dlg.DoModal()) 
		{ 
			m_strFilePath = ""; 
			m_strFilePath = dlg.GetPathName();			 
			//判断文件是否打开 
			CFile file; 
			if (file.Open(m_strFilePath,CFile::modeWrite|CFile::modeCreate)) 
			{ 
				file.Close(); 
				varFileName.bstrVal = _bstr_t(m_strFilePath); 
				sDoc.SaveAs (&varFileName,&vt,&vt,&vt,&vt,&vt,&vt,&vt,&vt,&vt,&vt);	 
				//清除文件名对象 
				VariantClear(&varFileName); 
 
				//选择了一个文件名 
				blnUnSelect =false; 
			} 
			else 
			{ 
				AfxMessageBox("当前文件正在打开,无法进行存储!"); 
				m_strFilePath =""; 
			} 
		}//end of dlg.domodal 
		else 
		{ 
			AfxMessageBox("没有存储成功!"); 
			blnUnSelect =false; 
		}	 
	}//end of while 
} 
 
bool CPrintInWord::PrintBodyTypeOne(int totalNum) 
{ 
	SetActiveView(wdPaneNone);	 
	sSel=sApp.GetSelection(); 
	 
	//首先复制单元格 
	sSel.MoveRight(vwdCharacter,vTen,vwdExtend); 
	sSel.MoveDown(vwdLine,vOne,vwdExtend); 
	sSel.MoveRight(vwdCharacter,vFive,vwdExtend);	 
	sSel.Copy(); 
	sSel.MoveDown(vwdLine, vOne,vMove); 
 
	int TotalNum = totalNum; 
	int i =0;	 
	for(i=1; i