using System;
using System.Windows.Forms;
using System.IO;using Word = Microsoft.Office.Interop.Word;
using System.Reflection;namespace WordTrue
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.doc|*.doc";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                object miss = System.Reflection.Missing.Value;
                Word.Application word = new Word.ApplicationClass();
                object fileName = ofd.FileName;
                Word.Document doc = word.Documents.Open(ref fileName, ref  miss, ref  miss, ref  miss, ref  miss, ref  miss, ref  miss, ref  miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
                string s = doc.Content.Text.ToString();
                s = s.Substring(0, s.IndexOf("\r"));
                s = s.Trim();
                textBox1.Text = s;
                doc.Close(ref miss, ref miss, ref miss);
                word.Quit(ref miss, ref miss, ref miss);
                releaseObj(word);
               
            }
         }
        
        private void releaseObj(object obj)
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
            GC.Collect();
        }        private void textBox1_TextChanged(object sender, EventArgs e)
        {
       
        
        
        }
                }
}
代码如上。我想要提取文档的首行来替换文件名。针对这段代码,如果Word文档的第一行有标题并且为一句话的话,确实能达到要求,但是当没有标题并且第一行为空的话那textBox1中就显示为空白了,还有一种情况就是一段话占了从第一行开始的好几行,那textBox1中显示的就是这段话了,作为文件名来说那太长了,所以我这样想:如果第一行有字符(不包括空字符)那就提取第一行的字符,但不能超过12个字符。如果第一行无字符,那就提取最开始的12个非空字符,当然如果前12个字符中出现了“,”、“。”、“!”这类字符,那就到此为止,提取这类字符之前的字符(不包括“,”、“。”、“!”这类字符)。
总之我的想法是想从文档的内容中提取一些字符来替换文件名,如果各位有更好的想法可以按你们的想法来提取字符。
好了的话,请各位把这方法的代码写入到我完整的代码中再回帖,不要只发这部分的代码,在这里先谢谢大家了哦!
哦,对了,既然是要用提取的字符来替换文件名,还请各位能把实现替换的方法也写上,本人很菜,还请各位多多帮忙!

解决方案 »

  1.   

    通过书签获取内容
    通过正则删除相关字符,或直接使用当前日期
    WORD另存为
      

  2.   

    这个,Word会有一个标题字段可以读取的。
    不过大部分人都不会仔细设置这个值。另外也可以通过判断什么文字被设置了 header1 样式来判断。不过同样很多人不会使用样式。所以没有什么好办法。
      

  3.   

    using System;   
    using System.Collections;   
    using System.Configuration;   
    using System.Data;   
    using System.Web;   
    using System.Web.Security;   
    using System.Web.UI;   
    using System.Web.UI.HtmlControls;   
    using System.Web.UI.WebControls;   
    using System.Web.UI.WebControls.WebParts;   
    using Microsoft.Office.Interop;   
    using Microsoft.Office;   
    public partial class Default2 : System.Web.UI.Page   
    {   
        protected void Page_Load(object sender, EventArgs e)   
        {   
            lit.Text = GetText(Server.MapPath("b.doc"));   
        }   
        public string GetText(string fileName)   
        {   
            //实例化COM   
            Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();   
            object fileobj = fileName;   
            object nullobj = System.Reflection.Missing.Value;   
            //打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)   
            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,   
                ref nullobj, ref nullobj, ref nullobj,   
                ref nullobj, ref nullobj, ref nullobj,   
                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj   
                );   
            //取得doc文件中的文本   
            string outText = doc.Content.Text;   
            //关闭文件   
            doc.Close(ref nullobj, ref nullobj, ref nullobj);   
            //关闭COM   
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);   
            //返回   
            return outText;   
        }   
      
    }  本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/11/09/4790543.aspx