怎么用COM读写WORD里的文本信息?
1.你说的这个主要是取得word接口 然后配合word自带的宏完成任务 实现起来很简单COleVariant vTrue((short)TRUE), vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);_Application m_App;//定义Word提供的应用程序对象;Documents m_Docs;//定义Word提供的文档对象;Selection m_Sel;//定义Word提供的选择对象;m_Docs.ReleaseDispatch();m_Sel.ReleaseDispatch();m_App.m_bAutoRelease=true;if(!m_App.CreateDispatch("Word.Application")){ AfxMessageBox("创建WordXP服务失败!"); exit(1); }//下面是定义VARIANT变量;COleVariant varFilePath(sPath+"MYDOC.DOC");COleVariant varstrNull("");COleVariant varZero((short)0);COleVariant varTrue(short(1),VT_BOOL);COleVariant varFalse(short(0),VT_BOOL);m_Docs.AttachDispatch(m_App.GetDocuments());//将Documents类对象m_Docs和Idispatch接口关联起来;m_Docs.Open(varFilePath,varFalse,varFalse,varFalse,varstrNull,varstrNull,varFalse,varstrNull,varstrNull,varTrue,varTrue,varTrue,varTrue,varTrue,varTrue);//打开Word文档; m_Sel.AttachDispatch(m_App.GetSelection());//将Selection类对象m_Sel和Idispatch接口关联起来;下面就是操作了~2.用批处理做不到 如果只是这样子的话 连VC都可以省了 VBS 就能做到比如新建一个c:\1.doc 里面写上wangtk1982 然后保存退出 编辑下面文件为vbs文件 运行看看结果Dim oWordSet oWord = WScript.CreateObject("Word.Application")set myDoc =oWord.Documents.Open("c:\1.doc")With oWord.Selection.Find.Text = "1982".Replacement.Text = "2011".Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = False.MatchWholeWord = False.MatchByte = True.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd WithoWord.Selection.Find.Execute ,,,,,,,,,,1myDOc.SavemyDoc.Closeoword.Quit
如何用C语言读取word中的数据
1. #include 2. #include 3. #include 4. int main()5. {6. char ch;7. char buffer[1024];//缓冲区8. int len = 0;9. FILE *fp;10. fp=fopen("word.txt","r");11. if(fp==NULL)12. {13. printf("open file word.txt failed!\n");14. }15. while(!feof(fp))16. {17. ch=fgetc(fp);18. buffer[len++] = ch;//放到缓冲区19. if (ch == '\n'){20. buffer[len] = '\0';21. printf ("%s", buffer);22. len=0;23. sleep(3);24. }//回车输出25. }26. if (len){buffer[len] = '\0';printf ("%s", buffer);}//如果最后没有回车,输出缓冲区内容27. fclose(fp);28. return 0;29. }
如何用c++读取word文档
基本步骤(1)创建)一个 MFC 的程序工程。
注意:在VC中对WORD进行操作需要在MFC AppWizard - Step 2 of4中的Automaiton选项上打上勾。
(2)Ctrl+W 执行 ClassWizard(本文按照 VC6 操作,示例程序是在VC6 下编写测试的)。
(3)Add Class...\From a type Library... 在 Office目录中,找到想使用的类型库。
(我使用的是 Office2003,其Word 的类型库文件,保存在 E:\ProgramFiles\Microsoft Office\Office12\MSWOR.OLB)。
(4)选择类型库文件后,在弹出的对话窗中继续选择要添加的类。
具体选择什么类,要看你将来在程序中打算调用什么功能。
当然,也可以不用考虑这么多,用鼠标和Shift键配合,全部选择也可以。
(5)初始化COM。
方法一,找到App的InitInstance()函数,在其中添加AfxOleInit()函数的调用;方法二,在需要调用COM功能的地方 CoInitialize(NULL),调用完毕后CoUninitialize()。
(6)在你需要调用 Office 功能函数的 cpp 文件中 #include //为了方便操作 VARIANT 类型变量,使用 CComVariant 模板类 #include "文件名.h" //具体的头文件名,是由装载类型库的文件名决定的,如MSWORD。
示例程序: //word应用程序 _Application app; //初始化连接 app.CreateDispatch("word.Application"); Documents doc; CComVarianta(_T(strWord)),b(false),c(0),d(true),aa(0),bb(1); _Document doc1; doc.AttachDispatch(app.GetDocuments()); doc1.AttachDispatch(doc.Add(&a,&b,&c,&d)); Range range; //求出文档的所选区域 range=doc1.GetContent();//取出文件内容 str=range.GetText(); m_richedit.SetWindowText(str); //关闭 app.Quit(&b,&c,&c); //释放环境 app.ReleaseDispatch();
c++如何读取word
基本步骤(1)创建)一个 MFC 的程序工程。
注意:在VC中对WORD进行操作需要在MFC AppWizard - Step 2 of4中的Automaiton选项上打上勾。
(2)Ctrl+W 执行 ClassWizard(本文按照 VC6 操作,示例程序是在VC6 下编写测试的)。
(3)Add Class...\From a type Library... 在 Office目录中,找到想使用的类型库。
(我使用的是 Office2003,其Word 的类型库文件,保存在 E:\ProgramFiles\Microsoft Office\Office12\MSWOR.OLB)。
(4)选择类型库文件后,在弹出的对话窗中继续选择要添加的类。
具体选择什么类,要看你将来在程序中打算调用什么功能。
当然,也可以不用考虑这么多,用鼠标和Shift键配合,全部选择也可以。
(5)初始化COM。
方法一,找到App的InitInstance()函数,在其中添加AfxOleInit()函数的调用;方法二,在需要调用COM功能的地方 CoInitialize(NULL),调用完毕后CoUninitialize()。
(6)在你需要调用 Office 功能函数的 cpp 文件中 #include //为了方便操作 VARIANT 类型变量,使用 CComVariant 模板类 #include "文件名.h" //具体的头文件名,是由装载类型库的文件名决定的,如MSWORD。
示例程序: //word应用程序 _Application app; //初始化连接 app.CreateDispatch("word.Application"); Documents doc; CComVarianta(_T(strWord)),b(false),c(0),d(true),aa(0),bb(1); _Document doc1; doc.AttachDispatch(app.GetDocuments()); doc1.AttachDispatch(doc.Add(&a,&b,&c,&d)); Range range; //求出文档的所选区域 range=doc1.GetContent();//取出文件内容 str=range.GetText(); m_richedit.SetWindowText(str); //关闭 app.Quit(&b,&c,&c); //释放环境 app.ReleaseDispatch();
用C语言怎么读取word,excel和ppt文件中的数据
VC++6.0 有智能手段,非常简单,好久没用VC6.0了,我这也没有装,具体步骤记不清了,大概就是通过智能添加一个类,有个选项好像是从DLL还是什么来的一个按钮,找到WORD的执行程序exe文件,就能生成一个WORD的类,里面有WORD的所有方法,如何调用就不用我说了
c/c++ 怎么读取汉字
#include int main(){FILE *pword,*pword1;char a;if((pword = fopen("word.txt","rt")) == NULL) return 0;if((pword1= fopen("word1.txt","at")) == NULL) return 0;do{a = fgetc(pword);fputc(a,pword1);}while (a != EOF);fclose(pword);fclose(pword1);return 1;}
c#如何读写文本文件
新建一个Log.txt文件引入System.IO名称空间,用文件流using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace StreamWrite{class Program{static void Main(string[] args){try{FileStream aFile = new FileStream("Log.txt", FileMode.OpenOrCreate);StreamWriter sw = new StreamWriter(aFile);bool truth = true;// Write data to file.sw.WriteLine("Hello to you.");sw.WriteLine("It is now {0} and things are looking good.",DateTime.Now.ToLongDateString());sw.Write("More than that,");sw.Write(" it's {0} that C# is fun.", truth);sw.Close();}catch (IOException ex){Console.WriteLine("An IOException has been thrown!");Console.WriteLine(ex.ToString());Console.ReadLine();return;}}}}读取文件,这里介绍StreamReader对象static void Main(string[] args){string strLine;try{FileStream aFile = new FileStream("Log.txt",FileMode.Open);StreamReader sr = new StreamReader(aFile);strLine = sr.ReadLine();//Read data in line by line 这个兄台看的懂吧~一行一行的读取while(strLine != null){Console.WriteLine(strLine);Line = sr.ReadLine();}sr.Close();}catch (IOException ex){Console.WriteLine("An IOException has been thrown!");Console.WriteLine(ex.ToString());Console.ReadLine();return;}}另外对于简单的文档可以直接sr.ReadToEnd()从头读到尾,还有sr.Read() 返回类型char。
这些兄台可以自己看书去学
生活不止眼前的狗血