C# 将 .doc 转换为 .htm
我正在尝试将 .doc 文件转换为 .htm 格式以在 ASP.NET MVC 页面中查看。
我在 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);
我尝试用 google 搜索将 .doc (甚至 .ppt )转换为 .htm 格式的方法,并且总是找到与上面有些类似的代码。
但我不断收到此异常:
由于以下错误,检索 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败:80040154 类未注册(HRESULT 异常:0x80040154 (REGDB_E_CLASSNOTREG))。
在该行:
Microsoft.Office.Interop.Word.Application objWord = new Microsoft.Office.Interop.Word.Application();
这是因为我安装了 Word Starter 2010 而不是完整的 Office 2010,还是有其他解决方案?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在服务器端使用 MS Office 中的 COM 对象并不是一个好主意。第一个问题是技术性的 - 进程存在几个陷阱(即有时 excel/word 在调用 Quit() 后不会退出)。这并不容易,但它是可以解决的。
然而第二个问题是许可。您需要为每个将使用 MS Office 的用户提供许可证。因此,如果您想在互联网上使用它,您将遇到严重的财务问题。
有几个库可以在不安装 MS Office 的情况下打开(保存、转换等)MS Office 格式。我曾与 Aspose 合作过图书馆,但还有其他几个。
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.
您收到此异常是因为 COM 对象未配置为允许 aspnet 用户身份的启动和访问权限。
最好将应用程序池标识用户更改为“网络服务”,该用户有足够的权限来执行 COM+ 组件。
有关更多详细信息检查此
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