所以我目前正在尝试将Word文档(.DOC)转换为文本文件,因为我想用它的正则表达式找东西的文档中的文本文档。所以我想出了下面的内容,它将Word文档转换为富文本格式(通过将其附加到富文本框中),但这不会转换为纯文本格式。当我用普通的文本文档进行尝试时,它会在新行上打印每个单词。我一直无法找到有关如何在C#中执行此操作的任何信息。我使用的是C#和visual studio 2010.我不希望文档中有任何特殊字符(如粗体,下划线等),但是如果有人知道我如何能够健壮并提取那些超级真棒。转换为Word文档用C#
我想把它当作一个文本文档,因为有几种方法,我知道我可以在普通文本中使用,但我怀疑他们会在字的文字工作,由于附带的Word文档隐藏/特殊字符。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
namespace ReadWordDocProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string testFile = @"C:\Users\<mycomputer>\Documents\TestItemHelpers\TestWordDoc.docx";
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document document = application.Documents.Open(testFile);//path here
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
string text = document.Words[i].Text;
//Do output with text here
richTextBox1.AppendText(text);
}
((_Application)application).Quit(); //cast as _Application because there's ambiguity
}
}
}
“当我与它印上了一个新行的每一个字普通的文本文档试图”什么是你在这里尝试的代码? –
作为一种非编程解决方案,您是否尝试过从Word中复制整个文档内容并将其粘贴到文本编辑器中?如果这只是一次性任务,那肯定是通向纯文本文档的最快途径。 – adv12
我会有很多像这样的文件进来,这似乎有点不切实际。我知道该怎么做,但我希望能有一个更简单的解决方案。 – user3003304