c 如何读写word文档 java读写word文档 - 电脑知识 - 【三明电脑网】_三明电脑维修_三明笔记本电脑维修_监控安装_市区上门维修

全国统一24小时服务热线:400-0000-000400-0000-000  / 1399000000

当前位置:首页 > 电脑知识 > 正文

c 如何读写word文档 java读写word文档

发布日期:2021-04-29

摘要:怎么用COM读写WORD里的文本信息? 1 你说的这个主要是取得word接口 然后配合word自带的宏完成任务 实现起来很简单COleVariant vTrue((short)TRUE), vFals...

c 如何读写word文档

怎么用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/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。

这些兄台可以自己看书去学

如何用C语言读写文件

一个c语言读写文件程序:#include "stdio.h"#include main(){ FILE *fp1;//定义文件流指针,用于打开读取的文件 FILE *fp2;//定义文件流指针,用于打开写操作的文件 char text[1024];//定义一个字符串数组,用于存储读取的字符 fp1 = fopen("d:\\a.txt","r");//只读方式打开文件a.txt fp2 = fopen("d:\\b.txt","w");//写方式打开文件a.txt while(fgets(text,1024,fp1)!=NULL)//逐行读取fp1所指向文件中的内容到text中 { puts(text);//输出到屏幕 fputs(text,fp2);//将内容写到fp2所指向文件中 } fclose(fp1);//关闭文件a.txt,有打开就要有关闭 fclose(fp2);//关闭文件b.txt}...

怎么用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#如何读写配置文件?

key, temp;public Ini(string path){this.sPath = path;ini"的文件。

其一般形式如下:[section1] /, sPath)。

以及XML文件的国际标准化给INI文件又一次打击。

但在某些场合.StringBuilder(255);//键名 // 每次从ini中读取多少字节System.Text;/ 声明INI文件的写操作函数 WritePrivateProfileString()[System.Runtime.InteropServices.DllImport("kernel32")]private static extern longWritePrivateProfileString(string section,INI文件是很多,最重要的就是"System.Runtime;。

该文件主要存放用户所做的选择以及系统的各种参数。

用户可以通过修改INI文件,来改变应用程序和系统的很多配置。

但自从Windows 95的退出;}public string ReadValue(stringsection, string key){//kernel32&quot、&quot, string filePath), string key;/ section=配置节, stringfilePath);/声明INI文件的写操作函数 WritePrivateProfileString()[DllImport("/ 配置节/,INI文件还拥有其不可替代的地位。

比如绿色软件的规定就是不向注册表和系统中填入新东西。

对于软件需要储存的信息就需要存入到文件中了。

XML虽然兼容性比较好,但对于仅仅保存几个自定义参数而言就显得大材小用了;键值keyword1 = valuelkeyword2 = value2……[section2]keyword3 = value3keyword4 = value4 在Windows系统中;}public string ReadValue(stringsection, string key){/// 声明INI文件的读操作函数 GetPrivateProfileString()[System.Runtime.InteropServices.DllImport("kernel32")]private static extern intGetPrivateProfileString(string section, string key, string def,System.ini".引入命名空间usingSystem, string key。

1;System32.ini"kernel32&quot.Text.StringBuilder retVal;private string sPath = null.ToString(); //.StringBuilder temp =new System.Text.声明(把一个Win32 API函数转成C#函数)//注意类型的转换}到此基本功能已经实现了;和"Win.ini&quot.InteropServices,path=路径WritePrivateProfileString(section,key, value。

这是就可以选择使用快速简单的储存方式:INI文件。

本文就来探讨一下C#是如何对INI进行读写操作。

主要思路是调用Win32 API;)]private static extern intGetPrivateProfileString(string section;}public void Writue(string section,string key,key=键名,temp=上面,path=路径GetPrivateProfileString(section;/ section=配置节, "", string def;/ section=配置节,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点, string val,使应用程序和系统都把许多参数和初始化信息放进了注册表中,key=键名,value=键值;声明INI文件的读操作函数 GetPrivateProfileString()[DllImport(" 每次从ini中读取多少字节System.Text;2, string value){// section=配置节;)]private static extern longWritePrivateProfileString(string section, string key, string val, stringfilePath), StringBuilderretVal, int size, string filePath);3.函数public void Writue(string section,string key, int size.StringBuilder temp =new System.Text, sPath).StringBuilder(255);/,path=路径WritePrivateProfileString(section,key, value;//, string value){/。

下面我们将所有的代码重新整合一下:namespace Library.File{public class Ini{/,key=键名,temp=上面,path=路径GetPrivateProfileString(section,key, "", temp, 255, sPath);return temp.ToString();}}} 开始调用函数。

// 写入iniIni ini = newIni("C:/config.ini");ini.Writue("Setting","key1", "HELLO WORLD!");ini.Writue("Setting","key2", "HELLO CHINA!");// 读取iniIni ini = newIni("C:/config.ini");string str1 =ini.ReadValue("Setting", "key1");MessageBox.Show(str1); 二,在一些小的应用中,有时候不需要使用数据困这样大规模的数据管理工具,也很少进行数据的查询、修改等操作,而仅用文件来存储数据。

这时就需要使用。

net中的文件操作对象,如file、streamReader、streamWriter等。

1,使用File对象操作文件System.IO.File类提供了一系类的静态办法,完成对晚间的常用操作,如新建、删除、拷贝、移动等2,使用StreamWriter写入文件 在System.IO空间中定义了一个文件写入器对象StreamWriter,使用它可以以一种特定的编码向输出流中(Stream)写入字符。

3,使用SteamReader读取文件 与streamWrite对应, 255, sPath);returntemp,key=键名,value=键值INI文件就是扩展名为&quot

在vs中如何用C语言读写txt文件时,文件的位置应该放到哪

使用C语言的文件操作函数可以读写txt文件,如果使用相对路径,文件必须放在程序相同的文件夹内。

1、C语言标准库提供了一系列文件操作函数。

文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。

例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。

2、例程: #includeint a;char b,c[100];int main(){ FILE * fp1 = fopen("input.txt", "r");//打开输入文件 FILE * fp2 = fopen("output.txt", "w");//打开输出文件 if (fp1==NULL || fp2==NULL) {//若打开文件失败则退出 puts("不能打开文件!"); rturn 0; } fscanf(fp1,"%d",&a);//从输入文件读取一个整数 b=fgetc(fp1);//从输入文件读取一个字符 fgets(c,100,fp1);//从输入文件读取一行字符串 printf("%ld",ftell(fp1));//输出fp1指针当前位置相对于文件首的偏移字节数 fputs(c,fp2);//向输出文件写入一行字符串 fputc(b,fp2);//向输出文件写入一个字符 fprintf(fp2,"%d",a);//向输出文件写入一个整数 fclose(fp1);//关闭输入文件 fclose(fp2);//关闭输出文件,相当于保存 return 0;}...

...读取一个文件,写入另一个文件。

要求分别按照文本和二进制方式进...

#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;}

上一篇:怎么把gif添加到word

下一篇:word无法使用查找功能 word修订功能无法使用