C# 如何将RTF转换为纯文本?

C# 如何将RTF转换为纯文本?,c#,format,rtf,C#,Format,Rtf,我收到一个相当大的excel文件,每行包含一个来自oracle数据库的clob转储,其中一个可能如下所示: {\rtf1\ansi\deff0\deftab708{\fonttbl{\f0\fnil\fcharset0 Courier New;}{\f1\fnil\fcharset0 Arial;}{\f2\fnil\fcharset0 MS Sans Serif;}{\f3\fnil\fcharset0 Times New Roman;}{\f4\fnil\fcharset238 Times

我收到一个相当大的excel文件,每行包含一个来自oracle数据库的clob转储,其中一个可能如下所示:

{\rtf1\ansi\deff0\deftab708{\fonttbl{\f0\fnil\fcharset0 Courier New;}{\f1\fnil\fcharset0 Arial;}{\f2\fnil\fcharset0 MS Sans Serif;}{\f3\fnil\fcharset0 Times New Roman;}{\f4\fnil\fcharset238 Times New Roman CE;}{\f5\fnil\fcharset204 Times New Roman Cyr;}{\f6\fnil\fcharset161 Times New Roman Greek;}{\f7\fnil\fcharset162 Times New Roman Tur;}{\f8\fnil\fcharset186 Times New Roman Baltic;}}{\colortbl\red0\green0\blue0;\red255\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red128\green0\blue128;\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green128\blue0;\red128\green0\blue0;\red128\green128\blue128;\red255\green255\blue255;}\paperw11906\paperh16838\margl1417\margr1417\margt1417\margb1417{\*\pnseclvl1\pnucrm\pnstart1\pnhang\pnindent720{\pntxtb}{\pntxta{.}}}{\*\pnseclvl2\pnucltr\pnstart1\pnhang\pnindent720{\pntxtb}{\pntxta{.}}}{\*\pnseclvl3\pndec\pnstart1\pnhang\pnindent720{\pntxtb}{\pntxta{.}}}{\*\pnseclvl4\pnlcltr\pnstart1\pnhang\pnindent720{\pntxtb}{\pntxta{)}}}{\*\pnseclvl5\pndec\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}{\*\pnseclvl6\pnlcltr\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}{\*\pnseclvl7\pnlcrm\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}{\*\pnseclvl8\pnlcltr\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}{\*\pnseclvl9\pnlcrm\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}{\pard\ql\li0\fi0\ri0\sb0\sl\sa0 \plain\f3\fs24\cf0 FOO FOO FOO \'85\'85. \'85\'85..}}
现在,通过将这些数据放入
System.Windows.Forms.RichTextBox
.Rtf
中,然后读取其
.Text
值,我得到了一个简单的转换。但是,不知何故,它带来了它的新词

我试着用手把它们取下来

rtf.Replace(“\n”和“).Replace(“\r”和“).Replace(Environment.NewLine)”

但这似乎没有帮助

有人知道如何将富格文本格式转换为单行纯文本吗?

在.NET Framework中,您可以使用RichTextBox控件创建支持RTF的字处理器,并使用户能够以所见即所得的方式对文本应用格式

您还可以使用RichTextBox控件以编程方式从文档中删除RTF格式代码并将其转换为纯文本。执行此类操作不需要将控件嵌入Windows窗体中。

看看这个,提取代码以保存

更新了——VB.NET程序的复制和粘贴错误——对不起,各位

class ConvertFromRTF
{
    static void Main()
    {

        string path = @"test.rtf";

        //Create the RichTextBox. (Requires a reference to System.Windows.Forms.dll.)
        using(System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox());
        {

            // Get the contents of the RTF file. Note that when it is 
           // stored in the string, it is encoded as UTF-16. 
            string s = System.IO.File.ReadAllText(path);

            // Convert the RTF to plain text.
            rtBox.Rtf = s;
            string plainText = rtBox.Text;

            // Now just remove the new line constants
            plainText = plainText.Replace("\r\n", ",");

            // Output plain text to file, encoded as UTF-8.
            System.IO.File.WriteAllText(@"output.txt", plainText);
        }
    }
}

您是要在原始rtf上还是在RichTextBox.Text中的纯字符串上进行替换?这很接近,但不能完全满足OP的需要,请参阅我的答案。
ControlChars
在哪里定义?OP说他/她已经试图替换
\n
\r
@L.B,你需要将它们作为一个分组一起替换——或者至少我发现了这一点。@L.B,这是一个VB.NET程序的复制粘贴错误,我在回答中提到了这一点。很抱歉出现任何困惑或挫折。@Mike我仍然有问题,但是
rtf=rtf.Replace(“\n”,“替换”).Replace(“\r”,“替换”).Replace(Environment.NewLine,”).Replace(“\\par”,“替换”)似乎解决了这个问题。什么是
\par?
您真的应该处理
RichTextBox