如何在控制台程序中读取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按照文本文档读取,如果不是项目需求,这个实现比较适合初学者。