如何在控制台程序中讀取WORD文檔的內容?
1.在VC中新建一控制台程序,選支持MFC(當然,你也可以不選擇支持MFC的,不過會很麻煩)
2.按CTRL+W調出MFC ClassWizard,Add Class->From a type library,選擇你的word的類型庫(例如我的是word2003,安裝在e盤,我的路徑是"e:\edittools\microsoft office\office11\msword.olb"),選擇完畢後,在彈出的窗口中選擇要讓classwizard生成的包裝類,在本例中要用到_Application,Documents,_Document,Range這四個類,選中他們後按OK
3.進入你的main函數所在的cpp文件,加入頭文件引用
#include "msword.h" //引用剛才classwizard生成的idispatch包裝類
4.加入代碼
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetMoleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
if (CoInitialize(NULL) != S_OK)
{
AfxMessageBox("初始化COM支持庫失敗!");
return -1;
}
_Application wordApp;
Documents docs;
_Document doc;
Range aRange;
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
wordApp.CreateDispatch("Word.Application",NULL);
wordApp.SetVisible(FALSE);
docs=wordApp.GetDocuments();
doc=docs.Open(COleVariant("d:\\txt.doc"),vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
aRange=doc.Range(vOpt,vOpt);
AfxMessageBox(aRange.GetText());//這里GetText得到的就是word文件的純文本了,你可以將其寫到txt文件中
doc.Close(vOpt,vOpt,vOpt);
wordApp.Quit(vOpt,vOpt,vOpt);
CoUninitialize();
}
return nRetCode;
}
Ⅱ mfc如何導出數據至word文檔中
你閱讀MSDN (October 2001)文章: 抄Q179706 HOWTO: Use MFC to Automate Excel & Create/Format a New Workbook Q186120 HOWTO: Use MFC to Automate Excel and Fill a Range with an Array Q186427 HOWTO: Catch Microsoft Excel 97 Application Events Using VC++ 會對你有幫助。 如果要在你的程序中嵌入word等,可以使用htmlview實現。
Ⅲ 如何把已讀入內存中的WORD文檔顯示在MFC程序的窗口中
使用ole技術可以把word文檔嵌入MFC程序裡面,但是要讀取文件內容並顯示是很麻煩的,因為word文檔格式估計只有微軟知道。
Ⅳ 如何在MFC編輯框中導入WORD文檔內容
和導入Excel一樣的操作吧
Ⅳ VC++ MFC 打開word文檔的問題
#include "stdafx.h"
#pragma warning(disable:4259)
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\MSO.DLL" rename("IAccessible", "msoIAccessible")
using namespace Office;
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"
//using namespace VBIDE;
#import "d:\\Program Files\\Microsoft Office\\OFFICE11\\MSWORD.OLB" rename("ExitWindows","WordExitWindows")
using namespace Word;
#include <atlbase.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(0);
{
_ApplicationPtr m_WordPtr;
DocumentsPtr m_DocsPtr;
_DocumentPtr m_DocPtr;
HRESULT hr;
try{
//CLSID sid;
//CLSIDFromProgID(L"word.application",&sid);
hr=m_WordPtr.CreateInstance(__uuidof(Application));
m_WordPtr->put_Visible(VARIANT_TRUE);
m_DocPtr=m_WordPtr->Documents->Add();
LPSTR a = "c:\\22.doc";
CComBSTR b = a;
BSTR c = b.m_str;
VARIANT va;
va.vt = VT_BSTR;
va.bstrVal = c;
m_WordPtr->Documents->Open(&va);
SelectionPtr sel=m_WordPtr->GetSelection();
sel->TypeText(_bstr_t("sadsaS12as"));
sel->MoveEnd();
InlineShapePtr sh=sel->GetInlineShapes()->AddPicture(_bstr_t("c:\\1.jpg"));
sel->SetRange(1,2);
//sel->TypeText(_bstr_t("!!!!!!!"));
_bstr_t text=sel->GetText();
char *s=_com_util::ConvertBSTRToString(text);
cout<<s<<endl;
TablesPtr tables = sel->GetTables();
TablePtr table = tables->Add(sel->GetRange(), 2, 5);
BordersPtr bords = table->GetBorders();
bords->PutOutsideLineStyle(wdLineStyleSingle);
bords->PutOutsideLineWidth(wdLineWidth150pt);
bords->PutInsideLineStyle(wdLineStyleSingle);
for (int i = 1; i<=2; i++)
{
for (int j = 1; j<=5; j++)
{
table->Cell(i,j)->GetRange()->PutText("20");
}
}
CellPtr cell = table->Cell(1,1);
cell->Merge(table->Cell(1,2));
sel->Delete();
m_WordPtr->GetActiveDocument()->SaveAs(&CComVariant("c:\\1.doc"));
//m_WordPtr->Quit();
}
catch(_com_error *e)
{
MessageBox(NULL, "haha ", "no word ",MB_OK);
return -1;
}
}
CoUninitialize();
return 0;
}
把導入庫改為你的word配置的正確路徑即可
Ⅵ mfc程序怎麼實現打開word文檔
調用word打開,可以搜一搜"MFC如何運行word"的資料
Ⅶ MFC讀寫Excel、word文件。
我前幾天寫了來一個,但是好像根據自不同的Excel版本會出現創建Excel失敗的效果,我在自己電腦上就行,換了電腦就不行了,不知道什麼原因。我發給你,你看一下吧,即使不能用,裡面的函數還是有用的,都有解釋!
Ⅷ 如何用C++讀取word中的圖片,最好顯示在MFC
#include <stdlib.h> #include <stdio.h> int main () { FILE * fpPhoto, * fpText, * fpTarget ; int iRead ; char szBuf[100] ; printf ("請輸入第一個文件專名(屬jpg):\n") ; gets (szBuf) ; fpPhoto = fopen (szBuf, "rb") ; printf ("請輸入第二個文件名(txt):\n") ; gets (szBuf) ; fpText = fopen (szBuf, "rb") ; printf ("請輸入目的文件名(jpg):\n") ; gets (szBuf) ; fpTarget = fopen (szBuf, "wb") ;
Ⅸ 如何向MFC編輯框導入WORD文檔
首先添加一個編輯框,把ID改成IDC_EDIT
然後在導入按鈕的消息響應專函數中添加如屬下代碼:
CFile file;
CString str;
GetDlgItemText(IDC_EDIT,str);
file.Open("1.doc",CFile::modeWrite|CFile::modeCreate);
file.Write(str,str.GetLength());
file.Close();
在導出按鈕的消息效應函數中添加如下代碼:
CFile file;
char str[1000]={"\0"};
file.Open("1.doc",CFile::modeRead);
file.Read(str,file.GetLength());
file.Close();
SetDlgItemText(IDC_EDIT,str);
不明白的話,可以繼續追問~~
Ⅹ mfc 如何打開文件
讀取EXCEL文檔里的數據,因為EXCEL是有格式的,所以和讀取TXT等數據文件完全不同。回
如果TXT,那麼使用答CFile類Open打開文件就可以讀取數據了,相對比較簡單。
讀取EXCEL文檔里的數據,需要用ADO數據源或者OLE的Office組件,在Open菜單響應函數中設置數據源並進行資料庫操作,如果初學,可能會比較困難,需要熟悉的東西很多。
建議:EXCEL文件可以另存為csv格式,這個是純文本的,可以直接CFile按照文本文檔讀取,如果不是項目需求,這個實現比較適合初學者。