1. 程式人生 > >MS Office 自動化程式設計(二)---Word文件的讀寫

MS Office 自動化程式設計(二)---Word文件的讀寫

示例一:儲存至Word

經過一些瞭解之後,就不需要每次都將所有的介面匯入工程了。可以根據需要匯入,此次匯入的介面為:

Application、_Document、Documents、Range。新建基於對話方塊的MFC工程,引入標頭檔案,關鍵程式碼如下:

void CWordOperationDlg::OnClickedButton1()
{
	// TODO: Add your control notification handler code here
	CApplication oApp;
	CDocuments	oDocs;
	CDocument0	oDoc;

	if (!oApp.CreateDispatch(_T("Word.Application"), NULL))
	{
		AfxMessageBox(_T("啟動Word程式失敗!"));
		exit(1);
	}

	//檢視自動化過程
	oApp.put_Visible(true);
	oDocs = oApp.get_Documents();
	COleVariant varOPt(DISP_E_PARAMNOTFOUND, VT_ERROR);
	COleVariant varStartLine, varEndLine;
	varStartLine.intVal = 2;
	varEndLine.intVal = 50;
	//新增一個新文件
	oDoc = oDocs.Add(varOPt, varOPt, varOPt, varOPt);

	//獲取文件區域
	CRange range =  oDoc.Range(varStartLine, varEndLine);
	UpdateData(TRUE);
	range.put_Text(m_edit);
	//儲存docx文件
	try
	{
		oDoc.SaveAs(COleVariant(_T("G:\\softwaredevelopment\\MFC_VC_Windows\\project\\TEMP.DOCX")),
			varOPt, varOPt, varOPt,
			varOPt, varOPt, varOPt,
			varOPt, varOPt, varOPt,
			varOPt, varOPt, varOPt,
			varOPt, varOPt, varOPt);
	}
	catch (COleException* e)
	{
		LPVOID lpMsg;
		::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, e->m_sc,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsg, 0, NULL);
		::MessageBox(NULL, (LPCTSTR)lpMsg, _T("COM Error"), MB_OK|MB_SETFOREGROUND);
		::LocalFree(lpMsg);
	}
	catch(COleDispatchException *e)
	{
		TCHAR msg[512];
		wsprintf(msg, _T("程式執行出錯'%d',系統提示資訊為:\n\n%s"), e->m_scError & 0x0000FFFF,
			e->m_strDescription);
		::MessageBox(NULL, msg, _T("無法儲存檔案"), MB_OK|MB_SETFOREGROUND);
	}

	oDoc.Close(varOPt, varOPt, varOPt);
	oApp.Quit(varOPt, varOPt, varOPt);
}

執行結果:


示例二:Word表格操作

Word表格由表和單元格組成,對應Table和Cell物件,在以上工程中引入相應介面Table,Tables,Cell,Paragraph,Paragraphs,對錶格的新增過程是先建表,再設定單元格。

在以上工程的基礎上新增的關鍵程式碼:

//得到段落
	oParas = oDoc.get_Paragraphs();
	oPara = oParas.get_First();
	//得到段落區域
	oRange = oPara.get_Range();
	//設定第一段內容
	oRange.put_Text(_T("標題"));
	oPara.put_Alignment(1);

	COleVariant table_start(short(2));
	oRange = oDoc.Range(table_start, varOPt);
	//新增表格
	oTables = oDoc.get_Tables();
	oTable = oTables.Add(oRange, 3, 3, varOPt, varOPt);
	//設定表格內容
	for (int i = 1; i <= 3; i++)
	{
		for (int j = 1; j <= 3; j++)
		{
			oCell = oTable.Cell(i, j);
			oRange = oCell.get_Range();
			CString txt;
			txt.Format(_T("第%d行第%d列"),i,j);
			oRange.put_Bold(1);
			oRange.put_Text(txt);
		}
	}

操作Word結果:

示例二:Word報表解決方法

由以上示例可以看出對Word的操作是相當繁瑣的,針對於此,利用Word的模板,只需填入關鍵文字,就能得到一份精美的Word文件。

同理,首先將報表的格式固定做成模板,程式向單元格填入資料。而利用“書籤”就能快速定位正確的輸出位置。在型別庫中,對應的介面為Bookmarks、Bookmark。
1、先準備好Wrod模板

2、新建MFC基於對話方塊工程WordReport

3、根據需要引入型別庫中的介面

關鍵部分程式碼:

void CWordReportDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	CTable0		table;
	CTables0	tables;
	if (!m_oApp.CreateDispatch(_T("Word.Application"), NULL))
	{
		AfxMessageBox(_T("Create Word service failed!"));
		exit(1);
	}

	m_oApp.put_Visible(true);
	m_oDocs = m_oApp.get_Documents();
	COleVariant vopt(DISP_E_PARAMNOTFOUND, VT_ERROR);
	COleVariant start_line, end_line;
	COleVariant dot(_T("G:\\softwaredevelopment\\MFC_VC_Windows\\project\\template.dot"));
	//使用模板建立文件
	m_oDoc = m_oDocs.Add(&dot, &vopt, &vopt, &vopt);
	//獲取文件書籤
	m_oBookmarks = m_oDoc.get_Bookmarks();
	//依次設定標籤
	COleVariant tem1(_T("rol1"));
	m_oBookmark = m_oBookmarks.Item(&tem1);
	m_oRange = m_oBookmark.get_Range();
	//設定標題
	m_oRange.put_Text(_T("標題1"));
	
	COleVariant tem2(_T("rol2"));
	m_oBookmark = m_oBookmarks.Item(&tem2);
	m_oRange = m_oBookmark.get_Range();
	//設定標題
	m_oRange.put_Text(_T("標題2"));

	COleVariant tem3(_T("rol3"));
	m_oBookmark = m_oBookmarks.Item(&tem3);
	m_oRange = m_oBookmark.get_Range();
	//設定標題
	m_oRange.put_Text(_T("標題3"));

	//設定報表時間
	COleVariant tem4(_T("time"));
	m_oBookmark = m_oBookmarks.Item(&tem4);
	m_oRange = m_oBookmark.get_Range();
	//設定標題
	m_oRange.put_Text(_T("2012年5月20日"));

	//設定單元格內容
	tables = m_oDoc.get_Tables();
	table = tables.Item(1);
	CCell cell;
	for (int i = 2; i <= 9; i++)
	{
		for (int j = 1; j <= 3; j++)
		{
			cell = table.Cell(i,j);
			m_oRange = cell.get_Range();
			m_oRange.put_Text(_T("template"));
		}
	}
}


void CWordReportDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	if (m_oDocs.m_lpDispatch == NULL)
	{
		AfxMessageBox(_T("can't preview!"));
		return;
	}
	else
	{
		if (!m_oApp.get_PrintPreview())
		{
			m_oApp.put_PrintPreview(TRUE);
		}
		else
		{
			m_oApp.put_PrintPreview(FALSE);
		}
	}
}


void CWordReportDlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
	if (m_oDocs.m_lpDispatch == NULL)
	{
		AfxMessageBox(_T("Nothing! can't save!"));
		return;
	}
	else
	{
		OPENFILENAME ofn;
		TCHAR lpStrFileName[MAX_PATH] = _T("");
		ZeroMemory(&ofn, sizeof(ofn));

		ofn.lStructSize = sizeof(OPENFILENAME);
		ofn.hwndOwner = this->m_hWnd;
		ofn.lpstrFilter = _T("Word(.docx)\0*.docx\0");
		ofn.nMaxFile = MAX_PATH;
		ofn.lpstrFile = lpStrFileName;
		ofn.hInstance = AfxGetInstanceHandle();
		ofn.Flags = OPEN_EXISTING;

		COleVariant varOPt(DISP_E_PARAMNOTFOUND, VT_ERROR);
		if (GetSaveFileName(&ofn) == IDOK)
		{
			CString sFile = ofn.lpstrFile;
			try
			{
				m_oDoc.SaveAs(COleVariant(sFile),
					varOPt, varOPt, varOPt,
					varOPt, varOPt, varOPt,
					varOPt, varOPt, varOPt,
					varOPt, varOPt, varOPt,
					varOPt, varOPt, varOPt);
			}
			catch (COleException* e)
			{
				LPVOID lpMsg;
				::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
					FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, e->m_sc,
					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsg, 0, NULL);
				::MessageBox(NULL, (LPCTSTR)lpMsg, _T("COM Error"), MB_OK|MB_SETFOREGROUND);
				::LocalFree(lpMsg);
			}
			catch(COleDispatchException *e)
			{
				TCHAR msg[512];
				wsprintf(msg, _T("程式執行出錯'%d',系統提示資訊為:\n\n%s"), e->m_scError & 0x0000FFFF,
					e->m_strDescription);
				::MessageBox(NULL, msg, _T("無法儲存檔案"), MB_OK|MB_SETFOREGROUND);
			}

			COleVariant vopt(DISP_E_PARAMNOTFOUND, VT_ERROR);
			m_oApp.Quit(COleVariant((short)true), vopt, vopt);
		}
	}
}


程式執行結果: