日期:2014-05-18 浏览次数:20777 次
public static string Doc2Text(string docFileName) { //实例化COM Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); object fileobj = docFileName; 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.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); //取得doc文件中的文本 IDataObject data = [color=#FF0000]Clipboard.GetDataObject();[/color] string outText = data.[color=#FF0000]GetData(DataFormats.Text).ToString();[/color] //string outText = doc.Content.Text; //关闭文件 doc.Close(ref nullobj, ref nullobj, ref nullobj); //关闭COM wordApp.Quit(ref nullobj, ref nullobj, ref nullobj); //返回 return outText; }