夫天地者,万物之逆旅;光阴者,百代之过客。而浮生若梦,为欢几何?
Microsoft.Office.Interop.Word 组件 Word 转 HTML 设置转换后的页面Title

起因

最近在做将word文件转换成html实现在线查看的功能,虽然使用Microsoft.Office.Interop.Word组件转换后的效果不尽理想,但客户有此需求,还是要搞定。转换后的html页面标题在客户浏览器中显示的是生成的文件名和文件地址,导致体验很差。关于设置转换后页面标题的案例,网上又少又不全,一番搜索、尝试终于找到如下解决方案:

核心代码

Microsoft.Office.Interop.Word.ApplicationClass class2 = null;
            Microsoft.Office.Interop.Word.Document activeDocument = null;
            try
            {
                class2 = new Microsoft.Office.Interop.Word.ApplicationClass();
                class2.Documents.Open(ref obj7, ref confirmConversions, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref missing, ref missing);
                class2.Visible = false;
                activeDocument = class2.ActiveDocument;

                #region 获取高级属性对象
                try
                {
                    object oDocCustomProps = activeDocument.BuiltInDocumentProperties;
                    Type typeDocCustomProps = oDocCustomProps.GetType();
                    var oPropItem = typeDocCustomProps.InvokeMember("Item",
                                      System.Reflection.BindingFlags.Default |
                                      System.Reflection.BindingFlags.GetProperty,
                                      null, oDocCustomProps,
                                      new object[] { "TITLE" });
                    var typeItemProp = oPropItem.GetType();
                    typeItemProp.InvokeMember("Value",
                                                       System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.SetProperty,
                                                       null, oPropItem,
                                                       new object[] { downFileInfoName });
                }
                catch (Exception ex)
                {
                     
                }
                #endregion

                activeDocument.SaveAs(ref fileName, ref fileFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (activeDocument != null)
                    activeDocument.Close(ref missing, ref missing, ref missing);
                if (class2 != null)
                    class2.Quit(ref missing, ref missing, ref missing);
            }


作者:暗夜余晖

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

0

支持

0

反对

posted @2018-4-17  拜读(2043)

评论列表

评论内容:



喜欢请打赏

支付宝 微信

请放心支付