问题描述:刚开始在调试模式下可以转换成功,部署到问win2003 iis6下无法转换,后来设置权限可以转换,但部署到win2008 iis7下 无法转换,发现win2008下没有office组件,但是确实安装了(详见1解决);后来是无法访问上传doc文档,设置站点的asp net用户访问权限,后来读取 Document doc = (Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, (object)docs, new Object[] { fileName, true, true });doc返回null值(详见2,3)
1.不到 office组件的问题
在运行中 输入 regedit 打开注册表
进行收索“000209FF-0000-0000-C000-000000000046”,你会找到下面这个东东:
将“AppID”修改为“CLSID” 然后保存,如果没有就新建CLSID 值:{00020906-0000-0000-C000-000000000046} 重启服务器
之后查看组件服务应该就会出现了
参考:http://blog.youkuaiyun.com/w3031213101/article/details/6891173
2.组件服务配置
(1)、 Cmd中输入命令 comexp.msc -32 启动组件服务(启动组件服务 方法 运行 mmc、mmc -32(32) 、DCOMCNFG.exe(64)) 不行一个一个
(2)、 依次双击"组件服务"->"计算机"->"我的电脑"->"DCOM配置";
(3)、 在"DCOM配置"中找到"Microsoft office word 97-2003",在它上面点击右键,然后点击"属性",弹出"Microsoft word应用程序属性"对话框;
(4)、 点击"标识"标签,选择 启动用户(默认选项) ,我修改为交互式 (网上说这个情况下面,当服务器未被重启且无用户登录下,会出现8000401a错误,就是无权限访问,我没有测试);
(5)、 点击"安全"标签,在以下 三个项目中都选中 “自定义” 添加 ”everyone” 并分配最大权限(全选)
(6) 、设置Web.config文件 在Web.config文件的<system.web>中加入<identity impersonate="true"/> (没有这一步 会报word进程无法打开相应的word文件)。
参考:http://www.cnblogs.com/tangbinblog/archive/2012/11/29/2794110.html
3.新建个Desktop目录
・Windows 2008 Server x64
Please make this folder.
C:\Windows\SysWOW64\config\systemprofile\Desktop
・Windows 2008 Server x86
Please make this folder.
C:\Windows\System32\config\systemprofile\Desktop
并将访问权限设置为net service
参考:http://www.cnblogs.com/TonyJoule/archive/2010/05/18/1737973.html
http://bbs.youkuaiyun.com/topics/360262598
附转换代码
string wordPath = Server.MapPath("/word/1.doc");
string htmlPath = Server.MapPath("/html/1.html");
//上传word文件, 由于只是做示例,在这里不多做文件类型、大小、格式以及是否存在的判断
FileUpload1.SaveAs(wordPath);
#region 文件格式转换
//请引用Microsoft.Office.Interop.Word
ApplicationClass word = new ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents;
// 打开文件
Type docsType = docs.GetType();
object fileName = wordPath;
Document doc = (Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, (object)docs, new Object[] { fileName, true, true });
//判断与文件转换相关的文件是否存在,存在则删除。(这里,最好还判断一下存放文件的目录是否存在,不存在则创建)
if (File.Exists(htmlPath)) { File.Delete(htmlPath); }
//每一个html文件,有一个对应的存放html相关元素的文件夹(html文件名.files)
if (Directory.Exists(htmlPath.Replace(".html", ".files")))
{
Directory.Delete(htmlPath.Replace(".html", ".files"), true);
};
//转换格式,调用word的“另存为”方法
Type docType = doc.GetType();
object saveFileName = htmlPath;
docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatHTML });
// 退出 Word
wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null);