2012-06-28 33 views
5

我試試這個代碼有沒有什麼辦法可以將Microsoft word文檔轉換爲記事本文件.txt formate?

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文檔的簡單的純文本格式轉換爲文本documnt輸出。

+0

您無法安全地使用ASP.NET等服務程序中的Office API。它們僅用於桌面使用。 –

+0

我可以知道原因嗎? –

+0

請參閱[服務器端自動化Office的注意事項](http://support.microsoft.com/kb/257757) –

回答

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); 
+1

但我的應用程序不是不是桌面應用程序 –

+1

這並不意味着您不能添加對該組件的引用。這是最快,最簡單的解決方案。 – Nas

+0

他沒有不可用不RichTextBox ...在這一刻 –

相關問題