【问题标题】:convert a word doc to text doc using C#使用 C# 将 word doc 转换为 text doc
【发布时间】:2014-05-15 19:30:19
【问题描述】:

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

我不希望文档中出现任何特殊字符(如粗体、下划线等),但如果有人知道我如何能够健壮并提取那些非常棒的字符。

我希望它作为文本文档,因为我知道有几种方法可以用于常规文本,但我怀疑它们是否适用于 word 文本,因为 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 中复制整个文档内容并将其粘贴到文本编辑器中?如果这只是一次性任务,那肯定是获取纯文本文档的最快途径。
  • 我会收到很多这样的文件,手动操作似乎有点不切实际。我知道如何手工完成,但我希望有一个更简单的解决方案。
  • @BenAaronson 我逐行写入文本文档只是为了测试它是否有效。您是否认为 word doc 中的某些特殊字符可以将文本等效行翻译为 word doc 的单个单词?...

标签: c# regex text ms-word converter


【解决方案1】:

Microsoft 说您不应该使用 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,因为它适用于用户运行的活动(如 cmets 中所指出的),您可以将文档保存为文本文件,然后执行正则表达式:

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
}

【讨论】:

  • 您的第一个链接仅适用于服务器端处理。它非常适合用户运行的应用程序。
  • 我的程序可能会被用于服务器端工作,所以这对我来说实际上可能是完美的。
  • 我添加了 Interop SaveAs 以防您也有兴趣以这种方式查看。
  • 所以我查看了 spire doc 的资料,但免费版本最多只能阅读 100 段,这可能不符合我的目的。
  • 还有其他付费库可以阅读超过 100 段,但您可以使用它进行测试。如果你打算在服务器上使用它,那么你肯定会想要使用 MS Word Interop 以外的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多