码迷,mamicode.com
首页 > 其他好文 > 详细

读取Word文档的标题

时间:2015-02-04 10:45:19      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

一:描述,将读取的文档标题添加到下拉框中

技术分享

二:代码

 #region 方法:得到Word文档标题的内容
         public static List<string> GetTitles(int j,int m)//标题深度,标题字符个数选取标准
        {
            List<string> title = new List<string>();
            for (int i = 0; i <= j; i++)//标题深度
            {
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = null;
                object fileName = "D://1.doc";
                object unknow = Type.Missing;

                app.Visible = false;
                doc = app.Documents.Open(ref 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);
                for (int k = 1; k <= doc.Paragraphs.Count; k++)
                {
                    if(doc.Paragraphs[k].Range.Text.Trim().Length>m)
                    title.Add(doc.Paragraphs[k].Range.Text.Trim());
                }

                object unknowFirst = Type.Missing;
                object saveChangesFirst = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
                app.ActiveDocument.Close(ref saveChangesFirst, ref unknowFirst, ref unknowFirst);

                object unknowSec = Type.Missing;
                object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
                app.Quit(ref saveChanges, ref unknowSec, ref unknowSec);
            }
            return title;
        } 
    #endregion

 

读取Word文档的标题

标签:

原文地址:http://www.cnblogs.com/hongmaju/p/4271707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!