office word替换文本内容

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;


namespace ClassLibrary3
{
    class Class2
    {
        private static object unknow = Type.Missing;
        //private static object isReadOnly = ;
        static object oReadOnly = true;
        //object oMissing = System.Reflection.Missing.Value;


        public static void CreateWordDocument()
        {
            // 启动Word并使Word可见  
            Application wordApp = new Application() { Visible = false };


            // 新建Word文档  
            wordApp.Documents.Add();
            Document wordDoc = wordApp.ActiveDocument;
            Paragraph para = wordDoc.Paragraphs.Add();
            para.Range.Text = "欢迎你,进入Learning Hard博客";


            // 保存文档  
            object filename = @"D:\learninghard.doc";
            wordDoc.SaveAs2(filename);


            // 关闭Word  
            wordDoc.Close();
            wordApp.Application.Quit();
        }


        public static void ModifyWordDocument3()
        {
            Application wordApp = new Application() { Visible = true };
            object filename = @"D:\abc.docx";
            //Document doc = wordApp.Documents.Open(filename);
            Document doc = wordApp.Documents.Open(filename,
                    ref unknow, ref unknow, ref unknow, ref unknow,
                    ref unknow, ref unknow, ref unknow, ref unknow,
                    ref unknow, ref unknow, ref unknow, ref unknow,
                    ref unknow, ref unknow, ref unknow);
            


            // 关闭Word  
            doc.Close();
            wordApp.Application.Quit();


        }


        public static void ModifyWordDocument()
        {
            Application wordApp = new Application() { Visible = true };
            object filename = @"D:\abc3.docx";
            //Document doc = wordApp.Documents.Open(filename);
            Document doc = wordApp.Documents.Open(filename,
                    ref unknow, ref unknow, ref unknow, ref unknow,
                    ref unknow, ref unknow, ref unknow, ref unknow,
                    ref unknow, ref unknow, ref unknow, ref unknow,
                    ref unknow, ref unknow, ref unknow);
            int c = ParaCount(doc);
            for(int i = 1; i <= c; i ++)
            {
                try
                {
                    if (doc.Paragraphs[i] == null)
                    {
                        continue;
                    }
                    Paragraph p = doc.Paragraphs[i];


                    


                    string o = (string)GetTextFromPara(doc.Paragraphs[i]);


                    string s = "第" + (i.ToString());


                    s = s + "段\n" + o + "\n";


                    if (!(o == String.Empty || o == null))
                    {
                        object start = p.Range.Start;
                        object end = p.Range.End - 1;
                        if (p.Range.Tables.Count > 0)
                        {
                            end = end = p.Range.End - 1;
                        }
                        
                         
                        if (start == end) continue;


                        //p.Range.(start, end).Text = "------------" + i.ToString();
                        Range rang = doc.Range(ref start, ref end);
                        rang.Text = "------------" + i.ToString();
                        
                        


                    }


                    //p.Range.Text.Replace(o, "------------" + i.ToString() + "\n");


                    Write("D:\\csharp.log", s);
                }
                catch (Exception e) {
                    Write("D:\\csharp.log", e.Message.ToString());
                }
                


                doc.SaveAs2("d:\\abc4.docx");
            }


            // 关闭Word  
            doc.Close();
            wordApp.Application.Quit();


        }


        private static int ParaCount(Document doc)
        {
            return doc.Paragraphs.Count;
        }


        private static object GetTextFromPara(Paragraph p)
        {


            string s = p.Range.Text.Trim();
            s = s.Replace("\a", "");
            return s;
        }


        public static void Write(string path, string content)
        {
            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write("Hello World!!!!\n");
            sw.Write(content);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }




        public string HttpRequest(string postMethod, string postPar)
        {
            string s = "";
            string cReferralUrl = "http://www.baidu.com";
            try
            {
                HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(cReferralUrl);
                hwr.Accept = "text/html, application/xhtml+xml, */*";
                hwr.ContentType = "application/x-www-form-urlencoded";
                hwr.Method = "POST";
                hwr.KeepAlive = true;
                hwr.ReadWriteTimeout = 3000;
                byte[] data = Encoding.GetEncoding("gb2312").GetBytes(postPar.ToString());
                using (Stream stream = hwr.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                    stream.Close();
                }
                Stream stre = hwr.GetResponse().GetResponseStream();
                StreamReader sr = new StreamReader(stre);//创建一个stream读取流  
                string msgs = sr.ReadToEnd();
                sr.Close();
                return msgs;
            }
            catch (Exception error)
            {
                throw new Exception(String.Format("请求方法{0}时出现异常:", cReferralUrl) + error.Message + Environment.NewLine + "入参:" + postPar);
            }
        }
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值