0

下面是用于从 Document 读取数据并移植到 Textfile 的代码,

但是,在写入文本文件之前,我想删除或忽略文档中存在的特殊字符。特殊字符是指箭头、项目符号、复制符号等。当涉及到文本文件时,它会显示一些随机字符.所以,我想在写入文本文件之前删除或忽略那些字符或符号。

object file;

file = filepathtb.Text;

object Target = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Temp_str.txt";
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();

object Unknown = Type.Missing;
newApp.Documents.Open(ref file, 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, ref Unknown);
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;

// if(newApp.ActiveDocument.Content.Characters = a

newApp.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);
4

1 回答 1

1

尝试这样的事情:

string myText = "sample text...";
string formattedText = String.Empty;

foreach(char c in myText)
{
    if(Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c) || Char.IsPunctuation(c))
        formattedText += c;
}
于 2013-03-29T09:24:13.127 回答