[C#]Word读写


开头调用C#的Word连接库:

using Word=Microsoft.Office.Interop.Word;  

建立一个Word对象以及一些创建对象时会用到的变量,oWord是word应用程序,oDoc是里面的文档

public Word._Application oWord= new Word.Application();  
public object oFileName = @"C:\XXX.docx";  
public object oReadOnly = true;  
public object oMissing = System.Reflection.Missing.Value;  
public object dontsave = Word.WdSaveOptions.wdDoNotSaveChanges;  
public object save = Word.WdSaveOptions.wdPromptToSaveChanges;  
Word._Document oDoc  

打开word文档

//打开word文档  
oDoc = oWord.Documents.Open(ref path, ref oMissing, ref oReadOnly, ref oMissing,   
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,   
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,   
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing);

然后是进行操作,比如读表

//读取第一个映射表  
Word.Table currTab = oDoc.Tables[1];  
for (int row = 1; row < currTab.Rows.Count; row++)  
{  
     string text = currTab.Cell(row + 1, 1).Range.Text;  
}  

不过别忘了word文档读取出来的字符都带有回车符,不删除的话会显示成乱码

//去除word文档中自带的回车符  
private string del_enter(string s)  
{  
     return s.Remove(s.Length - 2, 2);  
}  

最后别忘了保存文档,关闭word应用程序

oDoc.Close(ref dontsave, ref oMissing, ref oMissing);  
oWord.Quit(ref dontsave, ref oMissing, ref oMissing); 


转载地址:http://www.verydemo.com/demo_c92_i12146.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值