繁体   English   中英

使用C#将Word文档转换为文本文档

[英]convert a word doc to text doc using C#

因此,我目前正在尝试将单词doc(.doc)转换为文本文档,因为我想在其上使用正则表达式来查找文档中的内容。 因此,我想出了以下内容,它将Word文档转换为RTF格式(通过将其附加到RTF框中),但这并没有转换为纯文本格式。 当我尝试使用常规文本文档时,它会在新行上打印每个单词。 我无法在C#中找到有关如何执行此操作的任何信息。 我正在使用C#和Visual Studio 2010。

我不希望文档中有任何特殊字符(如粗体,下划线等),但是如果有人知道我如何能够变得更强大并提取出那些超级棒的字符。

我希望将其作为文本文档,因为我知道我可以在常规文本上使用多种方法,但是我怀疑由于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 
        }


    }
}

微软说您不应该使用Microsoft Office Interop来操纵自动化应用程序中的文档。

您可以使用Spire Doc等免费库将Word Doc转换为TXT,然后打开txt文件。 我认为有一种方法可以从Spire直接保存到MemoryStream ,但是我不确定。 (我知道Aspose Words中有,但这不是免费的)。

private void button1_Click(object sender, EventArgs e)
{
    //Open word document
    Document document = new Document();
    string docPath = @"C:\Users\<computer name>\Documents\TestItemHelpers";

    document.LoadFromFile(Path.Combine(docPath,"TestWordDoc.docx"));

    //Save doc file.
    document.SaveToFile(Path.Combine(docPath,"TestTxt.txt"), FileFormat.Txt);

    string readText = File.ReadAllText(Path.Combine(docPath,"TestTxt.txt"));

    //do regex here
}

编辑:如果您要使用Interop,因为它可以进行用户运行的活动(如注释中所指出),则可以将文档另存为文本文件,然后执行正则表达式:

private void button1_Click(object sender, EventArgs e)
{
    string docPath = @"C:\Users\<computer name>\Documents\TestItemHelpers"
    string testFile = "TestWordDoc.docx";

    Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
    Document document = application.Documents.Open(Path.Combine(docPath,testFile );
    application.ActiveDocument.SaveAs(Path.Combine(docPath,"TestTxt.txt"), WdSaveFormat.wdFormatText, ref noEncodingDialog);
    ((_Application)application).Quit();

    string readText = File.ReadAllText(Path.Combine(docPath,"TestTxt.txt"));

    //do regex here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM