C#读写Word文件(二)

本文介绍如何使用C#进行Word文件的读写操作,包括文档的另存为、替换功能,以及页眉设置和段落样式的详细调整。内容涵盖查找替换特定内容、设置页眉页码和段落格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、将文档另存为及替换操作

1、另存为

【提示】:在“C#读写Word文件(一)”【方式二】打开word文档的基础上进行操作

MSWord.Paragraph para = doc.Content.Paragraphs.Add(ref Nothing);

para.Range.Paste();

para.Range.InsertParagraphBefore();//添加一次回车

object newName = Directory.GetCurrentDirectory()+@"\新建文档.doc";

doc.SaveAs(ref  newName, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing);//另存为

 

2、查找替换操作

【例一】按照内容查找以及替换

string strDir = Directory.GetCurrentDirectory();

string docName = strDir + @"\test.doc";

object Nothing = System.Reflection.Missing.Value;  //一个编程时需要经常使用的一个参数

MSWord.ApplicationClass wordapp = null;   //这是MSWord程序,在这个程序下,可以同时打开多个文档

MSWord.Document doc = null;  //需要打开的MSWord文档



object docObject = docName;  //由于COM操作中,都是使用的 object ,所以,需要做一些转变

if (!File.Exists(docName))

{

File.Create(docName);

}



wordapp = new MSWord.ApplicationClass();

wordapp.Visible = true; //所打开的MSWord程序,是否是可见的

Object isVisible = true;

doc = wordapp.Documents.Open(ref docObject, ref Nothing, ref Nothing,

ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,

ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing,

ref Nothing, ref Nothing, ref Nothing);



strin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据之道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值