我有一個字符串str
的RTF數據,我希望數據能夠加載到MS字對象中。我見過Documents.Open()
方法,但它需要一個物理文件路徑來讀取一個特定的文件,我沒有這樣的文件。C#Interop Word - 從一個字符串讀取RTF數據
如何打開Word的新實例並在其中加載我的RTF數據?
Microsoft.Office.Interop.Word.ApplicationClass wordapp = new ApplicationClass();
wordapp.Visible = false;
string str = @"{\rtf1\ansi\ansicpg1252\uc1\deff0{\fonttbl{\f0\fnil\fcharset0\fprq2 Arial;}{\f1\fswiss\fcharset0\fprq2 Arial;}{\f2\froman\fcharset2\fprq2 Symbol;}}
{\colortbl;}{\stylesheet{\s0\itap0\nowidctlpar\f0\fs24 [Normal];}{\*\cs10\additive Default Paragraph Font;}}{\*\generator TX_RTF32 17.0.540.502;}
\paperw12240\paperh15840\margl1138\margt1138\margr1138\margb1138\deftab1134\widowctrl\formshade\sectd\headery720\footery720\pgwsxn12240\pghsxn15840\marglsxn1138\margtsxn1138\margrsxn1138\margbsxn1138\pgbrdropt32\pard\itap0\nowidctlpar\plain\f1\fs20 test1\par }";
我會做一些格式的字,但applicationClass應該wordapp.Visible = false;
更新:我不喜歡被保存在系統中的文件。無論如何讀取和工作,而不將其保存在物理內存上?
如果我得到你的觀點正確,你想要做的RTF數據的格式化沒有物理文件。你可以檢查RichTextBox控件的這個 –
@faheemkhan:那麼這是一個好的開始。我可以閱讀richtextbox中的str。現在我要嘗試將它加載到word doc的新實例中。 –