C# convert .doc to .htm

2020-04-08 16:09发布

问题:

I am trying to convert .doc file to .htm format to view in an ASP.NET MVC page.

I am using the following code in C# :

using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;

....

Microsoft.Office.Interop.Word.Application objWord = new Microsoft.Office.Interop.Word.Application();

            object source = @"C:\Users\XYZ\Desktop\ScreenShot.doc";
            object target = @"C:\Users\XYZ\Desktop\ScreenShot.html";
            object unknown = Type.Missing;
            objWord.Documents.Open(ref source, 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.wdFormatRTF;
            objWord.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);

I have tried to google the way to convert .doc ( even .ppt ) to .htm format and have always found code somewhat similar to the above.

But I keep getting this exception :

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

at the line :

Microsoft.Office.Interop.Word.Application objWord = new Microsoft.Office.Interop.Word.Application();

Is this due to the reason that I have a Word Starter 2010 installed and not the complete Office 2010, or is there some other solution to it ?

回答1:

Using COM objects from MS Office on server side is not good idea. The frist problem is technical - there are several pitfalls with processes (i.e. sometimes excel/word does not quit after calling Quit()). It is not easy, but it is solvable.

However the second problem is licensing. You need license for every user who will be using the MS Office. So, if you want use it on internet web, you will have serious financial issues.

There are several libraries which can open (save, convert, etc...) MS Office formats without having MS Office installed. I worked once with Aspose library, but there are several others.



回答2:

You got this exception as COM object is not configured to allow launch and access permissions for the aspnet user identity. It's better to change Application Pool Identity user to be "Network Service", which which has enough permissions to execute COM+ components.

For more details check this