前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#操作 word代码

C#操作 word代码

作者头像
全栈程序员站长
发布2021-12-27 08:24:32
3530
发布2021-12-27 08:24:32
举报
文章被收录于专栏:全栈程序员必看
代码语言:javascript
复制
#region  读取word 
        /// <summary>
        /// 读取word所有文字内容(不包含表格)
        /// </summary>
        /// <returns>word中的字符内容(纯文本)</returns>
        public string ReadAllFromWord()
        {
            Word.ApplicationClass app = null;
            Word.Document doc = null;
            object missing = System.Reflection.Missing.Value;
            object FileName = m_FilePath;//@"E:/学习试验项目/ReadFromWordDoc/test.doc";
            object readOnly = true;
            object isVisible = false;
            try
            {
                app = new Word.ApplicationClass();
                doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,
                    ref missing, ref missing, ref missing);

                string textString = "";
                //读取全部内容 何问起 hovertree.com
                textString = doc.Content.Text.Trim();
//                int ParCount = this.getParCount(doc);//段数
//                for (int i = 1 ; i <= ParCount ; i++)
//                {
//                    textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();//
//                }
                textString = textString.Replace("/a","");    //替换空串为空。(word中/a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
                textString = textString.Replace("/r","/n");    //替换回车为回车换行
                return textString;      
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (doc != null)
                {
                    try
                    {
                        doc.Close(ref missing, ref missing, ref missing);
                    }
                    catch
                    {}
                    doc = null;
                }
                if (app != null)
                {
                    try
                    {
                        app.Quit(ref missing, ref missing, ref missing);
                    }
                    catch
                    {}
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();

            }
        }
        #endregion

 

 

 

#region 追加写入word /// <summary>
        /// 追加写入word
        /// </summary>
        /// <param name="InsertText">需要写入的字符串</param>
        public void WriteToWord(string InsertText)
        {
            Word.ApplicationClass app = null;
            Word.Document doc = null;
            object missing = System.Reflection.Missing.Value;
            object FileName = m_FilePath;//@"E:/学习试验项目/ReadFromWordDoc/test.doc";
            object readOnly = false;
            object isVisible = false;
            try
            {
                app = new Word.ApplicationClass();
                doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,
                    ref missing, ref missing, ref missing);
                //激活word文档
                doc.Activate();
                //追加到最后一段(段落是按照 /n 来作为标志的)
                doc.Paragraphs.Last.Range.Text = InsertText + "/n";//加个结束符(增加一段),否则再次插入的时候就成了替换.
                //保存
                doc.Save();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (doc != null)
                {
                    try
                    {
                        doc.Close(ref missing, ref missing, ref missing);
                    }
                    catch
                    {}
                    doc = null;
                }
                if (app != null)
                {
                    try
                    {
                        app.Quit(ref missing, ref missing, ref missing);
                    }
                    catch
                    {}
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        #endregion

推荐:http://www.cnblogs.com/roucheng/p/3521864.html

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/120361.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档