有没有办法将Microsoft Word文档转换为记事本文件.txt格式?

时间:2012-06-28 09:51:55

标签: c# asp.net web-applications ms-word office-interop

我试试这段代码

string[] ext = att.Name.Split('.');
string file = ext[0].ToString();
object Target = file + ".txt";
object nullobject = System.Reflection.Missing.Value;

Application.Documents.Open(ref FileName, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref value, ref value, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatUnicodeText;

Application.ActiveDocument.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown);
Application.Visible = false;
Microsoft.Office.Interop.Word.Document oDoc1 = Application.ActiveDocument;
string strNewDocText1 = oDoc1.Content.Text;

但是在strNewDocText1中获取包括项目符号和额外单词格式的输出

我希望将我的word文档的简单文本格式简单化为文本文档。

1 个答案:

答案 0 :(得分:2)

我相信你从这里拿到这个例子: http://www.codeproject.com/Articles/5273/How-to-convert-DOC-into-other-formats-using-C

所以基本上你有一个RTF,必须转换成纯文本。这是一个例子

最简单的方法是添加对System.Windows.Forms.dll的引用。

System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();

string richText = text// The rich text (with bullets and so on.)
rtBox.Rtf = richText ;
string plainText = rtBox.Text;

System.IO.File.WriteAllText(@"output.txt", plainText);