2012-09-28 198 views
1

我已经被赋予了相当大的Excel文件,每行包含我们的Oracle数据库的一个CLOB转储,其中一人可能是这样的:现在如何将RTF转换为纯文本?

{\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, "")

删除它们,但它似乎并没有帮助。

有谁知道我怎么能富文本格式转换为单行纯文本

+1

您是否试图对原始rtf或RichTextBox.Text中的纯字符串进行替换? –

回答

8

看看这个example,代码提取保存。

已更新 - 从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); 
     } 
    } 
} 
+0

“ControlChars”的定义在哪里? OP说他/她已经试图替换'\ n'和'\ r'。 –

+0

@ L.B,你需要将它们合并为一个分组 - 至少这是我发现的。 –

+0

@ L.B,它是一个VB.NET程序的复制和粘贴错误,我现在在我的答案中提到过。抱歉有任何困惑或沮丧。 –

2

How to: Convert RTF to Plain Text (C# Programming Guide)

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

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

+0

这已经是OP所做的...... –

+0

这很接近,但没有完全实现OP的需求,请看我的答案。 –