Import doc and docx files into .Net and C #

I am writing a text editor and I want to add the ability to import .doc and .docx files. I know I can use OLE Automation, but if I use the recent OLE library, it will not work with people who have an older version of Word, and if I use an older version instead, it will not be able to read .docx. Any ideas? Thanks

EDIT: Another solution would be to, like my HTML and RTF application, convert the command line .doc and .docx files to one of these formats, for example: http://www.snee.com /bobdc.blog/ 2007/09 / using-word-for-command-line-co.html

+3
source share
3 answers

It works with the Office 2003 PIA tested on my computer with Office 2010:

using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;

public string GetHtmlFromDoc(string path)
    var wordApp = new Application {Visible = false};

//Cargar documento
            object srcPath = path;
            var wordDoc = wordApp.Documents.Open(ref srcPath);

            //Guardarlo en HTML
            string destPath = Path.Combine(Path.GetTempPath(), "word" + (new Random().Next()) + ".html");
            if (wordDoc != null)
            {
                object oDestPath = destPath;
                object exportFormat = WdSaveFormat.wdFormatHTML;
                wordDoc.SaveAs(ref oDestPath, ref exportFormat);
            }

            //Cerrar
            wordDoc.Close();
            wordApp.Quit();

            //Comprobar que el archivo existe);
            if (File.Exists(destPath))
            {
               return File.ReadAllText(destPath, Encoding.Default);
}
return null;
}
+2
source

Why not use Office Primary Interop Assemblies (PIA)?

I think you have to decide which versions of Word you want to support. I suggest you settle for Word 2003 as the lowest. This will allow you to use PIA Office 2003 and the program against them. Installing PIA on the machine also sets up call forwarding redirects, so they work with newer versions in Word. There should be no problem opening .docx files from Word 2007 or 2010 through PIA Office 2003, although I have not tried this myself.

+1
source

OpenXML xpath .NET / docx.

0

Source: https://habr.com/ru/post/1781344/


All Articles